本文是依据Cocos2d-x 3.4制作休闲游戏《Stick Hero》系列教程文章所完成的
直接来代码:
AppDelegate.h
1 #ifndef _APP_DELEGATE_H_ 2 #define _APP_DELEGATE_H_ 3 4 #include "cocos2d.h" 5 6 /** 7 @brief The cocos2d Application. 8 9 The reason for implement as private inheritance is to hide some interface call by Director. 10 */ 11 class AppDelegate : private cocos2d::Application 12 { 13 public: 14 AppDelegate(); 15 virtual ~AppDelegate(); 16 17 /** 18 @brief Implement Director and Scene init code here. 19 @return true Initialize success, app continue. 20 @return false Initialize failed, app terminate. 21 */ 22 virtual bool applicationDidFinishLaunching(); 23 24 /** 25 @brief The function be called when the application enter background 26 @param the pointer of the application 27 */ 28 virtual void applicationDidEnterBackground(); 29 30 /** 31 @brief The function be called when the application enter foreground 32 @param the pointer of the application 33 */ 34 virtual void applicationWillEnterForeground(); 35 }; 36 37 #endif // _APP_DELEGATE_H_
AppDelegate.cpp
1 #include "AppDelegate.h" 2 #include "HelloWorldScene.h" 3 #include "WelcomeScene.h" 4 5 USING_NS_CC; 6 7 AppDelegate::AppDelegate() { 8 9 } 10 11 AppDelegate::~AppDelegate() 12 { 13 } 14 15 bool AppDelegate::applicationDidFinishLaunching() { 16 // initialize director 17 auto director = Director::getInstance(); 18 auto glview = director->getOpenGLView(); 19 if(!glview) { 20 glview = GLView::create("My Game"); 21 director->setOpenGLView(glview); 22 } 23 24 // 添加设计分辨率适配,参数,(设计分辨率的宽度,高度,分辨率适配的方式) 25 glview->setDesignResolutionSize(1080, 1980, ResolutionPolicy::FIXED_WIDTH); 26 27 // turn on display FPS 28 director->setDisplayStats(true); 29 30 // set FPS. the default value is 1.0/60 if you don‘t call this 31 director->setAnimationInterval(1.0 / 60); 32 33 // create a scene. it‘s an autorelease object 34 //auto scene = HelloWorld::createScene(); 35 auto scene = WelcomeScene::create(); 36 37 // run 38 director->runWithScene(scene); 39 40 return true; 41 } 42 43 // This function will be called when the app is inactive. When comes a phone call,it‘s be invoked too 44 void AppDelegate::applicationDidEnterBackground() { 45 Director::getInstance()->stopAnimation(); 46 47 // if you use SimpleAudioEngine, it must be pause 48 // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); 49 } 50 51 // this function will be called when the app is active again 52 void AppDelegate::applicationWillEnterForeground() { 53 Director::getInstance()->startAnimation(); 54 55 // if you use SimpleAudioEngine, it must resume here 56 // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); 57 }
Resource.h
1 #ifndef __Resource_H__ 2 #define __Resource_H__ 3 4 // 游戏欢迎界面 (WelcomScene) 时的背景图片 5 #define welcome_background_image0 "image/bg/bg1.jpg" 6 #define welcome_background_image1 "image/bg/bg2.jpg" 7 #define welcome_background_image2 "image/bg/bg3.jpg" 8 #define welcome_background_image3 "image/bg/bg4.jpg" 9 #define welcome_background_image4 "image/bg/bg5.jpg" 10 11 // 游戏欢迎界面 (WelcomeScene) 的标题图片 12 #define welcome_title_image "image/uires_1.png" 13 14 // 游戏欢迎界面 (WelcomeScene) 的开始菜单图片 15 #define welcome_start_image "image/uires_2.png" 16 17 // 游戏欢迎界面 (WelcomeScene) 的平台图片 18 #define welcome_floor_image "image/stage1.png" 19 20 // 游戏人物 Hero 的图片 21 #define hero1 "image/anim1/stay1.png" 22 23 // 棍子 stick 的图片 24 #define stick_image "image/stick1.png" 25 26 27 #endif __Resource_H__
WelcomeScene.h
1 #ifndef __Welcome_Scene_H__ 2 #define __Welcome_Scene_H__ 3 4 #include "cocos2d.h" 5 6 USING_NS_CC; 7 8 class WelcomeScene :public Scene{ 9 10 public: 11 12 WelcomeScene(); 13 ~WelcomeScene(); 14 15 virtual bool init(); 16 17 CREATE_FUNC(WelcomeScene); 18 19 20 }; 21 22 23 #endif __Welcome_Scene_H__
WelcomeScene.cpp
1 #include "WelcomeScene.h" 2 #include "BackgroundLayer.h" 3 #include "Resource.h" 4 5 WelcomeScene::WelcomeScene(){ 6 7 } 8 9 WelcomeScene::~WelcomeScene(){ 10 11 } 12 13 bool WelcomeScene::init(){ 14 15 if (!Scene::init()){ 16 return false; 17 } 18 19 Size visibleSize = Director::getInstance()->getVisibleSize(); 20 21 22 23 BackgroundLayer* _backgroundLayer = BackgroundLayer::create(); 24 this->addChild(_backgroundLayer, 1, 1); 25 26 return true; 27 }
BackgroundLayer.h
1 #ifndef __Background_Layer_H__ 2 #define __Background_Layer_H__ 3 4 #include "cocos2d.h" 5 #include "Resource.h" 6 #include "Player.h" 7 #include "GameOverLayer.h" 8 9 USING_NS_CC; 10 11 class BackgroundLayer :public Layer{ 12 13 public: 14 15 BackgroundLayer(); 16 ~BackgroundLayer(); 17 18 virtual bool init(); 19 20 CREATE_FUNC(BackgroundLayer); 21 22 void BGImageMove(float); 23 24 // 开始菜单按钮的回调函数 25 void start(Ref *pSender); 26 27 void addStage(); 28 29 void stageMove(); 30 31 // 单点触摸事件回调函数 32 virtual bool onTouchBegan(Touch *touch, Event *unused_event); 33 virtual void onTouchMoved(Touch *touch, Event *unused_event); 34 virtual void onTouchEnded(Touch *touch, Event *unused_event); 35 36 // 将棍子添加到视线中 37 void addStick(); 38 // 将棍子加长 39 void stickLength(float); 40 void stopStick(); 41 42 // 旋转棍子 43 void rotateStickAndGo(); 44 45 void stageAndPlayerMove(); 46 47 void resetStick(); 48 49 void initStick(); 50 51 void playerMoveToNextStage(); 52 53 void PlayerDown(); 54 void PlayerMove(); 55 56 private: 57 58 // 可视区域大小 59 Size visibleSize; 60 // 背景 61 Sprite* _imageOne; 62 Sprite* _imageTwo; 63 // 开始按钮菜单 64 MenuItemSprite* _startButton; 65 Menu* _menu; 66 // 标题 67 Sprite* _gameTitle; 68 69 // 英雄所站的平台 70 Sprite *stage_sprite[3]; // 存储平台数据 71 int stage_number; // 标识,为了方便计算 72 73 // 单点触摸事件 74 EventListenerTouchOneByOne *_touchListener; 75 // 简单的状态机 76 bool isStart; 77 78 // 英雄 79 Player _player; 80 81 // 棍子 82 Sprite *_stick; 83 84 // 存储我们棍子的长度 85 float touchLength; 86 87 int nowStage; 88 int lastStage; 89 int nextStage; 90 91 Vec2 stickPoint; 92 93 GameOverLayer* over; 94 95 float destLengthMin; 96 97 float destLengthMax; 98 }; 99 100 101 102 #endif __Background_Layer_H__
BackgroundLayer.cpp
1 #include "BackgroundLayer.h" 2 3 4 BackgroundLayer::BackgroundLayer(): 5 stage_number(1), 6 isStart(false), 7 lastStage(2), 8 nowStage(0), 9 nextStage(1){ 10 11 } 12 13 BackgroundLayer::~BackgroundLayer(){ 14 15 } 16 17 bool BackgroundLayer::init(){ 18 19 if (!Layer::init()){ 20 return false; 21 } 22 23 visibleSize = Director::getInstance()->getVisibleSize(); 24 25 /***************** 背景图片随机显示 ***************/ 26 27 // 随机生成 5 种背景图片 28 srand(time(0)); // 随机生成数种子 29 int _bgImageNumber = rand() % 5; 30 log("%d", _bgImageNumber); 31 switch (_bgImageNumber){ 32 33 case 0: 34 _imageOne = Sprite::create(welcome_background_image0); 35 _imageTwo = Sprite::create(welcome_background_image0); 36 break; 37 38 case 1: 39 _imageOne = Sprite::create(welcome_background_image1); 40 _imageTwo = Sprite::create(welcome_background_image1); 41 break; 42 43 case 2: 44 _imageOne = Sprite::create(welcome_background_image2); 45 _imageTwo = Sprite::create(welcome_background_image2); 46 break; 47 48 case 3: 49 _imageOne = Sprite::create(welcome_background_image3); 50 _imageTwo = Sprite::create(welcome_background_image3); 51 break; 52 53 case 4: 54 _imageOne = Sprite::create(welcome_background_image4); 55 _imageTwo = Sprite::create(welcome_background_image4); 56 break; 57 58 default: 59 break; 60 61 } 62 63 _imageOne->setPosition(visibleSize.width / 2, visibleSize.height / 2); 64 _imageTwo->setPosition(visibleSize.width / 2 + _imageTwo->getContentSize().width, visibleSize.height / 2); 65 this->addChild(_imageOne, 1); 66 this->addChild(_imageTwo, 1); 67 68 /***************** 背景图片随机显示 ***************/ 69 70 /***************** 添加标题 ***************/ 71 72 _gameTitle = Sprite::create(welcome_title_image); 73 _gameTitle->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 5 * 4)); 74 this->addChild(_gameTitle, 2); 75 76 /***************** 添加标题 ***************/ 77 78 79 /***************** 添加开始按钮 ***************/ 80 81 _startButton = MenuItemSprite::create(Sprite::create(welcome_start_image), Sprite::create(welcome_start_image), NULL, this, menu_selector(BackgroundLayer::start)); 82 _startButton->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2 + 10)); 83 // 按钮上下来回移动的效果 84 MoveTo *startButtonMoveDown = MoveTo::create(2.0f, Vec2(visibleSize.width / 2, visibleSize.height / 2 - 20)); 85 MoveTo *startButtonMoveUp = MoveTo::create(2.0f, Vec2(visibleSize.width / 2, visibleSize.height / 2 + 20)); 86 Sequence *startButtonSequence = Sequence::create(startButtonMoveDown, startButtonMoveUp, nullptr); 87 RepeatForever *startButtonRepeatForever = RepeatForever::create(startButtonSequence); 88 _startButton->runAction(startButtonRepeatForever); 89 _menu = Menu::create(_startButton, nullptr); 90 _menu->setPosition(Vec2::ZERO); 91 this->addChild(_menu, 2); 92 93 94 /***************** 添加开始按钮 ***************/ 95 96 /***************** 添加英雄所站的平台 ***************/ 97 98 // 对所有平台进行一次初始化, 99 // 并且把第一个平台显示在屏幕中心 100 101 for (int i = 0; i < 3; i++){ 102 stage_sprite[i] = Sprite::create(welcome_floor_image); 103 } 104 // 在 x轴 方向扩大30倍 105 stage_sprite[0]->setScaleX(30); 106 // 设置另两个平台的位置 107 for (int i = 1; i < 3; i++){ 108 stage_sprite[i]->setPosition(Vec2(visibleSize.width + stage_sprite[i]->getScaleX() * stage_sprite[i]->getContentSize().width, stage_sprite[i]->getContentSize().height / 2)); 109 } 110 stage_sprite[0]->setPosition(Vec2(visibleSize.width / 2, stage_sprite[0]->getContentSize().height / 4)); 111 for (int i = 0; i < 3; i++){ 112 this->addChild(stage_sprite[i], 3); 113 } 114 115 /***************** 添加英雄所站的平台 ***************/ 116 117 118 /***************** 单点触摸事件 ***************/ 119 120 _touchListener = EventListenerTouchOneByOne::create(); 121 _touchListener->setSwallowTouches(true); // true 表示不向下传递触摸事件 122 _touchListener->onTouchBegan = CC_CALLBACK_2(BackgroundLayer::onTouchBegan, this); 123 _touchListener->onTouchMoved = CC_CALLBACK_2(BackgroundLayer::onTouchMoved, this); 124 _touchListener->onTouchEnded = CC_CALLBACK_2(BackgroundLayer::onTouchEnded, this); 125 _eventDispatcher->addEventListenerWithSceneGraphPriority(_touchListener, this); 126 127 /***************** 单点触摸事件 ***************/ 128 129 130 /***************** 添加英雄 ***************/ 131 132 //// 初始化主角 133 //_player.init(); 134 //// 先获取到我们Player对象中的Sprite对象,然后在对Sprite对象进行坐标设置。设置的位置是在我们平台之上 135 //_player.getSprite()->setPosition(Vec2(visibleSize.width / 2, stage_sprite[0]->getContentSize().height / 4 * 3)); 136 //// 将我们Player对象中的Sprite对象加入到场景中 137 //this->addChild(_player.getSprite(), 10); 138 139 140 _player.init(); 141 _player.setPosition(Vec2(visibleSize.width / 2, stage_sprite[0]->getContentSize().height / 4 * 3)); 142 this->addChild(_player.getSprite(), 5); 143 _player.stay(); 144 145 /***************** 添加英雄 ***************/ 146 147 148 /***************** 添加棍子 ***************/ 149 150 _stick = Sprite::create(stick_image); 151 // 这里我们先把锚点设置为图片的正下方,这样我们也方便我们设置它的位置 152 _stick->setAnchorPoint(Vec2(0.5, 0)); 153 // 在我们不需要用到这个棍子的时候,我们先把它设置在屏幕外面 154 _stick->setPosition(Vec2(-_stick->getContentSize().width, -_stick->getContentSize().height)); 155 this->addChild(_stick, 15); 156 157 /***************** 添加棍子 ***************/ 158 159 160 161 return true; 162 } 163 164 void BackgroundLayer::BGImageMove(float){ 165 166 } 167 168 void BackgroundLayer::start(Ref *pSender){ 169 170 // 设置动画,标题和按钮向上飞出屏幕,平台移动到指定位置 171 MoveTo *menuMove = MoveTo::create(0.8f, Vec2(visibleSize.width / 2, visibleSize.height + _menu->getContentSize().height)); 172 MoveTo *gameTitleMove = MoveTo::create(0.8f, Vec2(visibleSize.width / 2, visibleSize.height + _gameTitle->getContentSize().height)); 173 _menu->runAction(menuMove); 174 _gameTitle->runAction(gameTitleMove); 175 MoveTo *gamefloor = MoveTo::create(1.0f, Vec2(100, stage_sprite[0]->getContentSize().height / 4)); 176 stage_sprite[0]->runAction(Sequence::create(gamefloor, CallFunc::create([this](){ 177 // 移除按钮 178 this->removeChild(_menu); 179 log("remove menu"); 180 // 移除标题 181 this->removeChild(_gameTitle); 182 log("remove gametitle"); 183 }),nullptr)); 184 addStage(); 185 isStart = true; 186 _player.start(Vec2(100, stage_sprite[0]->getContentSize().height / 4 * 3)); 187 } 188 189 void BackgroundLayer::stageMove(){ 190 191 /*********************************************** 192 * 193 * 开始的三行代码是用来得到当前对应的平台编号和前后平台的编号, 194 * 这里一定要注意我们自己的编号是[1,3],而数组的编号是[0,2],注意逻辑关系 195 * 196 *************************************************/ 197 198 nowStage = stage_number == 0 ? 2 : stage_number - 1; 199 lastStage = nowStage == 0 ? 2 : (nowStage - 1); 200 nextStage = nowStage == 2 ? 0 : (nowStage + 1); 201 MoveTo *nowStageMove = MoveTo::create(1.0f, Vec2(100, stage_sprite[0]->getContentSize().height / 4)); 202 stage_sprite[nowStage]->runAction(nowStageMove); 203 MoveTo *lastStageMove = MoveTo::create(1.0f, Vec2(-stage_sprite[lastStage]->getContentSize().width * stage_sprite[lastStage]->getScaleX(), stage_sprite[0]->getContentSize().height / 4)); 204 stage_sprite[lastStage]->runAction(lastStageMove); 205 addStage(); 206 initStick(); 207 208 } 209 210 void BackgroundLayer::addStage(){ 211 212 /***************************************************** 213 * 214 * setScale 并不会改变它本来的宽度和高度,你必须在你获取长度或者宽度之后在乘以你设置的倍数。 215 * tage_number我们的数组编号是[0,2],初始的时候我们把它的值设置为1,表示第一个平台, 216 * 当我们每加入一个平台这个数就+1,也就是我们接下来控制的平台, 217 * 当这个数==2时,+1之后应该是回到第一个平台的编号,所以这里写一个逻辑判断完成这个功能。 218 * 219 *****************************************************/ 220 221 stage_sprite[stage_number]->setScaleX(CCRANDOM_0_1() * 40); 222 stage_sprite[stage_number]->setPosition(Vec2(visibleSize.width + stage_sprite[stage_number]->getScaleX() * stage_sprite[stage_number]->getContentSize().width, stage_sprite[stage_number]->getContentSize().height / 4)); 223 MoveTo *stageMove = MoveTo::create(0.5f, Vec2(visibleSize.width / 2 + CCRANDOM_0_1() * visibleSize.width / 3, stage_sprite[stage_number]->getContentSize().height / 4)); 224 stage_sprite[stage_number]->runAction(stageMove); 225 if (stage_number + 1 <= 2){ 226 stage_number += 1; 227 } 228 else{ 229 stage_number = 0; 230 } 231 } 232 233 bool BackgroundLayer::onTouchBegan(Touch *touch, Event *unused_event){ 234 if (isStart){ 235 //stageMove(); 236 addStick(); 237 } 238 return true; 239 } 240 241 void BackgroundLayer::onTouchMoved(Touch *touch, Event *unused_event){ 242 243 } 244 245 void BackgroundLayer::onTouchEnded(Touch *touch, Event *unused_event){ 246 247 stopStick(); 248 } 249 250 void BackgroundLayer::addStick(){ 251 //stageMove(); 252 253 // 棍子设置在平台的右上角那个点上了 254 _stick->setPosition(Vec2(stage_sprite[nowStage]->getPosition().x + stage_sprite[nowStage]->getContentSize().width * 255 stage_sprite[nowStage]->getScaleX() / 2, stage_sprite[nowStage]->getContentSize().height / 4 * 3)); 256 //_stick->setPosition(stickPoint); 257 //_stick->setPosition(Vec2(stage_sprite[stage_number]->getPosition().x + stage_sprite[stage_number]->getContentSize().width * stage_sprite[stage_number]->getScaleX() / 2, stage_sprite[stage_number]->getContentSize().height / 4 * 3)); 258 this->schedule(schedule_selector(BackgroundLayer::stickLength)); 259 } 260 261 void BackgroundLayer::stickLength(float){ 262 263 _stick->setScaleY(_stick->getScaleY() + 1); 264 } 265 266 void BackgroundLayer::stopStick(){ 267 268 // 存储我们棍子的长度 269 touchLength = _stick->getContentSize().height * _stick->getScaleY(); 270 this->unschedule(schedule_selector(BackgroundLayer::stickLength)); 271 rotateStickAndGo(); 272 } 273 274 void BackgroundLayer::rotateStickAndGo(){ 275 276 // 算出棍子到中间的平台之间的最小距离(即,计算出到平台前沿的距离) 277 destLengthMin = abs( 278 stage_sprite[lastStage]->getPositionX() 279 - stage_sprite[nowStage]->getPositionX() 280 - stage_sprite[lastStage]->getContentSize().width 281 * stage_sprite[lastStage]->getScaleX() / 2 282 - stage_sprite[nowStage]->getContentSize().width 283 * stage_sprite[nowStage]->getScaleX() / 2 284 ); 285 // 算出棍子到中间的平台之间的最大距离(即,计算出到平台后沿的距离) 286 destLengthMax = destLengthMin + stage_sprite[nowStage]->getContentSize().width * stage_sprite[nowStage]->getScaleX(); 287 // CallFunc是一个动作类回调函数, 288 // 用这样一个回调可以连接不同精灵同时或者按顺序进行一些动作,这里我们设置棍子和主角两个对象的动画 289 CallFunc *moveToNext = CallFunc::create(CC_CALLBACK_0(BackgroundLayer::playerMoveToNextStage, this)); 290 RotateTo *stickRotate = RotateTo::create(1.0f, 90); 291 Sequence *gogo = Sequence::create(stickRotate, moveToNext, NULL); 292 293 RotateTo *stickRotateDown = RotateTo::create(1.0f, 180); 294 CallFunc* GoCallBack = CallFunc::create(CC_CALLBACK_0(BackgroundLayer::PlayerMove, this)); 295 Sequence* StickDown = Sequence::create(stickRotateDown, GoCallBack, NULL); 296 297 // 判断语句判断我们计算出的棍子长度是不是在我们最大距离和最小距离之间, 298 // 若果是就选择90度,反正则旋转180度。 299 log("%f", touchLength); 300 log("%f", destLengthMin); 301 log("%f", destLengthMax); 302 303 304 if (touchLength >= destLengthMin && touchLength <= destLengthMax){ 305 _stick->runAction(gogo); 306 }else if (touchLength < destLengthMin || touchLength > destLengthMax){ 307 _stick->runAction(StickDown); 308 } 309 } 310 311 void BackgroundLayer::stageAndPlayerMove(){ 312 313 // 简单说明一下计算逻辑, 314 // 我们每个平台的x坐标都会移动到100这个点上, 315 // 所以我们只需要加上这个平台一半的宽度, 316 // 再减去主角的一半的宽度是因为主角需要站到平台上, 317 // 这样才看着和谐点 318 Vec2 dest; 319 dest.x = 100 + stage_sprite[nowStage]->getContentSize().width * stage_sprite[nowStage]->getScaleX() / 2 - _player.getSprite()->getContentSize().width / 2 - 5; 320 dest.y = stage_sprite[nowStage]->getContentSize().height / 4 * 3; 321 MoveTo *playermove = MoveTo::create(1.0f, dest); 322 _player.getSprite()->runAction(playermove); 323 stageMove(); 324 resetStick(); 325 } 326 327 void BackgroundLayer::resetStick(){ 328 329 // 重新设置的角度属性,这样下一次使用的它依然是向上的 330 _stick->setRotation(0); 331 // 将它移动到屏幕之外 332 _stick->setPosition(-_stick->getContentSize().width, -_stick->getContentSize().height); 333 // 重新设置了长度属性 334 _stick->setScaleY(1); 335 } 336 337 void BackgroundLayer::initStick(){ 338 339 stickPoint.x = 100 + stage_sprite[nowStage]->getContentSize().width*stage_sprite[nowStage]->getScaleX() / 2; 340 stickPoint.y = stage_sprite[nowStage]->getContentSize().height; 341 } 342 343 void BackgroundLayer::playerMoveToNextStage(){ 344 345 //_player.getSprite()->runAction(MoveBy::create(1.0f, Vec2(destLengthMin + stage_sprite[nowStage]->getContentSize().width * stage_sprite[nowStage]->getScaleX() / 2, 0))); 346 347 stageAndPlayerMove(); 348 } 349 350 void BackgroundLayer::PlayerDown() 351 { 352 // 这个函数中,我们实现了落下,简单粗暴一点,我直接设置落下800像素的距离。 353 // 这里我们使用的是MoveBy,提醒一下大家,这个方法和MoveTo是不同的, 354 // 前者是以自己为起点,以提供的Vec2型变量为一个表示方向和长度向量经行移动, 355 // 后者是直接移动的指定的点,都有同样的动画效果,根据情况来使用不同的方法。 356 // 在这里我们顺便加入结束层, 357 // 层上加一个层是一个很常用的做法,没有问题, 358 // 但是在场景上加场景,虽然不会报错,但是会有潜在危险,希望大家不要使用。 359 360 MoveBy* Down = MoveBy::create(1.0f, Vec2(0, -800)); 361 _player.getSprite()->runAction(Down); 362 over = GameOverLayer::create(); 363 this->addChild(over, 8); 364 365 } 366 367 void BackgroundLayer::PlayerMove() 368 { 369 MoveBy* GO = MoveBy::create(1.0f, Vec2(touchLength, 0)); 370 CallFunc* DownCallBack = CallFunc::create(CC_CALLBACK_0(BackgroundLayer::PlayerDown, this)); 371 Sequence* Goon = Sequence::create(GO, DownCallBack, NULL); 372 _player.getSprite()->runAction(Goon); 373 }
Player.h
1 #ifndef __Player_Layer_H__ 2 #define __Player_Layer_H__ 3 4 #include "cocos2d.h" 5 6 USING_NS_CC; 7 8 class Player{ 9 10 public: 11 12 Player(); 13 ~Player(); 14 // 初始化 15 void init(); 16 // 获取精灵 17 Sprite* getSprite(); 18 // 设置位置 19 void setPosition(Vec2); 20 // 走 21 void walk(Vec2); 22 // 开始 23 void stay(); 24 // 暂停 25 void stop(); 26 // 开始的时候让主角跟着平台走 27 void start(Vec2); 28 29 private: 30 31 Sprite *_player; 32 Animation *_walkAnimation; 33 Animation *_stayAnimation; 34 Animate *_walkAnimate; 35 Animate *_stayAnimat; 36 }; 37 38 39 #endif __Player_Layer_H__
Player.cpp
1 #include "Player.h" 2 #include "Resource.h" 3 4 Player::Player(){ 5 6 } 7 8 Player::~Player(){ 9 10 } 11 12 void Player::init(){ 13 14 _player = Sprite::create(hero1); 15 _player->setAnchorPoint(Vec2(0.5, 0)); 16 17 /************************************** 18 * 19 * 先创建一个Vector类型的数据,用来存储我们动画的每一帧,而这个Vector中的数据类型为SpriteFrame*, 20 * 在看到循环内,我先定义了一个字符型数组,这个用来存储我们每一帧图片的名字,这样方便后面使用, 21 * 然后是给这个字符型数组赋值,这里用到的时sprintf()方法,再来说一说SpriteFrame::create()这个方法, 22 * 这个方法我们一般是用来提取打包图片的(所有帧都是一张图片上),它根据后面Rect的不同来提取不同位置上的图片块, 23 * 但我们在这里也可以这样使用它,提取不同的图片,Rect的值就是图片的大小。 24 * 接下来是createWithSpriteFrame()方法,这个方法是用来从一个Vector数据中读取数据来创建一个动画(Animation), 25 * 其中的第二个参数是这个动画执行的持续时间。接下来一行的方法是用设置动画执行完时是否回到原始状态, 26 * setLoops()是用来设置这个动画反复执行的次数, 27 * 最后一句是用Animation对象去创建一个Animate对象,Animate才能被runAciton()方法所接收。 28 * 29 ***************************************/ 30 Vector<SpriteFrame*> frameVector; 31 for (int i = 1; i <= 5; i++){ 32 char pngName[260] = { 0 }; 33 sprintf(pngName, "image/anim1/stay%d.png", i); 34 frameVector.pushBack(SpriteFrame::create(pngName, Rect(0, 0, 54, 58))); 35 } 36 _stayAnimation = Animation::createWithSpriteFrames(frameVector, 0.1f); 37 _stayAnimation->setRestoreOriginalFrame(false); 38 _stayAnimation->setLoops(10000); 39 _stayAnimat = Animate::create(_stayAnimation); 40 41 frameVector.clear(); 42 for (int i = 1; i <= 5; i++){ 43 char pngName[260] = { 0 }; 44 sprintf(pngName, "image/anim1/walk%d.png", i); 45 frameVector.pushBack(SpriteFrame::create(pngName, Rect(0, 0, 54, 58))); 46 } 47 _walkAnimation = Animation::createWithSpriteFrames(frameVector, 1); 48 _walkAnimation->setRestoreOriginalFrame(false); 49 _walkAnimation->setLoops(10000); 50 _walkAnimate = Animate::create(_walkAnimation); 51 52 } 53 54 Sprite* Player::getSprite(){ 55 56 return _player; 57 } 58 59 void Player::setPosition(Vec2 _pos){ 60 61 _player->setPosition(_pos); 62 } 63 64 void Player::stay(){ 65 66 _player->runAction(_stayAnimat); 67 } 68 69 void Player::stop(){ 70 71 _player->stopAllActions(); 72 } 73 74 void Player::walk(Vec2 _dec){ 75 76 MoveTo *move = MoveTo::create(1.5f, _dec); 77 } 78 79 void Player::start(Vec2 _dec){ 80 81 MoveTo *move = MoveTo::create(1.0f, _dec); 82 _player->runAction(move); 83 }
GameOverLayer.h
1 #include "cocos2d.h" 2 USING_NS_CC; 3 4 class GameOverLayer 5 :public Layer 6 { 7 public: 8 GameOverLayer(); 9 ~GameOverLayer(); 10 CREATE_FUNC(GameOverLayer); 11 virtual bool init(); 12 void ReStart(Ref*); 13 };
GameOverLayer.cpp
1 #include "GameOverLayer.h" 2 #include "WelcomeScene.h" 3 4 GameOverLayer::GameOverLayer(){ 5 6 } 7 8 GameOverLayer::~GameOverLayer(){ 9 10 } 11 12 bool GameOverLayer::init() 13 { 14 if (!Layer::init()) 15 { 16 return false; 17 } 18 Size MyWinSize = Director::getInstance()->getVisibleSize(); 19 MenuItemSprite* ReStartBtn = MenuItemSprite::create(Sprite::create("image/uires_5.png"), Sprite::create("image/uires_5.png"), NULL, this, menu_selector(GameOverLayer::ReStart)); 20 ReStartBtn->setPosition(MyWinSize.width / 2, MyWinSize.height / 2 + 8); 21 Menu* menu = Menu::create(ReStartBtn, NULL); 22 menu->setPosition(0, 0); 23 this->addChild(menu); 24 return true; 25 } 26 27 void GameOverLayer::ReStart(cocos2d::Ref * _pSender) 28 { 29 WelcomeScene* scene = WelcomeScene::create(); 30 Director* pDirector = Director::getInstance(); 31 TransitionFade* jump = TransitionFade::create(0.3f, scene); 32 pDirector->replaceScene(jump); 33 }
时间: 2024-10-27 13:20:45