您好,登錄后才能下訂單哦!
接著上一篇博文,前面講到加載hero的實現,地圖加載補充一下,
在HelloWorldScene.cpp的函數init()中添加如下代碼:
int lvl=1; char mapPath[20]; char lvlinform[30]; //map_tmx_lvl1 sprintf(mapPath,"mapTmx/map%d.tmx",lvl); sprintf(lvlinform,"Lvl %d",lvl); CCTMXTiledMap *map = CCTMXTiledMap::tiledMapWithTMXFile(mapPath); //addChild(map,0,mapList); if(!map) { CCLOG("map not init!"); } map->setAnchorPoint(CCPointZero); map->setPosition(ccp(0,50*(size.height/320)));//112.5 map->setScaleX(size.width/480); map->setScaleY(size.height/320); this->addChild(map,-2,mapList);
動作游戲,運動比較重要,講一下運動的算法,在Global.h添加幾個函數的定義:
void addBtn_Jump(CCMenuItemImage * pBtn_Jump); void addBtn_Right(CCMenuItemImage * pBtn_Right); void addBtn_Left(CCMenuItemImage * pBtn_Left); void addBtn_Attack(CCMenuItemImage * pBtn_Attack); void addBoy(Boy* boy); void addSwordPlayer(Player* swordPlayer); void addScene(CCLayer* gameScene); void addEnemyMap(CCTMXTiledMap *eMap); void addTiledMap(CCTMXTiledMap *kmap); void move(ccTime dt);
這幾個函數的實現:
void Global::addBoy(Boy* boy) { this->boy=boy; } void Global::addBtn_Jump(CCMenuItemImage* pBtn_Jump) { this->pBtn_Jump=pBtn_Jump; } void Global::addBtn_Attack(CCMenuItemImage* pBtn_Attack) { this->pBtn_Attack=pBtn_Attack; } void Global::addBtn_Right(CCMenuItemImage* pBtn_Right) { this->pBtn_Right=pBtn_Right; } void Global::addBtn_Left(CCMenuItemImage* pBtn_Left) { this->pBtn_Left=pBtn_Left; } void Global::move(ccTime dt) { if(pBtn_Right->getIsSelected()&&yVel==0&&boy->getDirection()!=ATTACK) { if(boy->getDirection()!=RIGHT) { boy->setAnimation(RIGHT,MOVE); } swordPlayer->getActorSprite()->setFlipX(false); boy->getHeroSprite()->setFlipX(false); //-------------------2013.04.21---------------------------- boyDirection=RIGHT; xVel=2.0; } else if(pBtn_Left->getIsSelected()&&yVel==0&&boy->getDirection()!=ATTACK) { if(boy->getDirection()!=LEFT) { boy->setAnimation(LEFT,MOVE); } swordPlayer->getActorSprite()->setFlipX(true); boy->getHeroSprite()->setFlipX(true); //-------------------2013.04.21---------------------------- boyDirection=LEFT; xVel=-1*2.0; } //默認開啟跳躍 if(pBtn_Jump->getIsSelected()) { if(boy->getDirection()!=JUMP&&yVel!=5.0) { boy->setAnimation(JUMP,MOVE);//影響幀數 } yVel=5.0; } //測試 if(boy->getDirection()==JUMP) { //CCLOG(">-------------JUMP"); } if(boy->getDirection()==OVERLOOK) { //CCLOG("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<OVERLOOK"); } //落地時進行了跨越 if(yVel==0&&time==1.5) { if(boy>getDirection()!=OVERLOOK&&xVel==0&&boy>getDirection()!=ATTACK&&boy->getDirection()!=ATTACKED) { //落地時,的設置 if(getStage()==GETSWORD) { boy->getHeroSprite()->setIsVisible(false); swordPlayer->getActorSprite()->setIsVisible(true); } if(boy->getDirection()!=OVERLOOK) { boy->setAnimation(OVERLOOK,STAND); } //SWORDPLAYER ANIMATION if(swordPlayer->getDirection()!=SWORDSTAND) { //CCLOG("SWORDSTAND GROUND!"); swordPlayer->setAnimation(SWORDSTAND,MOVE); } } time=0; } float gravity=0.1; boyPosition.y=(yVel*time)-0.5*gravity*time*time;//導致下降 boyPosition.x=xVel*time; if(boyPosition.x==201.0||boyPosition.x==-201.0) { //CCLOG("kuayue jiaozheng!"); boyPosition.x=0.0; } float y=boy->getHeroSprite()->getPosition().y; if(boy->getHeroSprite()->getPosition().y<GROUND) { position=boy->getHeroSprite()->getPosition(); if(yVel==0) { position.y+=0.5*gravity*time*time;//導致上升,及一上一下(bug) } if(yVel>0) { time=0; } float x=boy->getHeroSprite()->getPosition().y; xVel=0; yVel=0; } boy>getHeroSprite()>setPosition(ccp(position.x+boyPosition.x,position.y+boyPosition.y)); swordPlayer>getActorSprite()>setPosition(ccp(position.x+boyPosition.x,position.y+boyPosition.y)); //CCLOG("BOY POSITION=%f",boy->getHeroSprite()->getPosition().x); time+=0.75; }
在HelloWorldScene.cpp添加如下代碼就可以讓英雄動起來:
sGlobal->addBtn_Attack(pBtn_Attack); sGlobal->addBtn_Jump(pBtn_Jump); sGlobal->addBtn_Left(pBtn_Left); sGlobal->addBtn_Right(pBtn_Right); Global::getInstance()->addPlayer(p); Global::getInstance()->addBoy(player); Global::getInstance()->addScene(this); //動畫的初始化 AnimationManager::getInstance()->loadAnimation(heroAnimation,12); schedule(schedule_selector(HelloWorld::tick),0.005f);
再新建一個函數:
void HelloWorld::tick(cocos2d::ccTime dt) { sGlobal->move(dt); }
現在hero可以動了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。