Jump to content
  • Advertisement
Sign in to follow this  
Chad Smith

Moving my enemies

This topic is 4493 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

EDIT: This post right here, is about how I couldn't get my enemies loaded right. That is now fixed, so I changed the topic title to my new problem. Making the enemies move. Go to the bottom of this topic, to see the post on it. Hey, me and my friend are creating a little 2D Sidescroller. I am trying to implent the enemies inside of the game right now. So, I create a enemy class. This is what the drawing function inside my enemy class looks like:
void Enemy::DrawEnemy(int sourcew, int sourceh, int x, int y)
{
     sourcew=enemySource.w;
     sourceh=enemySource.h;
     x=enemyDest.x;
     y=enemyDest.y;
     SDL_BlitSurface(enemy, &enemySource, SDL_GetVideoSurface(), &enemyDest);
}



Next, is when I create the Enemies instace. I do it as follows
Enemy enemy[10];
Next, I Init everyone of the enemies like this:
enemy[0].CreateEnemy(2);
enemy[1].CreateEnemy(2);
enemy[2].CreateEnemy(2);
enemy[3].CreateEnemy(2);
enemy[4].CreateEnemy(2);
enemy[5].CreateEnemy(2);
enemy[6].CreateEnemy(2);
enemy[7].CreateEnemy(2);
enemy[8].CreateEnemy(2);
enemy[9].CreateEnemy(2);



Also, I know it would be better to use a for loop to do that, and I had it like that, but it didn't work how I wanted it to work, so I wanted to see if it was my for loop doing it. Guess not. Anyway, back to the code! lol. Next, I draw all the enemies:
enemy[0].DrawEnemy(128, 128, 200, 300);
enemy[1].DrawEnemy(128, 128, 200, 400);
enemy[3].DrawEnemy(128, 128, 400, 200);
enemy[4].DrawEnemy(128, 128, 700, 400);
enemy[5].DrawEnemy(128, 128, 700, 300);
enemy[6].DrawEnemy(128, 128, 700, 200);
enemy[7].DrawEnemy(128, 128, 900, 300);
enemy[8].DrawEnemy(128, 128, 1000, 200);
enemy[9].DrawEnemy(128, 128, 2000, 500);



Again, I would have used a for loop here, but I wanted to see if my for loop was messed up. Here is the problem. All the enemies get drawn, but they always get drawn in the top left corner of the screen! Why is this happening? Is my logic wrong? Am I taking this a hardway, or what? Any help on enemies on this stuff will be helpfull! Thanks, Chad! [Edited by - Chad Smith on March 15, 2006 6:32:14 PM]

Share this post


Link to post
Share on other sites
Advertisement
Your code isn't copying your position information correctly. It should be


void Enemy::DrawEnemy(int sourcew, int sourceh, int x, int y)
{
enemySource.w = sourcew;
enemySource.h = sourceh;
enemyDest.x=x;
enemyDest.y=y;
SDL_BlitSurface(enemy, &enemySource, SDL_GetVideoSurface(), &enemyDest);
}

Share this post


Link to post
Share on other sites
OMG! That means I need to take a break for a little bit. That was a freaking simple newbie newbie mistake.

Thanks again dude!

rating++;

Chad.

Share this post


Link to post
Share on other sites
Alright,
today I worked really hard on some other stuff, but now I need to get the enemies moving. I did what I would usually do on moving stuff, to get the enemies moving. So, this is what I did. First off, I changed my Drawing function for the enemies. It now looks like the following:


DrawEnemy(int sourcew, int sourceh, int x, int y, float XVel);


That way, I can manage each of the enemies x velocity right when I draw them, without each and everysingle enemy having the same velocity. That would get very repetive. Also, inside the DrawEnemy function I do this now.


XVelEnemy=XVel;


Next, I added a function inside my enemyclass that now updates the destination of the enemy. The function looks like this in my enemy.cpp file:


void Enemy::UpdateEnemy()
{
enemyDest.x-=XVelEnemy;
}


So that should update the destination to move towards the main character as far as I am concerned.

Finally, how I call update in my main function. I do it as follows:


for(int i = 0; i<10; i++)
{
enemy.UpdateEnemy();
}


So, as far as I am aware this illiterates through the loop 9 times, and each of the enemies should be updated per frame. Am I right, or is my logic plain wrong?

I am sorry for asking for this much help, and I know I shouldn't since I need to learn from my own mistakes. I usually fix my own problems, but this is not making any since to me at all! Also, I am sure there is something that might be missing and I am just dumb enough to miss it, to make the whole thing wrong.

So, thanks to all who help!

Also, I am initing all the velocities to 2.

Chad.

Share this post


Link to post
Share on other sites
enemyDest is the point to where it should move, right? If it is, you are doing wrong stuff. You shouldn't change the destination when updating the position. You should change the actual coordinates of the object.

First, you must have 2 components of the velocity. Let's call the Vx and Vy. Then, on the Update function, you add Vx to the x coordinate and Vy to the y coordinate of the enemy. To make sure you won't let the enemy go past enemyDest, you do a verification to see if it passed through -- if it did, place there and zero the velocity, since movement is done.

Share this post


Link to post
Share on other sites
Umm...maybe you just didn't understand the type of game I am creating. I want it where the velocity dosen't go back to 0. And the destination, is where it starts. It is a SDL_Rect variable. And, that is how I have done all of my motion, and if you want the proof, then I will show you my bullet class, as it uses the exact same thing.

What could be going on?


Chad.

Share this post


Link to post
Share on other sites
The loop iterates 10 times, from 0..9. You shouldn't pass a velocity to DrawEnemy, as the velocity has nothing to do with drawing it. You should pass the velocity to UpdateEnemy. DrawEnemy shouldn't call UpdateEnemy or vice versa - they should be called independently by your game loop. Really every enemy should have its own velocity or you're going to have problems if and when you want them to move at different speeds.

Share this post


Link to post
Share on other sites
Thank you for that.

So, instead should I make the velocity variable an array? I would use vectors(I don't even know if that would be good here or not)but I don't understand them at this time.

Also, I do call the update function outside of the draw function. In the draw function, I just assigned the velocity variable. I think I understand what is wrong. I will work on this a little bit more.

I am also going over to my friends house, to get some faster development going on, instead of just sending the graphics through AIM. We would use CVS or something to that matter but we don't have the money to spend on a server.

Thanks,
Chad.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chad Smith
I am also going over to my friends house, to get some faster development going on, instead of just sending the graphics through AIM. We would use CVS or something to that matter but we don't have the money to spend on a server.


Free + I have two tutorials on using it [smile] Should be straight forward, but I'll be on aim later today when you run into troubles.

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!