![](/img/trans.png)
[英]Deleting a QGraphicsItem/QGraphicsObject from QGraphicsScene?
[英]animating a QGraphicsObject/QGraphicsItem in Qt
情況 :上層dialog
生成帶有正方形的柵格。 玩家坐在(0,0)處,並且當用戶單擊一個正方形時,尋路算法pathFind
生成一個QString
方向,指示玩家必須達到該方向。 然后將QString
轉換為int
數組。
dialog
從mysquare
類調用了一個名為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);
};
編輯:
編輯2:
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.