1

我正在用 qt creator 开发一个射击游戏,我的问题是与keyPressEvent我的场景功能的链接:

void Scene::keyPressEvent(QKeyEvent *event){
    liste_event << event->key();

    if (liste_event.contains(Qt::Key_Left)) {
        vaisseau->MoveX(-1);
    }

    if (liste_event.contains(Qt::Key_Right)) {
        vaisseau->MoveX(1);
    }
}

它可以编译,但我的精灵(vasisseau)移动得很慢。如何改进代码以使其运行得更快?

4

1 回答 1

0

Well, the problem is that you are leaving the animation of the game to the keyPressEvent, which is not triggered as often as you would like.

To solve this problem I suggest you use the traditional approach, which involves having an function to draw() the scene (and it's objects). The idea is that this function is called every X miliseconds, and the drawing will work independently of a key being pressed or not.

So in this case, inside keyPressEvent you would just store the key that was pressed, and in Scene::draw() you will implement the logic that will call MoveX() with the appropriate parameter, based on the stored key.

于 2012-06-20T16:45:16.510 回答