亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

cocos2d-x開發之動作游戲實戰--3

發布時間:2020-06-05 15:28:13 來源:網絡 閱讀:1070 作者:lonag 欄目:游戲開發

 現在只有hero一個在走來走去沒什么意思,現在添加幾個npc,給hero點壓力,好的那就開始吧!

添加enemy.h文件和enemy.h文件

#ifndef _ENEMY_H_//66
#define _ENEMY_H_
#include"cocos2d.h"
#include"DBgame.h"
#define MaxBuf 20
using namespace cocos2d;
class Enemy:public CCNode
{
public:
    Enemy(void);
    ~Enemy(void);
    //靜態方法用于創建hero實例
    static Enemy* monster(char *monsterName);
    void setAnimation(Direction dir,Behaviour be,char* monsterName);
                                                                                   
    Direction getDirection(); 
    Behaviour getBehaviour();
    void getCurrentAnimationIndex();
    void boxAttacked(Direction dir,Behaviour be,int i);
    void boxAttackedDone(CCNode* pSender,void *i);
    void portal(Direction dir,Behaviour be,int i);
    void portalDone(CCNode* pSender,void *i);
    void cube(Direction dir,Behaviour be,int i);
    void cubeDone(CCNode* pSender,void *i);
    void executeAnimation(Direction dir,Behaviour be,int nextAni,char* monsterName);
    void executeDone(CCNode* pSender,void* i);
    void deadAnimation(int id,char* monsterName);
    void enemyDeadAnimation(int id,char* monsterName);
    void enemyDeadAnimationDone();
    void enemypDingDeadAnimation(int id,char* monsterName);
    void enemypDingDeadAnimationDone();
    void enemyWingDeadAnimation(int id,char* monsterName);
    void enemyWingDeadAnimationDone();
    void dingDead(int id,char* monsterName);
    void dingDeadDone();
    int getDeadID();
    int id;
    void pDingDead(int id,char* monsterName);
    void pDingDeadDone();
    int getpDingDeadID();
    int pId;
    int wID;
    int getWingDeadID();
    //void wingDead(int id,char* monsterName);
    //void wingDeadDone();
    CCRect getRect();
    CCRect getCollisionRect(int width,int height);
                                                                                   
    Behaviour behaviour;
    Direction direction;
    void jump();
    CCSprite* getMonsterSprite();
    CCAnimate *getAnimate(char* monsterName);
protected:
                                                                                   
    CCAnimation* getAnimation();
    CCSprite *sprite;
    float xVel;
    float yVel;
    CCPoint touchPoint;
    bool init(char* monsterName);
};
#define MAXLIFE 100
class EnemyData
{
public:
    //yDing     42
    EnemyData(void);
    float getMonsterHeight(int i);
    //void initMonsterHeightArray(int i);
    void setMonsterHeight(float height,int i);
    int getEnemyLife(int i);
    void setEnemyLife(int life,int i);
    float getActionTime(int i);
    void setActionTime(float val,int i);
    CCPoint getPositionMonster(int i);
    void setPositionMonster(CCPoint pos,int i);
private:
    //CCMutableArray<float*> *monsterHeight;
    CCPoint monPosition[MaxBuf];
    float actionTime[MaxBuf];
    int enemyLife[MaxBuf];
    float monsterHeightArray[MaxBuf];
};
#endif//

enemy.h文件

#include"enemy.h"http://497
#include"cocos2d.h"
#include"core\boy.h"
#include"DBgame.h"
#include"core\Animation.h"
using namespace cocos2d;
Enemy::Enemy()
{
    xVel=0;
    yVel=0;
}
Enemy::~Enemy()
{
}
Enemy* Enemy::monster(char* monsterName)
{
                                                                       
    Enemy *monster = new Enemy();
    if (monster && monster->init(monsterName))
    {
                                                                           
        monster->autorelease();
        return monster;
    }
    CC_SAFE_DELETE(monster);
    return NULL;
}
bool Enemy::init(char* monsterName)
{
    bool bRet = false;
    do{
        //printf("hello-------------\n");
        this->setAnchorPoint(CCPointZero);
        //創建動畫
        //sprite=CCSprite::spriteWithSpriteFrameName("yDing_0.png");
        char charBuffer[128]="\0";
                                                                           
        sprintf(charBuffer,"%s_0.png",monsterName);
        //CCLOG("hello-------------%s",charBuffer);
        sprite=CCSprite::spriteWithSpriteFrameName(charBuffer);//yDing,pDing
        sprite->setAnchorPoint(CCPointZero);
        this->addChild(sprite);
        //設置狀態
        const char* str=charBuffer;
        if(strcmp("box_0.png",str)==0)
        {
            //CCLOG("BOX------------------");
            behaviour=MOVE;
            direction=BOVERLOOK;
        }
        else if(strcmp("pDing_0.png",str)==0)
        {
            //CCLOG("BOX------------------");
            behaviour=MOVE;
            direction=MOVERLOOK;
        }
        else if(strcmp("wing_0.png",str)==0)
        {
            behaviour=MOVE;
            direction=WLEFT;
        }
        else if(strcmp("yDing_0.png",str)==0)
        {
            behaviour=MOVE;
            //behaviour=DEAD;
            direction=MOVERLOOK;
            //direction=REMOVE;
        }
        else if(strcmp("portal_0.png",str)==0)
        {
            behaviour=MOVE;
            //behaviour=DEAD;
            direction=PORTALONE;
            sprite->setIsVisible(false);
            //direction=REMOVE;
        }
        else if(strcmp("cube_0.png",str)==0)
        {
            behaviour=MOVE;
            direction=CUBEONE;
        }
        //behaviour=MOVE;
        //direction=MOVERLOOK;
        //direction=MATTACK;
        CCAction* action=CCRepeatForever::actionWithAction(getAnimate(monsterName));//monsterName ---"yDing pDing"
        //sprite->runAction(CCRepeatForever::actionWithAction(getAnimate()));
        sprite->runAction(action);
        //
        bRet=true;
    }while(0);
    return bRet;
}
void Enemy::getCurrentAnimationIndex()
{
    /*
    假設有一個CCAnimation* anim,由5張圖組成。
    將這個anim包裝成CCAnimate* animate。使用的時侯sprite->runAction(animate),播放動畫。
    */
    //這里我們來獲取第幾幀:
    int currentAnimIndex = 0; //精靈當前播放的是第幾幀
    CCAnimation* anim=getAnimation();
    for(int i = 1; i <4; i++)
    {
        //CCLOG("animIndex");
        //5張圖5幀
        if(sprite->displayedFrame() == anim->getFrames()->getObjectAtIndex(i))
        {
            //這個i返回的只是一個索引,如果幀數是從1開始計算就要+1
            currentAnimIndex = i+1;
            CCLOG("animaIndex:%d",currentAnimIndex);
        }
    }
}
CCSprite* Enemy::getMonsterSprite()
{
    return this->sprite;
}
void Enemy::boxAttacked(Direction dir,Behaviour be,int i)
{
    behaviour=be;
    direction=dir;
                                                                       
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate("box"),
        CCCallFuncND::actionWithTarget(this, callfuncND_selector(Enemy::boxAttackedDone),(void*)i),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
//--------------------2013.3.3----------------------------------
//下一關的通道有關函數
void Enemy::portal(Direction dir,Behaviour be,int i)
{
    behaviour=be;
    direction=dir;
                                                                       
    sprite->setIsVisible(true);
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate("portal"),
        CCCallFuncND::actionWithTarget(this, callfuncND_selector(Enemy::portalDone),(void*)i),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::portalDone(CCNode* pSender,void* i)
{
    int j=(int)i;
    if(j==1)
    {
        behaviour=MOVE;
        direction=PORTALTWO;
    }
    sprite->stopAllActions();
    sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("portal")));
}
//---------------------2013.3.3--------------------------
//道具獲得箱子的有關函數
void Enemy::cube(Direction dir,Behaviour be,int i)
{
    behaviour=be;
    direction=dir;
                                                                       
    sprite->setIsVisible(true);
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate("cube"),
        CCCallFuncND::actionWithTarget(this, callfuncND_selector(Enemy::cubeDone),(void*)i),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::cubeDone(CCNode *pSender,void *i)
{
    int j=(int)i;
    if(j==1)
    {
        behaviour=MOVE;
        direction=CUBEONE;
    }
    else if(j==2)
    {
        behaviour=MOVE;
        direction=CUBETWO;
    }
    sprite->stopAllActions();
    sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("cube")));
    sprite->setIsVisible(false);
}
void Enemy::enemyDeadAnimation(int id,char* monsterName)
{
    behaviour=DEAD;
    direction=REMOVE;
    this->id=id;
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate(monsterName),
        CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::enemyDeadAnimationDone)),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::enemyDeadAnimationDone()
{
    sGlobal->enemyDeadAnimationDone(1,true);
}
void Enemy::enemypDingDeadAnimation(int id,char* monsterName)
{
    behaviour=DEAD;
    direction=REMOVE;
    this->pId=id;
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate(monsterName),
        CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::enemypDingDeadAnimationDone)),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::enemypDingDeadAnimationDone()
{
    sGlobal->enemyDeadAnimationDone(2,true);
}
void Enemy::enemyWingDeadAnimation(int id,char* monsterName)
{
    behaviour=DEAD;
    direction=WREMOVE;
    this->wID=id;
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate(monsterName),
        CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::enemyWingDeadAnimationDone)),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::enemyWingDeadAnimationDone()
{
    sGlobal->enemyDeadAnimationDone(3,true);
}
int Enemy::getWingDeadID()
{
    return this->wID;
}
void Enemy::deadAnimation(int id,char* monsterName)
{
    behaviour=DEAD;
    direction=REMOVE;
    this->id=id;
    sprite->stopAllActions();
    sprite->runAction(CCRepeatForever::actionWithAction(getAnimate(monsterName)));
}
void Enemy::dingDead(int id,char* monsterName)
{
    behaviour=DEAD;
    direction=REMOVE;
    this->id=id;
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate(monsterName),
        CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::dingDeadDone)),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::pDingDead(int id,char* monsterName)
{
    behaviour=DEAD;
    direction=REMOVE;
    this->pId=id;
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate(monsterName),
        CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::pDingDeadDone)),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
int Enemy::getpDingDeadID()
{
    return this->pId;
}
int Enemy::getDeadID()
{
    return this->id;
}
void Enemy::dingDeadDone()
{
    //behaviour=DEAD;
    //direction=REMOVE;
    //sprite->stopAllActions();
    //運動的算法
    CCPoint pos=this->getMonsterSprite()->getPosition();
    //sprite->stopAllActions();
    //sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("yDing")));
    sprite->runAction(CCMoveTo::actionWithDuration(1, CCPointMake(pos.x+40,pos.y)));
    //sprite->setIsVisible(false);
}
void Enemy::pDingDeadDone()
{
    //
}
void Enemy::boxAttackedDone(CCNode* pSender,void* i)
{
    int j=(int)i;
    if(j==1)
    {
        behaviour=MOVE;
        direction=BONE;
    }
    else if(j==2)
    {
        behaviour=MOVE;
        direction=BTWO;
    }
    else if(j==3)
    {
        behaviour=MOVE;
        direction=BTHREE;
    }
                                                                       
    sprite->stopAllActions();
    sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("box")));
                                                                       
}
void Enemy::setAnimation(Direction dir,Behaviour be,char* monsterName)
{
    direction=dir;
    behaviour=be;
    sprite->stopAllActions();
    sprite->runAction(CCRepeatForever::actionWithAction(getAnimate(monsterName)));
}
//1 2 3 yDing          11 12 13 pDing
void Enemy::executeAnimation(Direction dir,Behaviour be,int nextAni,char* monsterName)
{
    behaviour=be;
    direction=dir;
                                                                       
    this->cleanup();
    sprite->cleanup();
    CCAction *sequneceAction = CCSequence::actions(
        getAnimate(monsterName),
        CCCallFuncND::actionWithTarget(this, callfuncND_selector(Enemy::executeDone),(void*)nextAni),
        NULL);
                                                                       
    sprite->runAction(sequneceAction);
}
void Enemy::executeDone(CCNode* pSender,void* i)
{
    int next=(int)i;
    if(next==1)
    {
        behaviour=MOVE;
        direction=MOVERLOOK;
    }
    else if(next==2)
    {
        behaviour=MOVE;
        direction=MATTACK;
    }
    else if(next==3)
    {
        behaviour=DEAD;
        direction=REMOVE;
    }
    else if(next==11)
    {
        behaviour=MOVE;
        direction=MOVERLOOK;
    }
    else if(next==12)
    {
        behaviour=MOVE;
        direction=MATTACK;
    }
    else if(next==13)
    {
        behaviour=DEAD;
        direction=REMOVE;
    }
    else if(next==21)
    {
        behaviour=MOVE;
        direction=WRIGHT;
    }
    else if(next=22)
    {
        behaviour=MOVE;
        direction=WLEFT;
    }
    else if(next==23)
    {
        behaviour=MOVE;
        direction=WATTACK;
    }
    else if(next=24)
    {
        behaviour=DEAD;
        direction=WREMOVE;
    }
    sprite->stopAllActions();
    if(next<20&&next>10)
    {
        sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("pDing")));
    }
    else if(next<10)
    {
        sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("yDing")));
    }
    else if(next>20)
    {
        sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("wing")));
    }
                                                                       
}
//注意,不同精靈,這個函數寫法不同,主要區別都是OVERLOOK上,有些精靈是沒有四方向的
CCAnimate * Enemy::getAnimate(char* monsterName)
{
    if (behaviour==DEAD)
    {
        //direction=MOVERLOOK;
    }
                                                                       
    return AnimationManager::getInstance()->getAnimate(monsterName,behaviour,direction);
                                                                       
}
CCAnimation* Enemy::getAnimation()
{
    if (behaviour==DEAD)
    {
        //direction=MOVERLOOK;
    }
    return AnimationManager::getInstance()->getAnimation("yDing",behaviour,direction);
}
Direction Enemy::getDirection()
{
    return this->direction;
}
Behaviour Enemy::getBehaviour()
{
    return this->behaviour;
}
CCRect Enemy::getRect()
{
    float x=sprite->getPosition().x;
    float y=sprite->getPosition().y;
    //printf("x:%f  y:%f\n",x,y);
    return CCRectMake(x,y,70,62);
                                                                       
}
CCRect Enemy::getCollisionRect(int width,int height)
{
    float x=sprite->getPosition().x;
    float y=sprite->getPosition().y;
    //CCLOG("Enemy--x:%f  y:%f   width:%d    height:%d\n",x,y,width,height);
    return CCRectMake(x,y,width,height);
}
void Enemy::jump()
{
    this->sprite->runAction(CCJumpTo::actionWithDuration(0.05f,ccp(this->sprite->getPosition().x , this->sprite->getPosition().y),20,100));
}
EnemyData::EnemyData(void)
{
    for(int i=0;i<MaxBuf;i++)
    {
        enemyLife[i]=MAXLIFE;
        monsterHeightArray[i]=2.5;
        actionTime[i]=0.0;
        monPosition[i]=CCPointZero;
    }
    //monsterHeight=new CCMutableArray<float*>();
}
int EnemyData::getEnemyLife(int i)
{
    return this->enemyLife[i];
                                                                       
}
void EnemyData::setEnemyLife(int life,int i)
{
                                                                       
    this->enemyLife[i]=life;
}
float EnemyData::getMonsterHeight(int i)
{
    return this->monsterHeightArray[i];
}
void EnemyData::setMonsterHeight(float height,int i)
{
    this->monsterHeightArray[i]=height;
}
void EnemyData::setActionTime(float val,int i)
{
    this->actionTime[i]=val;
}
float EnemyData::getActionTime(int i)
{
    return this->actionTime[i];
}
void EnemyData::setPositionMonster(CCPoint pos,int i)
{
    this->monPosition[i]=pos;
}
CCPoint EnemyData::getPositionMonster(int i)
{
    return this->monPosition[i];
}

現在可以在scene中添加npc了,說到就開始。

Global.h中添加如下定義:

CCMutableArray<Enemy*> *enemyArray;
void addMonster();

Global.cpp中實現:

CCMutableArray<CCStringToStringDictionary *> * all = data->getObjects();
    do
    {
        for (int i = 0 ; i <all->count(); i++)
        {
            CCStringToStringDictionary * temp = all->getObjectAtIndex(i);
            if(temp->objectForKey("name")->toStdString() == std::string("y"))
            {
                float x = temp->objectForKey("x")->toFloat();    
                float y = temp->objectForKey("y")->toFloat();
                CCVector2d* vector=new CCVector2d();
                vector->x=x;
                vector->y=5;
                monsterPositionArray->addObject(vector);
                Enemy* monster = Enemy::monster("yDing");
                monster->getMonsterSprite()->setPosition(ccp(x,5));
                map->addChild(monster,map->getChildren()->count());
                enemyArray->addObject(monster);//yDing
            }
}


HelloworldScene.cpp中的init()中添加如下

sGlobal->addMonster();


下一篇將會講解enemy的群集運動以及配合動畫,以及hero的碰撞檢測準備。

待續。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

long原創

               

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

平安县| 秭归县| 叶城县| 濉溪县| 兰溪市| 霍州市| 深圳市| 锦州市| 阳春市| 应用必备| 金塔县| 永嘉县| 柳州市| 札达县| 西乌珠穆沁旗| 红河县| 广平县| 洞头县| 化州市| 偃师市| 兰考县| 靖宇县| 寿阳县| 阿拉善右旗| 辽中县| 曲周县| 隆子县| 建始县| 沂南县| 文化| 祁阳县| 舞阳县| 二连浩特市| 井陉县| 中江县| 巴彦淖尔市| 千阳县| 张家港市| 宜兰市| 建瓯市| 通许县|