ovg
ovg

Reputation: 1546

Qt QTimeLine skips the first frame

I am making a player move across the screen using QTimeLine to handle the animation/movement in the specified time.

In my player constructor:

timeLine = new QTimeLine(500);
timeLine->setFrameRange(1, 4);
timeLine->setCurveShape(QTimeLine::CurveShape::LinearCurve);
QObject::connect(timeLine, SIGNAL(frameChanged(int)), this, SLOT(animatedMove(int)));

I removed the animation stuff to just show the problem:

void PlayerSprite::animatedMove(int frame)
{
    qDebug() << "Frame: " << frame;
}

The timer is started in a slot which is called on key press:

void PlayerSprite::move(Direction direction)
{
    timeLine->start();
}

The output is:

GameView: Player is moving
Frame:  2
Frame:  3
Frame:  4
GameView: Player is moving
Frame:  1
Frame:  2
Frame:  3
Frame:  4

But it should be:

GameView: Player is moving
Frame:  1
Frame:  2
Frame:  3
Frame:  4
GameView: Player is moving
Frame:  1
Frame:  2
Frame:  3
Frame:  4

During each frame the player moves e.g. 2 steps. Therefore should move 4x2=8 steps... but on the first frame it only moves 6 steps meaning my character falls out of the game grid. I'm making an small 16 bit rpg ;)

Is this because it is assumed that my player is already in it's first frame? if so can this be overridden?

Upvotes: 0

Views: 395

Answers (1)

RA.
RA.

Reputation: 7777

This appears to be a known bug:

https://bugreports.qt.io/browse/QTBUG-41610

The easiest work-around in your case is to set the current time of the time line to the duration on creation:

timeLine = new QTimeLine(500);
timeLine->setFrameRange(1, 4);
timeLine->setCurveShape(QTimeLine::CurveShape::LinearCurve);
// Add this line:
timeLine->setCurrentTime(timeLine->duration());

Upvotes: 1

Related Questions