-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRunner.cpp
149 lines (121 loc) · 3.87 KB
/
Runner.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "Runner.h"
USING_NS_CC;
bool Runner::init(){
auto frameCache = SpriteFrameCache::getInstance();
frameCache->addSpriteFramesWithFile("hazefight.plist", "hazefight.png");
m_runner = Sprite::createWithSpriteFrameName("run0.png");
run_jumpSize = m_runner->getContentSize();
crouchSize = m_runner->getContentSize();
this->addChild(m_runner);
initActionSet();//初始化动作集合
initBody();
this->scheduleUpdate();
this->setTag(1);
return true;
}
void Runner::initActionSet(){
auto frameCache = SpriteFrameCache::getInstance();
frameCache->addSpriteFramesWithFile("hazefight.plist", "hazefight.png");
SpriteFrame* frame = NULL;
//3.0中改用vector 而不是用Array
Vector<SpriteFrame*>frameVector;
/* 1.----------------加载跑动的Animation-----------------*/
for (int i = 0; i < 6; i++) {
//从缓存池中加载精灵到Vector
//frame = SpriteFrame::create(StringUtils::format("ani_blow/runner%d.png", i), Rect(0, 0, 500, 500));
frame = frameCache->spriteFrameByName(String::createWithFormat("run%d.png", i)->getCString());
frameVector.pushBack(frame);
}
//用vector里面的SpriteFrame列表创建Animation 以及设置一些参数
auto run_animation = Animation::createWithSpriteFrames(frameVector, 0.1f, -1);
//将跑动的 Animation 取名为 running
AnimationCache::getInstance()->addAnimation(run_animation, "running");
/*4---------------*/
frameVector.clear();
for (int i = 0; i <= 3; i++){
//frame = SpriteFrame::create(StringUtils::format("ani_roll/runnerJumpUp%d.png", i), Rect(0, 0, 500, 500));
frame = frameCache->spriteFrameByName(String::createWithFormat("up%d.png", i)->getCString());
frameVector.pushBack(frame);
}
auto jumpUp_animation = Animation::createWithSpriteFrames(frameVector, 0.2);//不设置无限循环
AnimationCache::getInstance()->addAnimation(jumpUp_animation, "jumpUp");
frameVector.clear();
for (int i = 0; i <= 1; i++){
//frame = SpriteFrame::create(StringUtils::format("ani_roll/runnerJumpDown%d.png", i),Rect(0,0,500,500));
frame = frameCache->spriteFrameByName(String::createWithFormat("down%d.png", i)->getCString());
frameVector.pushBack(frame);
}
auto jumpDown_animation = Animation::createWithSpriteFrames(frameVector, 0.3);
AnimationCache::getInstance()->addAnimation(jumpDown_animation, "jumpDown");
}
void Runner::doAction(const char* actionName){
auto animation = AnimationCache::getInstance()->animationByName(actionName);
auto action = Animate::create(animation);
m_runner->runAction(action);
}
void Runner::initBody(){
//根据不同状态设置不同刚体大小
Size bodySize;
if (m_state == crouch){
bodySize = crouchSize;
}
else{
bodySize = run_jumpSize;
}
//创建runner的刚体
auto runerBody = PhysicsBody::createBox(bodySize, PHYSICSBODY_MATERIAL_DEFAULT);
//解决小人翻倒的问题
runerBody->setRotationEnable(false);
runerBody->getShape(0)->setRestitution(0);
//设置可以碰撞检测
runerBody->setCategoryBitmask(1);
runerBody->setCollisionBitmask(0x02);
//runerBody->setCollisionBitmask(1);
runerBody->setContactTestBitmask(1);
//绑定刚体
this->setPhysicsBody(runerBody);
}
void Runner::Run(){
m_state = running;
initBody();
doAction("running");
}
/**/
void Runner::Jump(){
//只有在跑动时才能起跳
if (m_state == running){
m_state = jumpUp;
auto mass = this->getPhysicsBody()->getMass() * 450;
this->getPhysicsBody()->applyImpulse(Vect(0, mass));
m_runner->stopAllActions();
doAction("jumpUp");
}
}
void Runner::update(float dt){
auto vel = this->getPhysicsBody()->getVelocity();
if (m_state == jumpUp){
if (vel.y < 0.1){
m_state = jumpDown;
m_runner->stopAllActions();
doAction("jumpDown");
}
}
if (m_state == jumpDown){
CCLOG("%f", vel.y);
//不应该是 等于 0
if (vel.y > 0){
m_state = running;
m_runner->stopAllActions();
doAction("running");
}
}
}
void Runner::Crouch(){
//只能在跑动的时候蹲下
if (m_state == running){
m_state = crouch;
m_runner->stopAllActions();
initBody();
doAction("crouch");
}
}