橫屏小遊戲--蘿莉快跑源代碼分析三
阿新 • • 發佈:2018-02-19
mark 無限 分享 idt alt popu odr tracking dispatch
主角出場:
初始化主角
hero = new GameObjHero(); hero->setScale(0.5); hero->setPosition(ccp(100,160)); hero->setVisible(false); addChild(hero,1);
進入GameObjHero類ccp文件
創建主角及動作
this->setContentSize(CCSizeMake(85,90)); //接收觸摸事件 CCDirector* pDirector = CCDirector::sharedDirector(); pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true); CCSprite * obj = CCSprite::create("s_hurt.png");//受傷 hurt = obj->getTexture(); obj = CCSprite::create("s_jump.png"); //跳躍 jump = obj->getTexture(); mainsprite = CCSprite::create("s_1.png"); //第1個動作 //主角奔跑的動畫幀 CCAnimation * animation = CCAnimation::create(); animation->addSpriteFrameWithFileName("s_1.png"); animation->addSpriteFrameWithFileName("s_2.png"); animation->addSpriteFrameWithFileName("s_3.png"); animation->addSpriteFrameWithFileName("s_4.png"); animation->addSpriteFrameWithFileName("s_5.png"); animation->addSpriteFrameWithFileName("s_6.png"); animation->setDelayPerUnit(0.1f); animation->setRestoreOriginalFrame(true); //執行動畫,無限循環 mainsprite->runAction(CCRepeatForever::create(CCAnimate::create(animation))); state = 0; addChild(mainsprite);
本層觸摸事件處理。觸摸開始函數設置主角跳躍
bool GameObjHero::ccTouchBegan(CCTouch* touch, CCEvent* event) { if(((GameMain *)this->getParent())->isover)//遊戲結束則不響應 return false; //setTheState(1); //設置運動狀態 this->setTheState(1); return true; }
setTheState函數用來設置主角幾種運動狀態。1表示跳躍、2表示受傷、0表示奔跑
void GameObjHero::setTheState(short var){ CCLOG("State = %d",state); if(state == var) return; state = var; switch(state){ case 1://跳躍 //先停止奔跑 this->stopAllActions(); mainsprite->stopAllActions(); //跳躍空中的姿勢 mainsprite->setTexture(jump); //執行跳躍動作,完畢後繼續奔跑 this->runAction(CCSequence::create(CCJumpBy::create(2.5,ccp(0,0),100,1),CCCallFuncN::create(this, callfuncN_selector(GameObjHero::jumpend)),NULL));//跳躍完繼續奔跑 break; case 2://受傷 this->stopAllActions(); mainsprite->stopAllActions(); mainsprite->setTexture(hurt);//受傷時姿勢 this->runAction(CCSequence::create(CCBlink::create(3, 10),CCCallFuncN::create(this, callfuncN_selector(GameObjHero::hurtend)),NULL));//受傷了閃爍,然後繼續奔跑 ((GameMain *)this->getParent())->setover(); //受傷了遊戲結束 break; case 0://奔跑動畫 this->stopAllActions(); mainsprite->stopAllActions(); CCAnimation * animation = CCAnimation::create(); animation->addSpriteFrameWithFileName("s_1.png"); animation->addSpriteFrameWithFileName("s_2.png"); animation->addSpriteFrameWithFileName("s_3.png"); animation->addSpriteFrameWithFileName("s_4.png"); animation->addSpriteFrameWithFileName("s_5.png"); animation->addSpriteFrameWithFileName("s_6.png"); animation->setDelayPerUnit(0.1f); animation->setRestoreOriginalFrame(true); mainsprite->runAction(CCRepeatForever::create(CCAnimate::create(animation))); break; } }
遊戲分數
gamemark = new GameMark(); addChild(gamemark,4);
遊戲結束語
gameover = CCSprite::create("gameover.png"); gameover->setAnchorPoint(ccp(0.5,0.5)); gameover->setPosition(ccp(0,0)); gameover->setPosition(ccp(size.width/2,size.height/2 + 70)); gameover->setVisible(false); gameover->setScale(0.5); addChild(gameover,5);
遊戲結束上一步菜單
CCMenuItemImage *pCloseItem = CCMenuItemImage::create("back.png","back.png", this, menu_selector(GameMain::menuBackCallback) ); pCloseItem->setPosition( ccp(size.width/2,size.height/2 - 50) ); pCloseItem->setScale(0.5); CCMenu *pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu,5,25); pMenu->setVisible(false); pMenu->setEnabled(false);
遊戲主頁面中update函數用來檢測主角是否掉下懸崖、主角是否吃了星星
//奔跑時檢測主角是否掉下 if(hero->state == 0) isherodrop(); //檢測 void GameMain::isherodrop(){ // 獲取背景圖的的坐標(前面創建背景連接的2張圖) CCPoint p1 = (map->getChildByTag(0))->getPosition(); CCPoint p2 = (map->getChildByTag(1))->getPosition(); int temp; if(p1.x <= 100 && (p1.x + 480) >= 100) //主角在第1張背景圖,主角x坐標100 { temp = (100 - p1.x) / 64;//獲取圖塊編號 if(bg1shu[temp] == -1){//沒有在圖塊上,則是掉下懸崖了 CCLog("this one"); hero->setTheState(2);//受傷 } }else{//背景圖2 temp = (100 - p2.x) / 64; if(bg2shu[temp] == -1) { CCLog("this two"); hero->setTheState(2); } } }
主角吃星星
CCPoint p1 = (map->getChildByTag(0))->getPosition(); CCPoint p2 = (map->getChildByTag(1))->getPosition(); for (int i = 0; i < 5; i++) { if (p1.x <= 100 && (p1.x + 480) > 100) { GameObjStar *obj = (GameObjStar *)(map->stars1)->objectAtIndex(i); //獲取單個星星 if(obj->get_visable() && isCollion(ccp(100,hero->getPosition().y + 62.5),ccp(p1.x + 86 + 96 * i,280),40,35,18.25,17.75))//星星可見且碰撞了 { obj->set_visable(false);//星星消失 gamemark->addnumber(200);//加分 } } else { GameObjStar *obj = (GameObjStar *)(map->stars2)->objectAtIndex(i); if(obj->get_visable() && isCollion(ccp(100,hero->getPosition().y + 62.5),ccp(p2.x + 86 + 96 * i,280),40,35,18.25,17.75)){ obj->set_visable(false); gamemark->addnumber(200); } } }
?
主角與星星碰撞 bool GameMain::isCollion(CCPoint p1,CCPoint p2,int w1,int h1,int w2,int h2){ //CCLOG("p1p2x(%f,%f) , p1.x-p2.x=%f w1+w2=%d",p1.x,p2.x,abs(p1.x-p2.x),(w1 + w2)); if(abs(p1.x - p2.x) < w1 + w2 && abs(p1.y - p2.y) < h1 + h2) { return true; } return false; };
?
橫屏小遊戲--蘿莉快跑源代碼分析三