![](/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.