Public Group

# Make an array of pictures move?

This topic is 2632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Anyone wanna help? I now have a picture moving from the left to the right (I choose to make a picture move so I could see if the code worked properly). But now I want to make my array of enemies move at exactly the same path... But I have trouble with passing the coordinates of that picture to the array in the correct way. Here's the code: http://codepad.org/4Or3X8K5

Basically, I just want a block of enemies move from the left of the screen to the right of the screen, like in Galaxian or Space Invaders (without 'em going down, that is). So I made an array of pictures representing bad guys, which gave me a block... but how do I make them move...?

Thanks for the help.

##### Share on other sites
Why did you use a 2D array? you can just use a 1D array and loop through them. And you could make a struct that contains a xpos and a ypos and even place a surface in there. Something like:
 Struct enemy { int xpos; int ypos; Surface *graphic; }; 
And then what you need to do is make a boolean that decides the direction of all your bad guys are going in your array. and then change this boolean when they hit the end of the screen. Something like:
 for(int i = 0; i < theamountofenemies; i++) { if(enemy.xpos > screenwidth - the width of your enemy) {movingLeft = true; } else if( enemy.xpos < 0) { moveLeft = false; } if(moveLeft) { enemy.xpos -= 1;} else { enemy.xpos += 1;} } 
So what this code does is that when they hit the 0 (the right side of the screen), you change that boolean to false so that they move in opposite direction, until they hit the other side. And with the moveLeft boolean you decide if the x position of your enemy is going left or right. And you do that for each invader in your array.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 29
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634112
• Total Posts
3015581
×