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?



  • I think you need to set your start frame to 0 not 1.

    timeLine->setFrameRange(0, 4);
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.