简体   繁体   中英

SFML. Code is not executed but it 100% should

Im trying to implement jumping mechanics so i have 2 states, falling and jumping. I code it this way that if its not jumping its falling and the other way around, bool switches when player is at the pick of jump and thats when jumpSpedDecrease > 0.299f, in code there u can see that in that if statement theres cout saying "This code should work". When i run program i can see this in code but the values i change there stay same way, but it obviously should change.

if (isJumping)
    {
        fallingSpeed = 0;
        falSpedIncrease = 0;
        birdSprite.setPosition(pos.x, pos.y - (jumpStartSped - jumpSpedDecrease) * time.asMilliseconds() * jumpingSpeed);
        jumpSpedDecrease += jumpSpedDcrsValue;
        std::cout << " jumpStartSped: " << jumpStartSped << " jumpSpedDecrease: " << jumpSpedDecrease << " jumpSpedDcrsValue: " << jumpSpedDcrsValue << " fallingSpeed: " << fallingSpeed << std::endl;
        if (jumpSpedDecrease > 0.299f) 
        {
            float fallingSpeed = 1;
            float startSped = 0.004f;
            float falSpedIcrsValue = 0.001f;
            float falSpedIncrease = 0;
            float jumpingSpeed = 1;
            float jumpStartSped = 0.3f;
            float jumpSpedDcrsValue = 0.001f;
            float jumpSpedDecrease = 0; 
            std::cout << "This code should work" << std::endl;
        }
        if (jumpSpedDecrease > 0.3f) isJumping = false;`

Whole code here:

#include <SFML\Graphics.hpp>
#include <iostream>

using namespace sf;



class Player
{
public:
float fallingSpeed = 1; 
float startSped = 0.004f;
float falSpedIcrsValue = 0.001f;
float falSpedIncrease = 0;
float jumpingSpeed = 1;
float jumpStartSped = 0.3f;
float jumpSpedDcrsValue = 0.001f;
float jumpSpedDecrease = 0;
bool isJumping = false;
Vector2u windowSize;
Time time;
Texture birdTex;
Sprite birdSprite;
void loadingSprite() 
{
    if (!birdTex.loadFromFile("./../Textures/flappyBird.png"))
    {
        std::cerr << "Cannot load texture" << std::endl;
    }
    birdSprite.setTexture(birdTex);
    Vector2u size = birdTex.getSize();
    birdSprite.setOrigin(size.x/2, size.y/2);
    birdSprite.setScale(0.1f, 0.1f);
    birdSprite.setPosition(windowSize.x/2, windowSize.y/2);
}
void falling()
{
    if (!isJumping)
    {
        Vector2f pos = birdSprite.getPosition();
        birdSprite.setPosition(pos.x, pos.y + (startSped + 
falSpedIncrease) * time.asMilliseconds() * fallingSpeed);
        falSpedIncrease += falSpedIcrsValue;
    }
}
void jump()
{
    Vector2f pos = birdSprite.getPosition();

    if (Keyboard::isKeyPressed(Keyboard::Space)) isJumping = true;

    if (isJumping)
    {
        fallingSpeed = 0;
        falSpedIncrease = 0;
        birdSprite.setPosition(pos.x, pos.y - (jumpStartSped - 
jumpSpedDecrease) * time.asMilliseconds() * jumpingSpeed);
        jumpSpedDecrease += jumpSpedDcrsValue;
        std::cout << " jumpStartSped: " << jumpStartSped << " 
jumpSpedDecrease: " << jumpSpedDecrease << " jumpSpedDcrsValue: " << 
jumpSpedDcrsValue << " fallingSpeed: " << fallingSpeed << std::endl;
        if (jumpSpedDecrease > 0.299f) 
        {
            float fallingSpeed = 1;
            float startSped = 0.004f;
            float falSpedIcrsValue = 0.001f;
            float falSpedIncrease = 0;
            float jumpingSpeed = 1;
            float jumpStartSped = 0.3f;
            float jumpSpedDcrsValue = 0.001f;
            float jumpSpedDecrease = 0; 
            std::cout << "This code should work" << std::endl;
        }
        if (jumpSpedDecrease > 0.3f) isJumping = false;
    }

}
};

int main()
{
Clock clock;
RenderWindow window(VideoMode(600, 600), "Dupako");
window.setFramerateLimit(240);
Player player;
player.windowSize = window.getSize();
player.loadingSprite();

// MAIN LOOP //
while (window.isOpen())
{
    Event event;
    while (window.pollEvent(event)) {
        if (event.type == Event::Closed) window.close();
    }


    Time time = clock.getElapsedTime();
    // MOVEMENT FUNCTIONS START //


    player.time = time;
    player.falling();
    player.jump();


    // MOVEMENT FUNCTIONS END //
    clock.restart().asMilliseconds();

    window.clear();
    /// DRAW ///


    window.draw(player.birdSprite);



    /// DRAW END ///
    window.display();

    // MAIN LOOP END //
}

return 0;
}

Reconsider these lines:

if (jumpSpedDecrease > 0.299f) 
{
    float fallingSpeed = 1;
    float startSped = 0.004f;
    float falSpedIcrsValue = 0.001f;
    float falSpedIncrease = 0;
    float jumpingSpeed = 1;
    float jumpStartSped = 0.3f;
    float jumpSpedDcrsValue = 0.001f;
    float jumpSpedDecrease = 0; 
    std::cout << "This code should work" << std::endl;
}
if (jumpSpedDecrease > 0.3f) isJumping = false;

Here you just create some local variables (eg float jumpSpedDecrease = 0), thereby not modifying the class fields. For example, the fallingSpeed (class field) set to 0 here:

if (isJumping)
{
    fallingSpeed = 0;
    falSpedIncrease = 0;

is not set back to 1, when the jump ends. And you use this field in the falling() method. I suggest removing float type specifiers first (inside of if (jumpSpedDecrease > 0.299f) {} statement).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM