gpt4 book ai didi

c++ - SFML C++ 不渲染

转载 作者:行者123 更新时间:2023-11-28 06:27:01 27 4
gpt4 key购买 nike

我遇到了一个问题,我正在写一个小游戏,有几次当我添加新变量如 float 或 sf::Vector2f 时,sfml 没有渲染其他元素或不移动(或其他奇怪的问题)。

这是我的主要功能:

const int resW=800, resH=600;
sf::RenderWindow app(sf::VideoMode(resW, resH), "Jump");//, sf::Style::Fullscreen);

int main()
{
//FPS
app.setFramerateLimit(60);
sf::Font font;
float jumpStrength = 0.0;

//when i uncomment it, my player is not rendered.
//When it's commented everything is rendered
//some time ago I had declared float number in player class,
//and then player couldn't move (problem with sfml setPosition()???
//sf::RectangleShape strengthRect(sf::Vector2f(100,100));
while (app.isOpen())
{
// Process events
sf::Event event;
while (app.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed ||
sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
app.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
jumpStrength+=0.2;
}
else if(jumpStrength>0.0)
{
player.Jump(jumpStrength);
}
}

// Clear screen
app.clear();

ChandleInput();
ChandleCollisions();

player.Update();
player.Draw(&app);
app.draw(strengthRect);
}
}

class CPlayer
{
private:

sf::Texture playerTex[3];
sf::Sprite player;

float g;
float height;
float jumpVel;

void ResetJump() {jumpVel = 11;
g = 0.2;
height = 0;}
void Move(float x, float y)
{
sf::Vector2f vec = player.getPosition();
vec.x += x;
vec.y += y;
player.setPosition(vec.x,vec.y);
}
public:
CPlayer()
{
if (!playerTex[0].loadFromFile("Images/frame_1.png"))
return;// EXIT_FAILURE;
if (!playerTex[1].loadFromFile("Images/frame_2.png"))
return;// EXIT_FAILURE;
if (!playerTex[2].loadFromFile("Images/frame_3.png"))
return;// EXIT_FAILURE;
player.setTexture(playerTex[0]);
int x,y;
int w,h;
w = player.getLocalBounds().width;
h = player.getLocalBounds().height;
x = resW/2;y=resH-h/2;
x-=w/2;
y-=h/2;
player.setPosition(x,y);
}
int GetX(){return player.getPosition().x;}
int GetY(){return player.getPosition().y;}
float GetHeight(){return height;}
void Update()
{
if(height>0)//skacze
{
jumpVel -= g;
height += jumpVel;
}
else
{
if(height<0)
{
height = 0;
}
if(jumped)
{
landed = true;
player.setTexture(playerTex[0]);
}
//Move(0,-1);
}
}
void Jump(float strength)
{
if(!jumped)
if(height<=0)//nie skacze
{
ResetJump();
height = jumpVel;
jumped = true;
player.setTexture(playerTex[1]);
jumpVel = strength;
}
}
void Draw(sf::RenderWindow *app)
{
Move(0.0,-height);
app->draw(player);
Move(0.0,height);

}
};

编辑

我注意到当我打电话时(在我的玩家类(class)) player.setPosition(...);播放器未呈现。当我不调用它时,它会被渲染。另一方面,当我不声明 RectangleShape 时,播放器会被正确移动和呈现。

编辑 2在类 CPlayer 中是 Move() 方法,其中是 setPosition()。

最佳答案

当遇到这种错误/故障时,游戏的行为会根据变量的数量或数组中的项目而改变,这很可能是由于变量未正确初始化引起的。

在它们的所有构造函数中为所有类中的所有属性赋值。

在你的例子中,g、height 和 jumpVel 没有被初始化。在构造函数中给它们一个默认值,并检查你的其他类是否在构造函数中初始化了它们的属性。

关于c++ - SFML C++ 不渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28347987/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com