Jump to content
  • Advertisement
Sign in to follow this  
JWindebank

Enemy Movement Patterns

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

Hi guys, Am working on some extremely basic enemy movement patterns and just can't get it to work. Can anyone see any problems with this please? First the pattern is loaded: enemy.cpp
void Enemy::setPattern(int directions, ...)
{
	va_list arguments;    
	
	va_start(arguments, directions);

	for(int x = 0; x < directions; x++)
	{
		m_Movement.push_back(va_arg(arguments, int))
	};
	
	m_Patterns = directions;

	va_end(arguments);         
}

main.cpp
Enemy enemy;

enemy.setPattern(LEFT, LEFT, STOPPED, RIGHT, RIGHT, STOPPED);

Then in each frame the enemy should be moved: enemy.cpp
void Enemy::move()
{
	switch (m_Movement.at(m_PatternPlace))
	{
		case STOPPED:
			break;
			
		case LEFT:
			addX(-ENEMYMOVE);
			break;
		
		case RIGHT:
			addX(ENEMYMOVE);
			break;
			
		case UP:
			addY(-ENEMYMOVE);
			break;
			
		case DOWN:
			addY(ENEMYMOVE);
			break;
	}

	m_PatternPlace++;

	if(m_PatternPlace == m_Patterns)
	{
		m_PatternPlace = 0;
	}
}

main.cpp
enemy.move();
enemy.draw();

The directions are an enumeration (STOPPED, LEFT, RIGHT, UP, DOWN). I know the movement will be extremely fast for now, but will slow it down once I get it moving properly. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
What is happening is that when the game starts the enemy just keeps going left...

Can anyone please give me some tips to at least debug this? At the moment it all seems perfectly fine to me, not sure what could be causing it. I can only think that the Enemy::move() function is not iterating through the pattern?

Thank you.

Share this post


Link to post
Share on other sites
Got it. :)

OK, for anyone interested. When using a function that can accept a multiple number of arguments, you need to specify how many arguments as an int first.

Example:

enemy.cpp

void Enemy::setPattern(int directions, ...)
{
va_list arguments;

va_start(arguments, directions);

for(int x = 0; x < directions; x++)
{
m_Movement.push_back(va_arg(arguments, int))
};

m_Patterns = directions;

va_end(arguments);
}



main.cpp

Enemy enemy;

enemy.setPattern(6, LEFT, LEFT, STOPPED, RIGHT, RIGHT, STOPPED);
^
//Needed this to tell the function how many arguments there were.



Now to allow for a slight pause to this and I'll have a relatively basic, working, enemy movement function. Cool! :D

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!