当前位置:编程学习 > wap >>

[ocos2d-x游戏引擎开发笔记]Tiled Map Editor

1.添加敌人
在对象层中加入敌人,

\

 
注意,加入了一个键值对,键为n,值为1。这个键值对主要就是为了区分敌人和英雄。将敌人画在地图上。
[cpp]
for(int i=0;i<int(objects->getObjects()->count());i++)  
        {  
            CCDictionary *enemy=(CCDictionary *)objects->getObjects()->objectAtIndex(i);  
            if(enemy->valueForKey("n")->intValue()==1)  
            {  
                CCSprite *s=CCSprite::create("enemy1.png");  
                float x=enemy->valueForKey("x")->floatValue();  
                float y=enemy->valueForKey("y")->floatValue();  
                s->setPosition(ccp(x,y));  
                s->setAnchorPoint(CCPoint(0,0));  
                _tileMap->addChild(s,4);  
                CCActionInterval *move=CCMoveBy::create(2,CCPoint(_player->getPosition().x-s->getPosition().x>0?10:-10,_player->getPosition().y-s->getPosition().y>0?10:-10));//   
                CCFiniteTimeAction *func=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::goon));  
                s->runAction(CCSequence::create(move,func,NULL));  
            }  
        }  
 
for(int i=0;i<int(objects->getObjects()->count());i++)
{
CCDictionary *enemy=(CCDictionary *)objects->getObjects()->objectAtIndex(i);
if(enemy->valueForKey("n")->intValue()==1)
{
CCSprite *s=CCSprite::create("enemy1.png");
float x=enemy->valueForKey("x")->floatValue();
float y=enemy->valueForKey("y")->floatValue();
s->setPosition(ccp(x,y));
s->setAnchorPoint(CCPoint(0,0));
_tileMap->addChild(s,4);
CCActionInterval *move=CCMoveBy::create(2,CCPoint(_player->getPosition().x-s->getPosition().x>0?10:-10,_player->getPosition().y-s->getPosition().y>0?10:-10));//
CCFiniteTimeAction *func=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::goon));
s->runAction(CCSequence::create(move,func,NULL));
}
}这段代码从对象层中获得所有对象,判断键为n的是否为1,有则为敌人,加入到地图中,并且让敌人运动。
在运动完后调用goon函数:
[cpp] 
void HelloWorld::goon(CCNode *pSender)  
{  
    CCSprite *s=(CCSprite *)pSender;  
    CCActionInterval *move=CCMoveBy::actionWithDuration(2,CCPoint(_player->getPosition().x-s->getPosition().x>0?10:-10,_player->getPosition().y-s->getPosition().y>0?10:-10));//   
                CCFiniteTimeAction *func=CCCallFuncN::actionWithTarget(this,callfuncN_selector(HelloWorld::goon));  
                s->runAction(CCSequence::actions(move,func,NULL));  
}  
 
void HelloWorld::goon(CCNode *pSender)
{
CCSprite *s=(CCSprite *)pSender;
CCActionInterval *move=CCMoveBy::actionWithDuration(2,CCPoint(_player->getPosition().x-s->getPosition().x>0?10:-10,_player->getPosition().y-s->getPosition().y>0?10:-10));//
CCFiniteTimeAction *func=CCCallFuncN::actionWithTarget(this,callfuncN_selector(HelloWorld::goon));
s->runAction(CCSequence::actions(move,func,NULL));
}在goon函数中继续调用自己,这样敌人就一直向英雄运动。
2.使英雄可以攻击
现在游戏的点击是使得英雄移动向指定的位置,所以,为了使得英雄有攻击,可以添加一个状态,状态为true的话,控制英雄移动,状态为false的话,控制发射子弹。在头文件中加入
[cpp] 
bool mode;  
 
bool mode;
在屏幕上添加菜单
[cpp] 
CCMenuItem *oon,*ooff;  
oon=CCMenuItemImage::create("projectile-button-on.png","projectile-button-on.png");  
ooff=CCMenuItemImage::create("projectile-button-off.png","projectile-button-off.png");  
CCMenuItemToggle *toggle=CCMenuItemToggle::createWithTarget(this,//回调函数所在的类      
menu_selector(HelloWorld::toggleGame),//回调函数      
ooff,oon,NULL    //添加的菜单      
  );    
CCMenu *menu=CCMenu::create(toggle,NULL);  
menu->setPosition(ccp(oon->getContentSize().width/2,oon->getContentSize().height/2));  
addChild(menu);  
 
CCMenuItem *oon,*ooff;
oon=CCMenuItemImage::create("projectile-button-on.png","projectile-button-on.png");
ooff=CCMenuItemImage::create("projectile-button-off.png","projectile-button-off.png");
CCMenuItemToggle *toggle=CCMenuItemToggle::createWithTarget(this,//回调函数所在的类   
menu_selector(HelloWorld::toggleGame),//回调函数   
ooff,oon,NULL    //添加的菜单   
    );  
CCMenu *menu=CCMenu::create(toggle,NULL);
menu->setPosition(ccp(oon->getContentSize().width/2,oon->getContentSize().height/2));
addChild(menu);
在toggleGame函数中添加控制状态改变的代码:
[cpp] view plaincopyprint?void HelloWorld::toggleGame(CCObject *pSender)  
{  
    mode=mode?false:true;  
}  
 
void HelloWorld::toggleGame(CCObject *pSender)
{
mode=mode?false:true;
}
当状态为false的时候,hero发射子弹:
[cpp] 
else  
{  
    CCSprite *s=CCSprite::create("Projectile.png");  
    s->setPosition(_player->getPosition());  
    _tileMap->addChild(s,4);  
    float dx=pp.x-_player->getPosition().x;  
    float dy=pp.y-_player->getPosition().y;  
    if(dx>0)  
    {  
        float lx=32*30-_player->getPosition().x;  
        float ly=dy/dx*lx;  
        CCActionInterval *move=CCMoveBy::create(3,ccp(lx+s->getContentSize().width,ly));  
        CCFiniteTimeAction *ff=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::targetFinish));  
        s->runAction(CCSequence::actions(move,ff,NULL));  
    }  
    else  
    {  
  &
补充:移动开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,