Different x y positions on several instances of same object

Axl Trauts

Member
Hi,

I am trying several things for this with no luck yet.

I created a shop, where you can buy 1-4 wingmen for the player (a plane). They will go to their respective position: two on the left, one behind the other and the other two on the right, one behind another, like a V formation. When the player moves, their x-y positions will move to keep formation.

I can buy all the four at the same time. They are created on the Draw Event of obj_player like this:

while(M_NUM_ALLY <> num_ally) // M_NUM_ALLY is a macro that points to an array position
{ instance_create(x,y,obj_ally); // The ally object
num_ally++; // a variable created at the start of obj_player to prevent creating infinite instances
break;
}

Now I need to do something on Step like this example:
//for the first ally:
x=obj.player.x+10
y=obj.player.y

//for the second ally:
x=obj.player.x+20
y=obj.player.y+10

//for the third ally:
x=obj.player.x-10
y=obj.player.y

//for the fourth ally:
x=obj.player.x-20
y=obj.player.y+10

But I am missing something about instance_number() somewhere. How can I do this?
 

Taddio

Member
Try something like this in your player instance. Just put their coordinates in each case.
Code:
if(instance_exists(obj_ally)){

var _num_wingman = instance_number(obj_ally);

switch(_num_wingman){
case 1:
    break;
case 2:
     break;
case 3:
     break;
case 4:
     break;
}
}
 

TheouAegis

Member
Code:
///Player Create///
AllyPos[0]=10;
AllyPos[1]=0;
AllyPos[2]=20;
AllyPos[3]=10;
AllyPos[4]=-10;
AllyPos[5]=0;
AllyPos[6]=-20;
AllyPos[7]=10;
Code:
///Player End Step (or at the very end of the Player's normal Step)///
var i = 0;
with obj_ally {
    x = other.x + other.AllyPos[i++];
    y = other.y + other.AllyPos[i++];
}
 

Axl Trauts

Member
Try something like this in your player instance. Just put their coordinates in each case.
Code:
if(instance_exists(obj_ally)){

var _num_wingman = instance_number(obj_ally);

switch(_num_wingman){
case 1:
    break;
case 2:
     break;
case 3:
     break;
case 4:
     break;
}
}
Thanks for your response! It worked but it moves all instances to the same position, depending on each case, as I feared. I am missing something:

Code:
if(instance_exists(obj_aliado)){

var _num_wingman = instance_number(obj_ally);

switch(_num_wingman){
    case 0:
        break;
    case 1: 
        obj_ally.x=x-largo/2
        obj_ally.y=y
        break;
    case 2:
        obj_ally.x=x+largo/2
        obj_ally.y=y
        break;
case 3:
        obj_ally.x=x-largo/2
        obj_ally.y=y+alto/2
     break;
case 4:
        obj_ally.x=x+largo/2
        obj_ally.y=y+alto/2
     break;
}
}
largo and alto just stores spride_width and sprite height
 

Axl Trauts

Member
Code:
///Player Create///
AllyPos[0]=10;
AllyPos[1]=0;
AllyPos[2]=20;
AllyPos[3]=10;
AllyPos[4]=-10;
AllyPos[5]=0;
AllyPos[6]=-20;
AllyPos[7]=10;
Code:
///Player End Step (or at the very end of the Player's normal Step)///
var i = 0;
with obj_ally {
    x = other.x + other.AllyPos[i++];
    y = other.y + other.AllyPos[i++];
}
Thanks! it works! but when I wanted to make the coordinates less hardcoded, using sprites length and width, it fails because the ally hasn´t been created yet. I´m not sure if I can change ally´s sprite later yet. For example:
AllyPos[0]=x-obj_ally.largo/2

obj_ally.largo stores obj_ally sprite_width.

Code:
PosAmigo[4]=-largo/2-16;
PosAmigo[5]=alto/2;
I´d like to change 16 to obj_ally.largo but well the "not set" error happens. Anyways I´m using it as a good base. Thanks
 

Taddio

Member
Are they simply drawn, or they are active objects with behavior? If the former, just use use draw_sprite_* for each one of them. In the case they are all objects with their own behavior, you will have to track their instance id's. You could have, for example, a 4-slots array or list which would contain every active ally id. Then use that id to set their position, not obj_ally (that will move all of them in the same spot, all 4 of them are obj_ally, after all).
Hope that helps
 

TheouAegis

Member
sprite_width is already a useable variable that gets updated with sprite_index.

you are getting errors because you are trying to reference the largo of the player, not the Ally, probably.

So, going off of my method, change the player's create event array to just 1, 0 or -1 (so +10 and +20 would be +1, -10 and -20 would be -1) for each entry. Then the End Step code would be

Code:
with obj_ally {
   x = other.x + other.AllyPos[i++]*sprite_width;
   y = other.y + other.AllyPos[i++]*sprite_height;
}
 
Top