Jump to content
  • Advertisement
Sign in to follow this  
Turbosheep

Make an array of pictures move?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!