Sign in to follow this  
Aiwendil

Independent Sprites Move Together

Recommended Posts

Hi again, I'm having a huge problem with my game at the moment. I'm trying to make sprites patrol back and forth, and the patrolling works, but when I have more than one sprite, they move together. I'll try to clarify what I just wrote. When I create my sprites, I give each a starting position, a starting patrol point (same as the sprite's starting position), and an ending patrol point. The sprite then moves from the starting patrol point to the ending patrol point and back. The problem is, all of the patrolling sprites travel the same distance. I've set the starting point of one sprite to (0.0f ,0.0f, 0.0f) and the end at (200.0f, 0.0f, 0.0f), and the other sprite started at (100.0f, 200.0f, 0.0f) and ended at (1500.0f, 200.0f, 0.0f). Both sprites then moved only 200 points, then reversed direction. The second sprite should have travelled for a much greater distance. Here is the code: Game.cpp
void Game::loadGame()
{
	mEnemies.resize(2);

	for (unsigned int i = 0; i < mEnemies.size(); i++)
	{
		mEnemies[i].createAnimatedSprite(true);
		mEnemies[i].setFrameWidth(136);
		mEnemies[i].setFrameHeight(124);
		mEnemies[i].setNumberOfFrames(13);
	}

	D3DXVECTOR3 enemyPos(0.0f, 0.0f, 0.0f);
	mEnemies[0].setPos(enemyPos);
	mEnemies[0].setPatrolStart(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
	mEnemies[0].setPatrolEnd(D3DXVECTOR3(200.0f, 0.0f, 0.0f));

	enemyPos = D3DXVECTOR3(100.0f, 200.0f, 0.0f);
	mEnemies[1].setPos(enemyPos);
	mEnemies[1].setPatrolStart(D3DXVECTOR3(100.0f, 200.0f, 0.0f));
	mEnemies[1].setPatrolEnd(D3DXVECTOR3(1500.0f, 200.0f, 0.0f));
}

void Game::drawObjects()
{

	for (unsigned int i = 0; i < mEnemies.size(); i++)
	{
		mEnemies[i].draw(mSprite, mViewMat, mEnemyTexture);
		mEnemies[i].drawCollisionLines(mLine);

		if (mPlayer.collision(mEnemies[i].getCollisionRect()))
			mGameState = Lose;
	}
}

void Game::updateObjects()
{
	for (unsigned int i = 0; i < mEnemies.size(); i++)
	{
		if (mEnemies[i].getDirection() == LEFT || mEnemies[i].getDirection() == RIGHT)
		{
			mEnemies[i].setFrameY(0 * mEnemies[i].getFrameHeight());
		}
		else if (mEnemies[i].getDirection() == UP)
		{
			mEnemies[i].setFrameY(4 * mEnemies[i].getFrameHeight());
		}
		else if (mEnemies[i].getDirection() == DOWN)
		{
			mEnemies[i].setFrameY(1 * mEnemies[i].getFrameHeight());
		}
		else if (mEnemies[i].getDirection() == DIAGONAL_UP_LEFT || mEnemies[i].getDirection() == DIAGONAL_UP_RIGHT)
		{
			mEnemies[i].setFrameY(2 * mEnemies[i].getFrameHeight());
		}
		else if (mEnemies[i].getDirection() == DIAGONAL_DOWN_LEFT || mEnemies[i].getDirection() == DIAGONAL_DOWN_RIGHT)
		{
			mEnemies[i].setFrameY(3 * mEnemies[i].getFrameHeight());
		}

		if (mEnemies[i].getDirection() == LEFT || mEnemies[i].getDirection() == DIAGONAL_UP_LEFT || mEnemies[i].getDirection() == DIAGONAL_DOWN_LEFT)
		{
			mEnemies[i].scale(-1.0f, 1.0f);
		}
		else
		{
			mEnemies[i].scale(1.0f, 1.0f);
		}

		mEnemies[i].setCollisionRect((0 + mViewMat._41) / universalScaleX,
									 (0 + mViewMat._42) / universalScaleY,
									 (100 + mEnemies[i].getWidth() + mViewMat._41) / universalScaleX,
									 (100 + mEnemies[i].getHeight() + mViewMat._42) / universalScaleY);
		mEnemies[i].update(mTimer.GetTimeElapsed(), 15.0f);

		mEnemies[i].patrol(mTimer.GetTimeElapsed());
	}
}

Sprite.cpp:
void Sprite::update(float dt, float update)
{
	static int index = 0;
	static bool bothX = false;
	static bool bothY = false;

	if (mAnimated)
	{
		mCurrentFrame += update * dt;
		if ((int)mCurrentFrame > mNumFrames)
			mCurrentFrame = 0.0f;
	}
}

void Sprite::patrol(float dt)
{
	static bool movingLeft = false;
	static bool movingRight = true;

	if (movingLeft)
	{
		mDirection = LEFT;
		if (mPos.x > mPatrolStart.x - 5 && mPos.x < mPatrolStart.x + 5)
		{
			mPos.x = mPatrolStart.x;
			movingLeft = false;
			movingRight = true;
		}

		moveHorizontally(dt, -200.0f);
	}
	else
	{
		mDirection = RIGHT;
		if (mPos.x > mPatrolEnd.x - 5 && mPos.x < mPatrolEnd.x + 5)
		{
			mPos.x = mPatrolEnd.x;
			movingLeft = true;
			movingRight = false;
		}

		moveHorizontally(dt, 200.0f);
	}
}

I've tried adding a sprite that is not in the mEnemies vector, but it still only moved the same amount as the other two. Any ideas? I'm completely lost, and this is the only thing I have left to do in my project, and it's due on Thursday (I'm entering it in a competition), so I'd appreciate any fast replies. Thanks.

Share this post


Link to post
Share on other sites
It could be that you use static variables in your class member functions. These are probably being shared by all objects when the member function is called. That is, each object is sharing that member function's static data. Make those static variables part of the class data and see if that helps.

Share this post


Link to post
Share on other sites
Yes, as nobodynews wrote, replace the static bools with member bools in the Sprite class. As you have it now, once one object changes from left to right all others will see the same change.

As an unrelated side note, a tip is to replace (in Sprite::update())
if ((int)mCurrentFrame > mNumFrames)
mCurrentFrame = 0.0f;

with
while ((int)mCurrentFrame > mNumFrames)
mCurrentFrame -= (float)mNumFrames;

Then if dt grows large between two frames you won't see a hiccup when it loops around, plus it will handle more than one loop-around in one go if needed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this