簡體   English   中英

在Qt中為QGraphicsObject / QGraphicsItem設置動畫

[英]animating a QGraphicsObject/QGraphicsItem in Qt

情況 :上層dialog生成帶有正方形的柵格。 玩家坐在(0,0)處,並且當用戶單擊一個正方形時,尋路算法pathFind生成一個QString方向,指示玩家必須達到該方向。 然后將QString轉換為int數組。

dialogmysquare類調用了一個名為movePlayer的函數,該函數應將播放器向正確的方向移動。

此運動不應該瞬時發生,而是一定速度,這就是為什么我要為每個運動設置動畫,所以需要一定的時間。

但是我的問題是玩家廣場根本沒有動。 有時,當我單擊網格外部的某個位置時,它會捕捉到結束位置。 奇怪的。

MySquare類(播放器)

 MySquare::MySquare(int x,int y, int w, int h){
        curX = x;
        curY = y;

    initX = x;
    initY = y;
    width = w;
    height = h;
    posAnimation.setPropertyName("getGridPos");
    posAnimation.setTargetObject(this);
}


bool MySquare::movePlayer(int direction){
    switch (direction){
    case 0: //move right
        curX+=width;
        break;
    case 1: //move up
        curY+=height;
        break;
    case 2: //move left
        curX+=-width;
        break;
    case 3: //move down
        curY+=-height;
        break;
    }
    //setPos(curX,curY);
    QPoint p;
    p.setX(curX);
    p.setY(curY);

    setGridPos(p);
    update();
    return true;
}

void MySquare::setGridPos(QPoint myPos) {
    posAnimation.setStartValue(pos());
    posAnimation.setDuration(2000); // 2 seconds
    posAnimation.setEndValue(myPos);
    posAnimation.start();
}


QPoint MySquare::getGridPos() const {
    return pos();
}

MySquare標頭(播放器)

 class MySquare : public QGraphicsObject
    {
        Q_OBJECT
        Q_PROPERTY(QPoint getGridPos READ getGridPos WRITE setGridPos) // define meta-property "pos"s
    public:
    QPropertyAnimation posAnimation;
    MySquare(int x,int y,int h, int w);
    QRectF boundingRect() const; //outer most edges of the object
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    bool movePlayer(int direction);
public slots:
    QPoint getGridPos() const; // getter
    void setGridPos(QPoint p); // setter

signals:
    void targetChanged(int x, int y);

private:
    int curX,curY,height,width,initX,initY;
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
};

編輯:

  • 我在setGridPos中添加了一些qDebug,這些都可以立即打印出來。 這告訴我腳本沒有阻止動畫,這可能是問題的一部分。

編輯2:

  • 當我只調用一次movePlayer函數時,它似乎可以工作;

edit3:我覺得可以使用動畫組來解決。

使用QSequentialAnimationGroup

從我第二次調用movePlayer開始,動畫就停止了工作。 我不得不重寫dialog某些部分:現在不再逐一給出方向,而是通過矢量傳遞。 我希望我沒有使用“ new”添加任何內存泄漏,但我認為沒有任何解決方法。

 void MySquare::movePlayer(std::vector <int> directions){
        QSequentialAnimationGroup *group = new QSequentialAnimationGroup;
        //determine direction
        //& add each element to the group
        QPoint startPosition;
        startPosition.setX(pos().x());
        startPosition.setY(pos().y());

        for (int i=0; i<directions.size(); i++){
            int direction = directions[i];
            switch (direction){
            case 0: //move right
                curX+=width;
                break;
            case 1: //move up
                curY+=height;
                break;
            case 2: //move left
                curX+=-width;
                break;
            case 3: //move down
                curY+=-height;
                break;
            }

            QPoint endPosition;
            endPosition.setX(curX);
            endPosition.setY(curY);

            QPropertyAnimation *animation = new QPropertyAnimation(this,"getGridPos");
            animation->setStartValue(startPosition);
            animation->setDuration(1000); // 1 seconds
            animation->setEndValue(endPosition);
            group->addAnimation(animation); ////add animatons to a QSequentialAnimationGroup
            //delete animation;

            //update startPosition for next loop
            startPosition.setX(endPosition.x());
            startPosition.setY(endPosition.y());
        }
        qDebug() << "start animation group" << endl;
        group->start();
        update();
        //delete group;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM