-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.cpp
66 lines (54 loc) · 1.36 KB
/
Animation.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Animation.h"
namespace dxco {
Animation::Animation(std::vector<cocos2d::CCTexture2D*> textures, float frameTime, bool repeat) {
this->textures = textures;
this->useFrames = false;
this->frameTime = frameTime;
this->repeat = repeat;
this->dt = 0;
this->index = 0;
this->finished = false;
}
Animation::Animation(std::vector<cocos2d::CCSpriteFrame*> spriteFrames, float frameTime, bool repeat) {
this->spriteFrames = spriteFrames;
this->useFrames = true;
this->frameTime = frameTime;
this->repeat = repeat;
this->dt = 0;
this->index = 0;
this->finished = false;
}
void Animation::restart() {
this->finished = false;
this->index = 0;
this->dt = 0;
}
void Animation::update(cocos2d::CCSprite* sprite, float dt) {
this->dt += dt;
if (this->dt > this->frameTime) {
this->dt = 0;
this->index++;
}
int size;
if (this->useFrames){
size = this->spriteFrames.size();
} else {
size = this->textures.size();
}
if (this->index >= size) {
if (this->repeat) {
this->index = 0;
} else {
this->finished = true;
this->index = size - 1;
}
}
if (this->useFrames){
cocos2d::CCSpriteFrame* frame = (cocos2d::CCSpriteFrame*) this->spriteFrames[this->index];
sprite->setDisplayFrame(frame);
} else {
cocos2d::CCTexture2D* texture = (cocos2d::CCTexture2D*) this->textures[this->index];
sprite->setTexture(texture);
}
}
} /* namespace dxco */