-
Notifications
You must be signed in to change notification settings - Fork 0
/
caranim.cpp
48 lines (44 loc) · 962 Bytes
/
caranim.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "caranim.h"
CarAnim::CarAnim(QObject *parent)
: QObject{parent}, m_moveRight(false), m_moveLeft(false)
{
m_carPixmap.load(":/images/car.png");
m_currentFrame = 4;
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &CarAnim::updateFrame);
m_timer->start(50);
}
QPixmap CarAnim::returnPixmap()
{
return m_carPixmap.copy(720*m_currentFrame,0,720,368);
}
void CarAnim::updateFrame()
{
if(m_moveRight && m_currentFrame <= 8)
{
m_currentFrame++;
if(m_currentFrame > 8)
{
m_currentFrame = 8;
}
}
else if(m_moveLeft && m_currentFrame >= 0)
{
m_currentFrame--;
if(m_currentFrame < 0)
{
m_currentFrame = 0;
}
}
else
{
if(m_currentFrame > 4)
{
m_currentFrame--;
}
else if(m_currentFrame < 4)
{
m_currentFrame++;
}
}
}