首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按键时“卡顿”运动

按键时“卡顿”运动
EN

Stack Overflow用户
提问于 2013-05-28 02:18:22
回答 1查看 687关注 0票数 1

第一次学习SFML并用C++做游戏。我的问题来自于角色的移动。我正在做一个类似星体的克隆,当按键时,移动不是很流畅。当同时按下旋转和向前移动时,角色会出现卡顿,并停止。有什么帮助吗?

Player.cpp

代码语言:javascript
复制
#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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-28 03:20:05

我认为这是由于时间管理,如果它是一个小的2D你可能有一个很高的FPS率。

然后你的move事件会被多次调用,并造成这个卡顿。

您应该限制您的帧率,并尝试添加时钟到您的事件,如果限制帧率还不够。

您可以在this page of the doc中找到您需要的内容

如果根本不是这样,给我们展示一下你的主循环,也许你有一些东西需要大量的资源。

希望能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16778563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档