第一次学习SFML并用C++做游戏。我的问题来自于角色的移动。我正在做一个类似星体的克隆,当按键时,移动不是很流畅。当同时按下旋转和向前移动时,角色会出现卡顿,并停止。有什么帮助吗?
Player.cpp
#include "Player.h"
#include "Bullet.h"
#include <iostream>
#include <valarray>
#define SPEED 10
#define ROTATION 15
Player::Player()
{
this->_x = 150;
this->_y = 150;
this->_xspeed = 0;
this->_yspeed = 0;
this->_rotation = ROTATION;
this->_user = this->loadSprite("/Users/ganderzz/Desktop/Programming/C_Plus/stest/stest/Resources/Player.png");
this->_user.setOrigin(16, 16);
}
void Player::Collision(RenderWindow & in)
{
if(this->_x >= (in.getSize().x-32) || this->_x <= 0)
this->_xspeed = 0;
}
void Player::Move(Event & e)
{
if(Keyboard::isKeyPressed(Keyboard::D))
{
this->_user.rotate(this->_rotation);
}
if(Keyboard::isKeyPressed(Keyboard::A))
{
this->_user.rotate(-this->_rotation);
}
if(Keyboard::isKeyPressed(Keyboard::W))
{
this->_yspeed = -sinf((90 + this->_user.getRotation()) * 3.14 / 180) * SPEED;
this->_xspeed = -cosf((90 + this->_user.getRotation()) * 3.14 / 180) * SPEED;
this->_x += this->_xspeed;
this->_y += this->_yspeed;
}
if(Keyboard::isKeyPressed(Keyboard::Space))
{
Bullet b(this->_x,this->_y,this->_user.getRotation());
}
}
void Player::Draw(RenderWindow & in)
{
this->_user.setPosition(this->_x, this->_y);
in.draw(this->_user);
}
Sprite Player::loadSprite(std::string filename)
{
this->_texture.loadFromFile(filename, IntRect(0,0,32,32));
return Sprite(this->_texture);
}发布于 2013-05-28 03:20:05
我认为这是由于时间管理,如果它是一个小的2D你可能有一个很高的FPS率。
然后你的move事件会被多次调用,并造成这个卡顿。
您应该限制您的帧率,并尝试添加时钟到您的事件,如果限制帧率还不够。
您可以在this page of the doc中找到您需要的内容
如果根本不是这样,给我们展示一下你的主循环,也许你有一些东西需要大量的资源。
希望能有所帮助。
https://stackoverflow.com/questions/16778563
复制相似问题