πŸŽ‰ Celebrating 25 Years of GameDev.net! πŸŽ‰

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Enemy movement off

Started by
7 comments, last by Josheir 4Β years ago

I have this program that has an enemy walking back and forth on a platform. The display is in three parts, in the middle part the platforms scroll in the opposite direction of the player. This gives the illusion that the player is moving. When the player is in the mid zone and the enemies move at the opposite speed of the platform there appears to be no movement because they are set at 10 and -10. What I want is the platform enemy to continue to move back and forth at the same speed of the player character and bottom walking enemies. I tried changing the speed of the enemy to 20 and it moves back and forth, but it is to quick.

Here is the video:

Here is the GitHub: Still coupled in some places.

https://github.com/josheirm/MarioBros/tree/moveplatforms​

Here is the code:

window.draw(marioSpriteL);

			if (character.gPlatformsMoved == "right")
			{
				miscObject.moveEnemies(window, 10, 0, 1);
				character.EnemyObject->changePlatformEdgesForEnemy(window, -10);
				platform::SearchVectorForPlatforms(vectorPlatforms, window, -10, 0.0f, 0);
				character.EnemyObject->moveEnemies(window, 20, -10, 0);

				character.gPlatformsMoved = "none";
			}
			else
			{
				miscObject.moveEnemies(window, 10, 0, 1);

				platform::SearchVectorForPlatforms(vectorPlatforms, window, 0, 0.0f, 0);
				character.EnemyObject->moveEnemies(window, 10, -1, 0);
			}

int Enemy::moveEnemies(sf::RenderWindow& inWindow, float changeX, float changeY, int isLeft1)
{
	//return(1);
	//inWindow.clear();
	for (std::vector<EnemyOnPlatform>::iterator it = enemyVector2.begin(); it != enemyVector2.end(); ++it)
	{
		//
		if (direction == right)
		{
			gottenSprite1 = it->getSpriteR();
		}
		else
		{
			gottenSprite2 = it->getSpriteL();
		}
		//////////////




		////////////////

		if (direction == right)
		{
			it->x = it->x + changeX;
		
			if (it->x >= it->platformRightEdge-76)
			{
				it->x = it->platformRightEdge-76;
				direction = left;
			}
			gottenSprite1->setPosition(it->x, it->y);
			inWindow.draw(*gottenSprite1);
			//inWindow.display();
		}
		else if (direction == left)
		{
			it->x = it->x - changeX;
			if (it->x <= it->platformLeftEdge)
			{
				it->x = it->platformLeftEdge;
				direction = right;
				///////inWindow.setPosition(it->x, it->y);
			}
			gottenSprite2->setPosition(it->x, it->y);
			//screenwidth
			if (it->x >= 0 && it->x <= 800 - 76)
			{
				inWindow.draw(*gottenSprite2);
				//inWindow.display();
			}
		}


		
	}
	

	return(1);
}

int Enemy::changePlatformEdgesForEnemy(sf::RenderWindow& inWindow, int changeX)
{
	for (std::vector<EnemyOnPlatform>::iterator it = enemyVector2.begin(); it != enemyVector2.end(); ++it)
	{
		//if (it->platformLeftEdge >= -150 && it->platformLeftEdge <= 800)
		{

			it->platformLeftEdge = it->platformLeftEdge + changeX;
			
		}
		//150 is the width of platforrm, 800 is screenwidth
		//if (it->platformRightEdge >= 0 && it->platformRightEdge < 800 + 150)
		{
			it->platformRightEdge = it->platformRightEdge + changeX;
			
		}
	}

	return(1);
}

//loop through platforms and move them horizontally
int platform::SearchVectorForPlatforms( std::vector<platform>& vector,sf::RenderWindow& inWindow, float changeX, float changeY, int isLeft1)
{

	sf::Sprite * gottenSprite;// = NULL;


	for (std::vector<platform>::iterator it = vector.begin(); it != vector.end(); ++it)
	{
		//sprite is platform
		gottenSprite = it->getSprite();

		it->x = it->x + changeX;

		//platformLeftEdge = platFormLeftEdge + changeX;
		//platformRightEdge = platformRightEdge + changeX;

		gottenSprite->setPosition((it->x), it->y);

		if (it->x > -300 && it->x < (800))
		{

			inWindow.draw(*it->platformSprite);
		}

	}
	//	//character faces left
	//	if (isLeft1 == 1)
	//	{
	//		it->x = it->x - changeX;

	//		gottenSprite->setPosition((it->x), it->y);
	//	}

	//	if (isLeft1 == 0)
	//	{
	//		it->x = it->x + changeX;
	//		gottenSprite->setPosition((it->x), it->y);
	//	}

	//	if (it->x > -150 && it->x < (800))
	//	{

	//		inWindow.draw(*it->platformSprite);
	//	}
	//}
	//Sleep(10);
	
	return(1);
}

int Misc::moveEnemies(sf::RenderWindow& inWindow, float changeX, float changeY, int isLeft1)
{
	//return(1);
	//window.clear();
	for (std::vector<Misc>::iterator it = enemyVector.begin(); it != enemyVector.end(); ++it)
	{
		//
		gottenSprite = it->getSprite();

		//character faces left
		/*if (isLeft1 == 1)
		{
			it->x = it->x + changeX;

			gottenSprite->setPosition((it->x), 524);
		}*/

		//if (isLeft1 == 0)
		//{
		it->x = it->x - changeX;
		gottenSprite->setPosition((it->x), 524);
		//}

		if (it->x > -76 && it->x <= (800))
		{
			//inWindow.draw(*it->enemySprite);
			inWindow.draw(*gottenSprite);
		}
		if (it->x < -76)
		{
			it->x = 1600;
		}
	}
	//inWindow.display();

	//Sleep(10);

	return(1);
}

Advertisement

Sorry about the comments, thank goodness they appear dull.

You will have to apply the same offsets to all objects as you have applied to the stationary platforms.

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

I'm not understanding. Everything is 10 or -10 except moveEnemies that is changed to show motion. Does anyone else have an explanation, please?

Josheir

Josheir said:
Everything is 10 or -10 except moveEnemies

fleabay said:
You will have to apply the same offsets to all objects

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

This goddamn forum editing is for shit…

Sorry 'bout that…

You're going to have to add 10/-10 to the enemies as well, in addition to the other calculations.

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

@Josheir Did you ever get this working? I actually downloaded the github repo and had it running, as is.

I don't think that you believe my solution is optimal or is even correct. If you don't have it working by now, I can try and get the enemy movement working correctly.

Also, I couldn't get it running in Debug, only Release. I only tried x64. If I remember correctly, in Debug it hit an exception on SFML window creation.

I couldn't find the jump key. Did you remove jumping?

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

Oh, fleabay, I have had much trouble trying to long on to this site. There seems to be real problems that keep surfacing. I would really appreciate if you could get the movement working. Its not that I think you are incorrect, it is just that I desire it to work at the apparent speed. I think the jump key was disabled when I was working on decoupling. I don't know why it wouldn't work in debug, probably something is different on your end, I guess. There are different libraries for release and debug on the SFML site. If you can fix it I would have some great experience totally! I have been meaning to get the jump working, when I do, I'll notify you.

This topic is closed to new replies.

Advertisement