在Qt中设置QGraphicsObject/QGraphicsItem的动画
animating a QGraphicsObject/QGraphicsItem in Qt
情况:上层dialog
生成具有正方形的光栅。玩家坐在(0,0(处,当用户点击正方形时,路径查找算法pathFind
会生成玩家达到该目标所需方向的QString
。然后将CCD_ 4转换为CCD_。
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);
};
编辑:
- 我在setGridPos中添加了一些qDebug,它们都会立即打印出来;这告诉我脚本没有阻塞动画,这可能是问题的一部分
第2版:
- 当我只调用movePlayer函数一次时,它似乎起作用
edit3:我有一种感觉,使用动画组可能会解决这个问题。
使用QSequentialAnimationGroup
。
从我第二次调用movePlayer开始,动画就停止工作了。我不得不重写dialog
的一些部分:现在不再逐个给出方向,而是通过向量传递。我希望我没有使用"新"添加任何内存泄漏,但我不认为有任何方法可以绕过它。
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;
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 将QGraphicsItem的移动区域限制在多边形区域内
- 使用 GLUT 使用键停止动画?
- 使用对象数组对 SFML 进行动画处理
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 如何检测是否在缓冲绘画动画中绘制最后一帧?
- 从精灵表在 sfml 中为精灵制作动画
- 如何更改 QGraphicsItem 对象的颜色?
- Qt3D 骨骼动画
- ASCII 旋转光标 (TUI) 动画出现问题
- 带有 std::cout 的多线程控制台文本动画
- 有没有办法像动画一样移动控制台窗口?
- 围绕特定轴对 qt3d 旋转进行动画处理
- 组合的QGraphicsItem未检测到子项上的单击
- 如何将QGraphicsItem QList保存到文件中,然后读取它?Qt c++
- 如何在Qt中设置围绕固定点的线的动画
- 试图用c++制作一个动画加载圈
- 无法在 qgraphicsItem 中对 QTransform 进行动画处理
- 在Qt中设置QGraphicsObject/QGraphicsItem的动画