c++ - (SFML)按下键时播放器构造函数未更新为正确的动画

标签 c++ animation constructor 2d sfml

当按下正确的对应键但只使用第 0 行动画时,我的 Sprite 向左、向右、向上和向下移动,但由于某些奇怪的原因,当我同时按下两个键时,例如 (W,A) 或 ( S, D) 是转移到它正在移动到哪一侧的相反动画。我尝试将指示动画更新的 if 语句移动到按键 if 语句中的嵌套 if 语句,但这没有任何好处,然后我尝试仅根据按键更新它而没有嵌套 if 语句,这也不起作用...我是 SFML 的新手,所以这真的让我很头疼,我也不介意批评,如果你看到我在 Sprite 运动方面可以做得更好的事情,请告诉我!谢谢你的帮助。
下面是我的播放器类构造函数

#include "Player.h"
    
    
    Player::Player(Texture* texture, Vector2u imageCount, float switchTime, float speed) :
        // initializer list from animation.cpp
        animation(texture, imageCount, switchTime) 
    {
        this->speed = speed;
        row = 0;
    
        body.setSize(Vector2f(100.0f, 150.0f));
        body.setTexture(texture);
        //sets initial position for test sprite sheet
        body.setPosition(550.0f, 900.0f);
    }
    
    Player::~Player()
    {
    }
    
    void Player::Update(float deltaTime)
    {
        Vector2f movement(0.0f, 0.0f);
    
        if (Keyboard::isKeyPressed(Keyboard::A))
        {
            //if A is pressed move to the left on the  x axis
            movement.x -= speed * deltaTime;
        }
        if (Keyboard::isKeyPressed(Keyboard::D))
        {
            //if D is pressed move to the right on the x axis
            movement.x += speed * deltaTime;
        }
        if (Keyboard::isKeyPressed(Keyboard::W))
        {
            // if W is pressed move up on the y axis
            movement.y += speed * deltaTime;
        }
        if (Keyboard::isKeyPressed(Keyboard::S))
        {
            // if S is pressed move down on the y axis
            movement.y -= speed * deltaTime;
        }
    
        if (movement.x == 0.0f || movement.y == 0.0f)//for idle animation
        {
            row = 0;//idle row for now just using walking until I get idle animation
        }
        else if(movement.x > 0.0f)
        {
            row = 1; //walking to the left animation
        }
        else if (movement.x < 0.0f )
        {
            row = 3; //walking to the right animation
        }
        else if (movement.y > 0.0f)
        {
            row = 0; // walking to stright animation
        }
        else if (movement.y < 0.0f)
        {
            row = 2;// walking back animation
        }
    
        animation.Update(row, deltaTime);
        body.setTextureRect(animation.uvRect);
        body.move(movement);
    
    }
    void Player::Draw(RenderWindow& window)
    {
        window.draw(body);
    }
下面是我的播放器类初始化
#pragma once
#include <SFML/Graphics.hpp>
#include "Animation.h"
using namespace std;
using namespace sf;

class Player
{
public:
    Player(Texture* texture, Vector2u imageCount, float switchTime, float speed);
    ~Player();

    void Update(float deltaTime);
    void Draw(RenderWindow& window);

private:
    RectangleShape body;
    Animation animation;
    unsigned int row;
    float speed;

};
在 Game while 循环和 Player 函数调用下方
    Player player(&playerTexture, Vector2u(9, 4), 0.09f, 100.0);

    //*************************************************************
    //clock & Time
    float deltaTime = 0.0f;

    Clock clock;



    while (window.isOpen())
    {

        deltaTime = clock.restart().asSeconds();

       //*********************************************************
       //Player Input

        if (Keyboard::isKeyPressed(Keyboard::Escape))
        {
            window.close();
        }



       //**********************************************************
       //draws everything

        player.Update(deltaTime);

        window.clear();
        window.draw(spritestartingBG);
        player.Draw(window);
        window.display();
    }

    return 0;
}

最佳答案

if (movement.x == 0.0f || movement.y == 0.0f)除非沿对角线移动,否则将是正确的——你可能想要 ||成为 &&。
同样,您的左/右动画是相反的——当 move.x 为 < 0.0,而不是 > 0.0 时,您正在向左移动。

关于c++ - (SFML)按下键时播放器构造函数未更新为正确的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62631745/

相关文章:

JavaFX 动画 - 性能下降

ios - UIRefreshControl 结束刷新不流畅

c++ - 为什么 const 成员必须在构造函数初始化器中而不是在其主体中初始化?

c# - 在 .Net 应用程序中调用 C++ Dll 时出现 System.Access 冲突异常

c++ - 将文件存储在 unsigned char 数组中并打印

android - Android 中用于转换的自定义动画对象?

c++ - 什么是构造函数继承?

c++ - 隐式生成的初始化列表构造函数

c++ - 保持动态分配数组而不用担心删除的最佳方法?

c++ - strace: SIGFPE 在任何事情发生之前