I am having problems with some SFML code. When the program runs without interaction the code runs fine and the enemy on the platform moves left and right as needed. However, when the S key is pressed and held (to move player right) both enemy types freeze when the character is in the middle section.
The S key press updates the movement and sets a string flag to "right" and changes the code run here in main:
if (isLeft == 0)
{
//continual running
window.clear();
//one change of movement from ongoing non key press
character.EnemyObject->moveEnemies(window, 1, -1, 0);
miscObject.moveEnemies(window, 10, 0, 1);
window.draw(marioSpriteL);
if (character.gPlatformsMoved == "right")
{
character.EnemyObject->changePlatformEdgesForEnemy(window, -10);
platform::SearchVectorForPlatforms(vectorPlatforms, window, -10, 0.0f, 0);
character.gPlatformsMoved = "none";
}
window.display();
Here are the two functions that are called differently when there is a right movement flag:
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;
gottenSprite->setPosition((it->x), it->y);
if (it->x > -150 && it->x < (800))
{
inWindow.draw(*it->platformSprite);
}
}
return(1);
}
If more code needs to be shown let me know.
Here is a quick video that shows the problem:
Thanks!
Josh