cocos2d - x 개발 의 액 션 게임 실전 - 1

이 인 스 턴 스 는 cocos2d - x - 12 버 전 을 기반 으로 개발 되 었 습 니 다. 좀 오래 되 었 지만 괜 찮 습 니 다.
     디자인 모델 은 단일 모델 을 사용 하고 개발 환경 은 vs 2010 이 며 후기 에 안 드 로 이 드 에 어떻게 이식 하 는 지 쓸 것 입 니 다. 시작 할 수 있 습 니 다.
     단일 모드 의 코드 를 복사 해 오 세 요.
#ifndef __SINGLETON_H__//40
#define __SINGLETON_H__
/*
    
*/
template 
class Singleton
{
public:
    inline static T* getInstance();
    inline static void release();
private:
    static T* t;
};
template 
inline T* Singleton::getInstance()
{
    if (!t)
    {
        t = new T;
    }
    return t;
}
template
inline void Singleton::release()
{
    if (t)
    {
        delete t;
        t = 0;
    }
}
template 
T* Singleton::t = 0;
#endif // __SINGLE_H__

Hello World 프로젝트 에 추가 합 니 다. 새 Global. h 파일 과 Animation. h 파일 부분 코드 는 다음 과 같 습 니 다.
#ifndef _GLOBAL_H_//143
#define _GLOBAL_H_
#include"cocos2d.h"
#include"core/Singleton.h"
#include"DBgame.h"
#include"core/boy.h"
#include"core\boss.h"
#include"Core\typedefine.h"
#define MaxBuffer 100
using namespace cocos2d;
class Global:public Singleton
{
public:
    Global(void);
    ~Global(void);
    bool init();
};
#define sGlobal Global::getInstance()
#endif//_GLOBAL_

Animation.h
#ifndef _ANIMATION_H_//41
#define _ANIMATION_H_
#include"core/boy.h"
#include "cocos2d.h"
#include "core/Singleton.h"
struct  AnimationFormation
{
    char *animateName;//   :      
    int frameNum;//  
    Behaviour behaviour;//  
    Direction direction;//  
};
extern AnimationFormation heroAnimation[12];
extern AnimationFormation bombAnimation[2];
extern AnimationFormation monsterAnimation[3];
extern AnimationFormation monster[3];
extern AnimationFormation pDing[3];
extern AnimationFormation wing[4];
extern AnimationFormation box[4];
extern AnimationFormation portal[2];
extern AnimationFormation cube[2];
extern AnimationFormation moonk[8];
extern AnimationFormation toto_weapon[11];
class AnimationManager:public Singleton
{
public:
    bool loadAnimation(AnimationFormation *af,int count);
    bool loadObjAnimation(AnimationFormation *af,int count);
    bool loadWeaponAnimation(AnimationFormation *af,int count);
    bool loadMoonkAnimation(AnimationFormation *af,int count);
    cocos2d::CCAnimate* getAnimate(char *name,Behaviour behaviour,Direction direction);
    cocos2d::CCAnimation* getAnimation(char *name,Behaviour behaviour,Direction direction);
    int getDirection(Direction direction);
    char* getDirectionName(Direction direction);
    char* getBehaviour(Behaviour behaviour);
private:
    char *getAnimationName(char *name,Behaviour behaviour,Direction direction);
};
#define sAnimation AnimationManager::getInstance()
#endif//

현재 HelloWorld. cpp 파일 을 열 고 다음 코드 를 추가 합 니 다.
주로 제어 부분
CCMenuItemImage *pBtn_Left = CCMenuItemImage::itemFromNormalImage(
            btn_left_d,
            btn_left_u
            );
        CCMenuItemImage *pBtn_Right = CCMenuItemImage::itemFromNormalImage(
            btn_right_d,
            btn_right_u
            );
                                                                                                                              
        CCMenuItemImage *pBtn_Jump = CCMenuItemImage::itemFromNormalImage(
            jump_btn_d,
            jump_btn_u
            );
        CCMenuItemImage *pBtn_Attack = CCMenuItemImage::itemFromNormalImage(
            attack_btn_d,
            attack_btn_u
            );
        CCMenu* pControlBtn = CCMenu::menuWithItems(pBtn_Left,pBtn_Right,pBtn_Attack, NULL);
        pBtn_Left->setPosition(ccp(50*(size.width/480),35*(size.height/320)));
        pBtn_Left->setScaleX(size.width/480);
        pBtn_Left->setScaleY(size.height/320);
        pBtn_Right->setPosition(ccp(150*(size.width/480),35*(size.height/320)));//50 150 435 325
        pBtn_Right->setScaleX(size.width/480);
        pBtn_Right->setScaleY(size.height/320);
        pBtn_Jump->setPosition(ccp(435*(size.width/480),40*(size.height/320)));//1174.5
        pBtn_Jump->setScaleX(size.width/480);
        pBtn_Jump->setScaleY(size.height/320);
        pBtn_Attack->setPosition(ccp(325*(size.width/480),40*(size.height/320)));//877.5
        pBtn_Attack->setScaleX(size.width/480);
        pBtn_Attack->setScaleY(size.height/320);
        CCMenu* jumpBtn = CCMenu::menuWithItems(pBtn_Jump,NULL);
        jumpBtn->setPosition(CCPointZero);
        pControlBtn->setPosition(CCPointZero);
        this->addChild(jumpBtn,1);
        this->addChild(pControlBtn,1);

조심 하 세 요, pBtnJump 는 다른 버튼 과 동시에 눌 릴 수 있 도록 단독으로 불 러 옵 니 다.setScale () 함수 확대 CCNode 포함 (CCSprite, 등).
그리고 지 도 를 불 러 오기 시작 합 니 다.
int lvl = CCUserDefault::sharedUserDefault()->getIntegerForKey("lvl");
        if(lvl == 0)
        {
            lvl = 1;
            CCUserDefault::sharedUserDefault()->setIntegerForKey("lvl",1);
        }
                                                                                              
//---------------------------------2013.3.4---------------------------------
        //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);

Animation. cpp 부분 코드
#include "core/Animation.h"//386
#include"DBgame.h"
using namespace cocos2d;
static char charBuffer[128];
AnimationFormation heroAnimation[12]=
{
    {"toto",1,STAND,LEFT},
    {"toto",1,STAND,RIGHT},
    {"toto",1,STAND,OVERLOOK},
    {"toto",2,MOVE,LEFT},
    {"toto",2,MOVE,RIGHT},
    {"toto",3,MOVE,JUMP},/*      3       */
    {"toto",5,MOVE,ATTACK},
    {"toto",4,MOVE,JUMPATTACK},
    {"toto",6,DEAD,OVERLOOK},
    {"toto",2,MOVE,ATTACKED},
    {"toto",4,MOVE,FIRESTAND},
    {"toto",4,MOVE,ICESTAND},
};
bool AnimationManager::loadAnimation(AnimationFormation *af,int count)
{
    //  ——       png,    SpriteFrame,        
    memset(charBuffer,0,sizeof(charBuffer));
    //sprintf(charBuffer,"objectTexture/16bit/4444-%sYPding.plist",af[0].animateName);
                                                                                   
    //CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(charBuffer);
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(totoYPdingPlist);
    //      
    CCMutableArray *spriteFrames = new CCMutableArray();
    for (int i=0;i *picArray = new CCMutableArray();
        for(int j=getDirection(af[i].direction);jm_sString=charBuffer;
            picArray->addObject(picName);
            //printf("------AnimationPicture:    %s
",charBuffer); //CCLOG("------AnimationPicture: %s",charBuffer); //FrameCache CCSpriteFrame *spriteFrame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(charBuffer); spriteFrames->addObject(spriteFrame); } // cache CCAnimation *animation=CCAnimation::animationWithFrames(spriteFrames,0.1f); memset(charBuffer,0,sizeof(charBuffer)); sprintf(charBuffer,"%s_%s%s",af[i].animateName,getBehaviour(af[i].behaviour),getDirectionName(af[i].direction)); //CCLOG("AnimationName: %s
",charBuffer); //parse plist CCPlistParseCache::sharedPlistParseCache()->addPictureArrayWithAnimationName(charBuffer,picArray); CCAnimationCache::sharedAnimationCache()->addAnimation(animation,charBuffer); spriteFrames->removeAllObjects(); } spriteFrames->release(); return true; } // animation frame(51 ) 1-5 6-12 13-15 cocos2d::CCAnimate* AnimationManager::getAnimate(char *name,Behaviour behaviour,Direction direction ) { CCAnimation* animation=CCAnimationCache::sharedAnimationCache()->animationByName(getAnimationName(name,behaviour,direction)); if(animation) { return cocos2d::CCAnimate::actionWithAnimation(animation); } return NULL; } CCAnimation* AnimationManager::getAnimation(char *name,Behaviour behaviour,Direction direction) { CCAnimation* animation=CCAnimationCache::sharedAnimationCache()->animationByName(getAnimationName(name,behaviour,direction)); if(animation) { return animation; } return NULL; } char* AnimationManager::getAnimationName(char *name,Behaviour behaviour,Direction direction ) { memset(charBuffer,0,sizeof(charBuffer)); sprintf(charBuffer,"%s_%s%s",name,getBehaviour(behaviour),getDirectionName(direction)); return charBuffer; } char* AnimationManager::getBehaviour( Behaviour behaviour ) { switch(behaviour) { case STAND: return "stand"; case MOVE: return "move"; case DEAD: return "dead"; } } char* AnimationManager::getDirectionName(Direction direction) { switch(direction) { case JUMP: return "jump"; case ATTACK: return "attack"; case JUMPATTACK: return "jumpattack"; case LEFT: return "left"; case RIGHT: return "right"; case MOVERLOOK: return "moverlook"; case MATTACK: return "mattack"; case OVERLOOK: return "overlook"; case ATTACKED: return "attacked"; case BOVERLOOK: return "boverlook"; case BONE: return "bone"; case BTWO: return "btwo"; case BTHREE: return "bthree"; case REMOVE: return "remove"; case PORTALONE: return "portalone"; case PORTALTWO: return "portaltwo"; case CUBEONE: return "cubeone"; case CUBETWO: return "cubetwo"; case FIRESTAND: return "firestand"; case FIRELEFT: return "fireleft"; case FIRERIGHT: return "fireright"; case ICERIGHT: return "iceright"; case ICELEFT: return "iceleft"; case ICESTAND: return "icestand"; case FIREEXPODE: return "fireexpode"; case SWORDRIGHT: return "swordright"; case SWORDLEFT: return "swordleft"; case SWORDSTAND: return "swordstand"; case SWORDATTACK: return "swordattack"; case WRIGHT: return "wright"; case WLEFT: return "wleft"; case WATTACK: return "wattack"; case WREMOVE: return "wremove"; case BO×××IGHT: return "bo***ight"; case BOSSLEFT: return "bossleft"; case BOSSATTACK: return "bossattack"; case BOSSATTACKONE: return "bossattackone"; case BOSSATTACKTWO: return "bossattacktwo"; case BOSSATTACKTHREE: return "bossattackthree"; case BO×××EMOVE: return "bo***emove"; case BOSSSTAND: return "bossstand"; } } int AnimationManager::getDirection( Direction direction ) { switch(direction) { case JUMP: return 36;/*35*/ case ATTACK: return 14; case JUMPATTACK: return 24; case LEFT: return 2; case RIGHT: return 2; case MOVERLOOK: return 2; case MATTACK: return 6; case OVERLOOK: return 1; case ATTACKED: return 39; case BOVERLOOK: return 5; case BONE: return 4; case BTWO: return 2; case BTHREE: return 1; case REMOVE: return 11; case PORTALONE: return 0; case PORTALTWO: return 3; case CUBEONE: return 0; case CUBETWO: return 3; case FIRESTAND: return 52; case ICESTAND: return 56; case FIRELEFT: return 0; case FIRERIGHT: return 0; case ICERIGHT: return 0; case ICELEFT: return 0; case FIREEXPODE: return 4; case SWORDSTAND: return 0; case SWORDRIGHT: return 0; case SWORDLEFT: return 0; case SWORDATTACK: return 4; case WRIGHT: return 0; case WLEFT: return 0; case WATTACK: return 2; case WREMOVE: return 11; case BO×××IGHT: return 4; case BOSSLEFT: return 4; case BOSSATTACK: return 8; case BOSSATTACKONE: return 10; case BOSSATTACKTWO: return 25; case BOSSATTACKTHREE: return 27;//27 13 case BO×××EMOVE: return 52; case BOSSSTAND: return 0; //0-8 8-16 16-24 24-32 //default: //return 0; } }

그리고 Hello World. cpp 파일 intit () 함수 에 추가 합 니 다.
AnimationManager::getInstance()->loadAnimation(heroAnimation,12);

다음은 HERO 의 실현.
boy.h
#ifndef _BOY_H_
#define _BOY_H_
#include"Common\BoundingBox.h"
#include"cocos2d.h"
#include"DBgame.h"
#include"Core\typedefine.h"
using namespace cocos2d;
class Boy:public CCNode
{
public:
    Boy(void);
    ~Boy(void);
    //        hero  
    static Boy* player();
    void setAnimation(Direction dir,Behaviour be);
    void setAnimation(Direction dir,Behaviour be,float time);
    Direction getDirection(); 
    Behaviour getBehaviour();
    void attack();
    void attackDone();
    void executeAnimation(Direction dir,Behaviour be,int nextAni);
    void executeDone(CCNode* pSender,void* i);
    void attacked();
    void attackedDone();
    BoundingBox hitBox;
    BoundingBox attackBox;
    CCRect getRect();
    CCRect getCollisionRect(int width,int height);
    Behaviour behaviour;
    Direction direction;
    CCSprite* getHeroSprite();
protected:
    CCAnimate *getAnimate();
    CCSprite *sprite;
    float xVel;
    float yVel;
    CCPoint touchPoint;
    //CCAnimate *getAnimate(char* playName);
    bool init();
};
#endif//

boy. cpp 의 실현
#include"core\boy.h"//215
#include"DBgame.h"
#include"core\Animation.h"
using namespace cocos2d;
Boy::Boy()
{
    xVel=0;
    yVel=0;
}
Boy::~Boy()
{
}
Boy* Boy::player()
{
    Boy *player = new Boy();
    if (player && player->init())
    {
        player->autorelease();
        return player;
    }
    CC_SAFE_DELETE(player);
    return NULL;
}
bool Boy::init()
{
    bool bRet = false;
    do{
        this->setAnchorPoint(CCPointZero);
        //    
        sprite=CCSprite::spriteWithSpriteFrameName("toto_0.png");
        sprite->setAnchorPoint(CCPointZero);
        this->addChild(sprite);
        //    
        behaviour=STAND;
        direction=OVERLOOK;
        //
        //behaviour=MOVE;
        //direction=ATTACKED;
                                                           
        sprite->runAction(CCRepeatForever::actionWithAction(getAnimate()));
                                                           
        bRet=true;
    }while(0);
    return bRet;
}
CCSprite* Boy::getHeroSprite()
{
    return this->sprite;
}
void Boy::setAnimation(Direction dir,Behaviour be)
{
    direction=dir;
    behaviour=be;
    sprite->stopAllActions();
    sprite->runAction(CCRepeatForever::actionWithAction(getAnimate()));
}
void Boy::setAnimation(Direction dir,Behaviour be,float time)
{
    CCAction* action=CCSequence::actions(
        getAnimate(),
        CCDelayTime::actionWithDuration(0.001f),
        NULL);
    //    
    sprite->runAction(action);
}
//  ,    ,        ,      OVERLOOK ,           
CCAnimate * Boy::getAnimate()
{
    if (behaviour==DEAD)
    {
        direction=OVERLOOK;
    }
    return AnimationManager::getInstance()->getAnimate("toto",behaviour,direction);
}
Direction Boy::getDirection()
{
    return this->direction;
}
Behaviour Boy::getBehaviour()
{
    return this->behaviour;
}

매 거 정의 코드
#ifndef _TYPEDEFINE_H
#define _TYPEDEFINE_H
typedef enum {
    UP,
    DOWN,
    JUMP,
    ATTACK,
    JUMPATTACK,
    LEFT,
    RIGHT,
    MOVERLOOK,
    MATTACK,
    OVERLOOK, //       
    ATTACKED,
    BOVERLOOK,
    BONE,
    BTWO,
    BTHREE,
    REMOVE,
    PORTALONE,
    PORTALTWO,
    CUBEONE,
    CUBETWO,
    FIRERIGHT,
    FIRELEFT,
    FIRESTAND,
    ICERIGHT,
    ICELEFT,
    ICESTAND,
    FIREEXPODE,
    SWORDRIGHT,
    SWORDSTAND,
    SWORDLEFT,
    SWORDATTACK,
    WRIGHT,
    WLEFT,
    WATTACK,
    WREMOVE,
    BO×××IGHT,
    BOSSLEFT,
    BO×××EMOVE,
    BOSSATTACK,
    BOSSATTACKONE,
    BOSSATTACKTWO,
    BOSSATTACKTHREE,
    BOSSSTAND
}  Direction;
typedef enum{
    STAND,
    MOVE,
    DEAD
} Behaviour;
typedef enum{
    GETFIRE,
    GETICE,
    GETSWORD,
    GETHAM,
    GETHP,
    NOSTAGE
}STAGE;
#endif;

HelloWord 에서 init () 함수 에 다음 코드 추가
player=Boy::player();
player->getHeroSprite()->setPosition(ccp(100,10));
map->addChild(player,1);
sprite=player->getHeroSprite();

계속 ----------------------------------------------

좋은 웹페이지 즐겨찾기