我遇到了一个问题,我正在编写一个小游戏,并且几次添加新变量(如float或sf :: Vector2f)时,sfml不呈现其他元素或不移动(或其他奇怪的问题)。 / p>
这是我的主要功能:
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);
}
};
修改
我注意到,当我打电话时(在我的球员班上) player.setPosition(...); 播放器未呈现。当我不调用它时,它会被渲染。 另一方面,当我不声明RectangleShape时,移动并正确渲染播放器。
编辑2 在类CPlayer中是Move()方法,其中是setPosition()。
答案 0 :(得分:1)
当出现这种错误/故障时,游戏的行为会根据变量的数量或数组中的项目而发生变化,更可能是由于变量未正确初始化而导致的。
为所有类中的所有属性赋予所有构造函数中的值 。
在您的情况下,g,height和jumpVel未初始化。在构造函数中为它们提供一个默认值,并检查其他类是否在构造函数中初始化了它们的属性。
答案 1 :(得分:0)
您是否需要将PollEvent
更改为GetEvent
? sfml 1.6要求。
答案 2 :(得分:0)
不幸的是,当你有一个窗口上下文时,你必须在main()
中定义它所以
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "MY GAME WINDOW");
}
而不是
sf::RenderWindow window(sf::VideoMode(800, 600), "MY GAME WINDOW");
int main()
{
//window stuff...
}
如果你想要多线程,你将不得不查看线程文档,但你可以做
void Thread(sf::RenderWindow &window)
{
// Draw stuff...
}
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "MY GAME WINDOW");
sf::Thread exampleThread(Thread, window);
window.setActive(false);
while (window.isOpen()) { /* preferably do events here still
something I struggle with */ }
}
但是文档再次适用于sfml。