cocos2d-x开发之动作游戏实战--2-创新互联
接着上一篇博文,前面讲到加载hero的实现,地图加载补充一下,
成都创新互联是专业的元谋网站建设公司,元谋接单;提供成都网站设计、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行元谋网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!在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("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<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 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可以动了。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享标题:cocos2d-x开发之动作游戏实战--2-创新互联
文章来源:http://pwwzsj.com/article/hpios.html