在QGraphicsScene中更改QMovie对象以响应KeyEvent

时间:2018-03-01 17:34:11

标签: qt qgraphicsscene

我在互联网上搜索了一段时间但找不到合适的答案。有没有人知道如何在QGraphicsScene中的QMovie对象中更改电影以响应keyEvent?我尝试使用setMovie并尝试在更改电影之前停止电影,然后再次启动电影,但它确实没有用,电影项目在关键事件后消失

WebResourceRequest

// QGraphicsScene Snippet

#include <QKeyEvent>
#include <QGraphicsScene>
#include <QLabel>
#include "player.h"
#include "weapons.h"

Player::Player(qreal playerSize, QGraphicsScene *gameScene){

    //transparent background

    this->QLabel::setAttribute(Qt::WA_TranslucentBackground);

    //Draw the player

    this->setGeometry((gameScene->width()-playerSize)/2,(gameScene->height()-playerSize)/2,playerSize,playerSize);

    setCurrentAnimation(IDLE_LEFT);
    this->movie()->setScaledSize(this->size());
    this->movie()->start();
}

//set Current animation

void Player::setCurrentAnimation(int animationCode)
{
    curr_Animation_Code = animationCode;
    switch(animationCode)
    {
        case IDLE_LEFT  : this->setMovie(new QMovie(":/player/Laura/idle_left.gif"));break;
        case IDLE_RIGHT : this->setMovie(new QMovie(":/player/Laura/idle_right.gif"));break;
        case RUN_LEFT   : this->setMovie(new QMovie(":/player/Laura/run_left.gif"));break;
        case RUN_RIGHT  : this->setMovie(new QMovie(":/player/Laura/run_right.gif"));break;
    }
}

void Player::keyPressEvent(QKeyEvent *event){
    if (event->key() == Qt::Key_Left){
        this->setGeometry(x()-10,y(),this->width(),this->height());
        if(curr_Animation_Code != RUN_LEFT)
        {
            this->movie()->stop();
            setCurrentAnimation(RUN_LEFT);
            this->movie()->start();
        }
    }
    else if (event->key() == Qt::Key_Right){
        this->setGeometry(x()+10,y(),this->width(),this->height());
        if(curr_Animation_Code != RUN_RIGHT)
        {
            this->movie()->stop();
            setCurrentAnimation(RUN_RIGHT);
            this->movie()->start();
        }
    }
}

0 个答案:

没有答案