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);
}