我正在尝试实现跳跃机制,所以我有2种状态,跌落和跳跃。我这样编码,如果它没有跳下它的下落,反之,当玩家处于跳动选择时bool切换,而当jumpSpedDecrease> 0.299f时切换,在代码中你可以看到,如果if语句中有cout说“此代码应该起作用”。当我运行程序时,我可以在代码中看到这一点,但是在那里更改的值保持不变,但是显然应该更改。
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;`
这里的完整代码:
#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;
}
答案 0 :(得分:3)
重新考虑以下几行:
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;
这里您仅创建了一些局部变量(例如float jumpSpedDecrease = 0),因此没有修改类字段。例如,此处的fallingSpeed(类字段)设置为0:
if (isJumping)
{
fallingSpeed = 0;
falSpedIncrease = 0;
跳转结束时,不会设置回1。然后,您可以在falling()方法中使用此字段。我建议先删除浮点类型说明符(在if(jumpSpedDecrease> 0.299f){}语句内部)。