Enemy Movement Patterns

Started by
1 comment, last by JWindebank 18 years, 10 months ago
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.
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.
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

This topic is closed to new replies.

Advertisement