Jump to content
  • Advertisement
Sign in to follow this  
xM1k3x

Simple movement AI help

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

Hey all, Got a little problem I am trying to get some sprites to move around the screen independently but I am having some issues. They fly around for like 2 seconds and then they stick to the right side of the screen and done move. Maybe someone can see why lol! Thanks:
for ( int count = 0; count < 10; count++)
   {
	 EnemyPawn[count].x = count * 2 % SCREEN_WIDTH;
	 EnemyPawn[count].y = count* 2 % SCREEN_HEIGHT;
	 EnemyPawn[count].width = 64;
	 EnemyPawn[count].height = 64;
	 EnemyPawn[count].bufferx = 8;
	 EnemyPawn[count].buffery = 9;

	 EnemyPawn[count].x += EnemyPawn[count - 1].x * 3;
   }

for(int count = 0; count < 3; count++)
		  {
			EnemyPawn[count].x += EnemyPawn[count].bufferx;
			EnemyPawn[count].y += EnemyPawn[count].buffery;

			EnemyPawn[count].AI();
		  }

void EnemyShip::AI()
{
	
	if(EnemyShip::x < (SCREEN_WIDTH - EnemyShip::width))
	{
		EnemyShip::x += EnemyShip::bufferx;
	}
	if(EnemyShip::x > (SCREEN_WIDTH - EnemyShip::width))
	{
		EnemyShip::x -= EnemyShip::bufferx;
	}
	if(EnemyShip::y > SCREEN_HEIGHT - 200)
	{
		EnemyShip::y -= EnemyShip::buffery;
	}
	if(EnemyShip::y < SCREEN_HEIGHT - 200)
	{
		EnemyShip::y += EnemyShip::buffery;
	}

}

THanks for looking over it!

Share this post


Link to post
Share on other sites
Advertisement
Let me make sure I'm understanding some things. The two loops in front of your code are initialization, right? If so, It would be helpful if they were in seperate boxes. As it is, it's kind of confusing : ). Why does the second one only go through three iterations while the first goes through ten?

Ok, now the stuff that matters. What behavior are you trying to implement? Knowing that will help us understand your code and maybe suggest a different way of doing it.

And don't use class name when referring to non-static members. It's just confusing. I don't get how that compiled. When inside a member function, just use the member variable name, like this:

if(x < (SCREEN_WIDTH - width)){//etc.

Share this post


Link to post
Share on other sites
I believe right now, when your guys hit the right edge, they move to the left a bit (bufferx distance), then next frame move back to the right again.

You'll want to store which direction each enemy is currently moving in as well.

If "bufferx" is the amount to move each frame, then when they hit an edge you must negate it, and increment the position separately. Also, I think you have a bug with your first "if", shouldn't it be comparing to the left edge (0)?

if(x < 0)
{
bufferx = -bufferx;
}
if(x > (SCREEN_WIDTH - width))
{
bufferx = -bufferx;
}
x += bufferx;






Same thing with the y-direction, of course.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!