SFML community forums

Help => General => Topic started by: Josheir on May 12, 2020, 07:32:58 pm

Title: Key press freezes enemys' movement
Post by: Josheir on May 12, 2020, 07:32:58 pm
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:  https://youtu.be/SCnwJMITIzw  (https://youtu.be/SCnwJMITIzw)

Thanks!

Josh
Title: Re: Key press freezes enemys' movement
Post by: Josheir on May 12, 2020, 09:17:16 pm
Oh, also the player's character moves slower when in the mid section.  May I please have help with this too please?   8)

Josh
Title: Re: Key press freezes enemys' movement
Post by: Hapax on May 12, 2020, 09:39:29 pm
The only thing I see happening in the video is that when the screen "scrolls", the enemies "seem" to stop moving.

I put quotes around "scroll" because to do the scroll, I guess you're actually moving the platforms to the left?
And, I put "seem" in quotes because if the platforms are moving left and the enemies are moving to the left at the same speed, they will "look" stationary.
Remember that when platforms (or anything really) move left, anything supposedly attached to them should also be moved by the same amount.
Title: Re: Key press freezes enemys' movement
Post by: Josheir on May 12, 2020, 10:06:57 pm
I hope I have this right.  The enemy is on the platform and therefore moves with it.  However, because the enemy is also moving left and right he shouldn't stay put on the platform. He should be moving back and forth there too.

Josh
Title: Re: Key press freezes enemys' movement
Post by: Hapax on May 13, 2020, 12:35:11 am
Yes, that's my point.

If an platform moves left, the enemy on it should be moved left by the platform - just to stay still! If the enemy moves left in relation to the platform, it should be moved left "again".

Think about it the other way around. If the platform is moving left, it should move the enemy to the left, but if the enemy is then moving right, it should be moved to the right. This should result in the enemy not moving at all while the platform moves left under it*.
*if we assume that the amount the platform moves is the same as the amount the enemy moves.
Title: Re: Key press freezes enemys' movement
Post by: Josheir on May 13, 2020, 03:12:11 pm
Well, I guess technically this is right.  I have all the characters moving at the same speed now which I want.  I am considering if the "feel" would be better if the characters continued there motion instead of "freezing".  Any idea how this is done?

Josh