温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

cocos2d-x开发之动作游戏实战--2

发布时间:2020-08-04 22:07:58 来源:网络 阅读:1234 作者:lonag 栏目:游戏开发

     接着上一篇博文,前面讲到加载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可以动了。



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI