본문 바로가기
프로그래밍/cocos2d-x

cocos2d-x 간단한 액션 게임 예제2

by -현's- 2013. 6. 9.
반응형



AppDelegate.h


#ifndef __APP_DELEGATE_H__

#define __APP_DELEGATE_H__


#include "cocos2d.h"


class  AppDelegate : private cocos2d::CCApplication

{

public:

    AppDelegate();

    virtual ~AppDelegate();

    virtual bool applicationDidFinishLaunching();


    virtual void applicationDidEnterBackground();


    virtual void applicationWillEnterForeground();

};


#endif  // __APP_DELEGATE_H__

 





AppDelegate.cpp


#include "cocos2d.h"

#include "AppDelegate.h"

#include "LoadingLayer.h"


USING_NS_CC;


AppDelegate::AppDelegate()

{

}


AppDelegate::~AppDelegate()

{

}


bool AppDelegate::applicationDidFinishLaunching()

{

    // initialize director

    CCDirector *pDirector = CCDirector::sharedDirector();

    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());


    // turn on display FPS

    pDirector->setDisplayStats(false);


    // set FPS. the default value is 1.0/60 if you don't call this

    pDirector->setAnimationInterval(1.0 / 60);


    // create a scene. it's an autorelease object

    CCScene *pScene = LoadingLayer::scene();


    // run

    pDirector->runWithScene(pScene);

    return true;

}


// This function will be called when the app is inactive. When comes a phone call,it's be invoked too

void AppDelegate::applicationDidEnterBackground()

{

    CCDirector::sharedDirector()->stopAnimation();


}


// this function will be called when the app is active again

void AppDelegate::applicationWillEnterForeground()

{

    CCDirector::sharedDirector()->startAnimation();


}

 









LoadingLayer.h


#ifndef _LoadingLayer_

#define _LoadingLayer_


#include "cocos2d.h"


using namespace cocos2d;


class LoadingLayer:public cocos2d::CCLayer

{

public:

virtual bool init();


static cocos2d::CCScene* scene();


CREATE_FUNC(LoadingLayer);


//이후 변수와 메서드 추가 선언


CCSize winSize;


bool isLoading;

bool imagesLoaded;


CCSprite *defaultImage;

CCSprite *main_bkgrnd;

CCSprite *tapToCont;

CCSprite *loading;


void spritesLoaded();

void tick(float dt);

virtual void ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent* event);

};


#endif





LoadingLayer.cpp


#include "LoadingLayer.h"

#include "MainMenuLayer.h"


CCScene* LoadingLayer::scene()

{

    CCScene *scene = CCScene::create();

    

    LoadingLayer *layer = LoadingLayer::create();

    

    // 960 x 640

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    if (winSize.width >= 960) {

        layer->setScale(2.0);

        layer->setAnchorPoint(ccp(0.0, 0.0));

    }


    scene->addChild(layer);

    

    return scene;

}


bool LoadingLayer::init()

{

    if ( !CCLayer::init() )

    {

        return false;

    }

    

    /////////////////////////////

    

    // 변수 초기화

    isLoading = true;

    imagesLoaded = false;

    // 터치 활성화

    this->setTouchEnabled(true);


    // 화면 사이즈 구하기

    winSize = CCSizeMake(480, 320);


    // 디폴트 이미지 세로형 화면에 추가하기

    defaultImage = CCSprite::create("Images/loading_bg.png");

    defaultImage->setPosition(ccp(winSize.width/2, winSize.height/2));

    this->addChild(defaultImage);


LoadingLayer::spritesLoaded();

    // 주기적으로 상태체크

    this->schedule(schedule_selector(LoadingLayer::tick));

  

    return true;

}


void LoadingLayer::spritesLoaded()

{

CCLog("11");

    // 메인 백그라운드 이미지 추가

    main_bkgrnd = CCSprite::create("Images/loading_bg.png");

    main_bkgrnd->setPosition( ccp(winSize.width/2, winSize.height/2) );

    this->addChild(main_bkgrnd);

    

    // "Loading..." 글자 이미지 추가

    loading = CCSprite::create("Images/loading_tx.png");

    loading->setPosition( ccp(350,50));

    this->addChild(loading);


    

    // "Loading..." 글자 이미지 깜박이게 애니메이션

    CCFiniteTimeAction* loadingSeqAct = CCSequence::create(CCFadeOut::create(1.0f),

                                                           CCFadeIn::create(1.0f),

                                                           NULL);

    CCAction* rep2 = CCRepeatForever::create((CCActionInterval*)loadingSeqAct);

    loading->runAction(rep2);


    

    // 시작할 때 로딩한 디폴트 이미지를 지운다. (메모리 절약)

    this->removeChild(defaultImage, true);

    

    

    // 이미지가 다 로드되었음을 표시

    imagesLoaded = true;


}


void LoadingLayer::tick(float dt)

{

    

    // 이미지가 다 로드됨

    if (imagesLoaded && isLoading) {

        

        isLoading = false;

        

        // "로딩중 이미지"를 삭제. (메모리 절약)

        this->removeChild(loading, true);

        

        // continue 이미지 추가

        tapToCont = CCSprite::create("Images/continue_tx.png");

        tapToCont->setPosition( ccp(350,50 ));

        this->addChild(tapToCont);

       

        // continue 깜박거림

        // 로딩중 자리에서 대신 나오며 다음 신으로 갈 수 있도록 안내 표시.

        CCFiniteTimeAction* loadingSeqAct = CCSequence::create(CCFadeOut::create(1.0f),

                                                               CCFadeIn::create(1.0f),

                                                               NULL);

        CCAction* rep2 = CCRepeatForever::create((CCActionInterval*)loadingSeqAct);

        tapToCont->runAction(rep2);

    }

    

}


void LoadingLayer::ccTouchesEnded(CCSet *pTouches, CCEvent* event)

{

    if (!isLoading) {

        // 클릭하면 메인메뉴 신으로 이동

       CCScene* pScene = MainMenuLayer::scene();

        CCDirector::sharedDirector()->replaceScene( CCTransitionProgressRadialCW::create(0.5f, pScene) );

    }

}











MainMenuLayer.h


#ifndef __MainMenuLayer__H__

#define __MainMenuLayer__H__


#include "cocos2d.h"


using namespace cocos2d;


class MainMenuLayer : public cocos2d::CCLayerColor

{

public:

    virtual bool init();  


    static cocos2d::CCScene* scene();



    CREATE_FUNC(MainMenuLayer);


void MainMenuLayer::newGameStart(CCObject* sender);



};

#endif  // __MainMenuLayer__H__





MainMenuLayer.cpp


#include "MainMenuLayer.h"

#include "StageSelect.h"


CCScene* MainMenuLayer::scene()

{

    CCScene *scene = CCScene::create();

    

    MainMenuLayer *layer = MainMenuLayer::create();

    

    // 960 x 640

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    if (winSize.width >= 960) {

        layer->setScale(2.0);

        layer->setAnchorPoint(ccp(0.0, 0.0));

    }


    scene->addChild(layer);

    

    return scene;

}


bool MainMenuLayer::init()

{

    if ( !CCLayerColor::initWithColor(ccc4(255,255,255,255)) )

    {

        return false;

    }

    

    /////////////////////////////

CCSize winSize=CCSizeMake(480, 320);


CCMenuItemFont *newGame=CCMenuItemFont::create("newGame", this, menu_selector(MainMenuLayer::newGameStart));

newGame->setColor(ccc3(0,0,0));

CCMenu* menu=CCMenu::create(newGame,NULL);

menu->setPosition(ccp(winSize.width/2, winSize.height/2));

this->addChild(menu);


    return true;

}


void MainMenuLayer::newGameStart(CCObject* sender)

{

CCScene* pScene=StageSelect::scene();

CCDirector::sharedDirector()->replaceScene(pScene);



}









●StageSelect.h


#ifndef __StageSelect__H__

#define __StageSelect__H__


#include "cocos2d.h"


using namespace cocos2d;


class StageSelect : public cocos2d::CCLayerColor

{

public:

    virtual bool init();  


    static cocos2d::CCScene* scene();


    CREATE_FUNC(StageSelect);

void StageSelect::doClick(CCObject* pSender);

void StageSelect::doClickX(CCObject* pSender);

bool stage2;

};

#endif  // __StageSelect__H__




●StageSelect.cpp


#include "StageSelect.h"

#include "MainMenuLayer.h"

#include "StageIdx.h"

#include "Stage1Layer.h"

#include "Stage2Layer.h"



CCScene* StageSelect::scene()

{

    CCScene *scene = CCScene::create();

    

    StageSelect *layer = StageSelect::create();

    

    // 960 x 640

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    if (winSize.width >= 960) {

        layer->setScale(2.0);

        layer->setAnchorPoint(ccp(0.0, 0.0));

    }


    scene->addChild(layer);

    

    return scene;

}


bool StageSelect::init()

{

    if ( !CCLayerColor::initWithColor(ccc4(255,255,255,255)) )

    {

        return false;

    }

    

    /////////////////////////////


stage2 = StageIdx::getInstance()->getStage2Bool();

//stage1

CCMenuItemFont *item1=CCMenuItemFont::create("Stage1 O", this, menu_selector(StageSelect::doClick ));

item1->setColor(ccc3(0,0,0));


//stage2

std::string a;

if(stage2)

{

a="Stage2 O ";

}else{

a="String2 X ";

}

CCMenuItemFont *item2=CCMenuItemFont::create(a.c_str(), this, menu_selector(StageSelect::doClick ));

item2->setColor(ccc3(0,0,0));

/*

//stage2

if(stage2)

{

CCMenuItemFont *item2=CCMenuItemFont::create("Stage2 O", this, menu_selector(StageSelect::doClick ));

item2->setColor(ccc3(0,0,0));

}else{

CCMenuItemFont *item2=CCMenuItemFont::create("Stage2 X", this, menu_selector(StageSelect::doClickX ));

item2->setColor(ccc3(0,0,0));

}

*/

//stage3_MainMenu

CCMenuItemFont *item3=CCMenuItemFont::create("Menu", this, menu_selector(StageSelect::doClick ));

item3->setColor(ccc3(0,0,0));

CCMenu* pMenu=CCMenu::create(item1, item2, item3, NULL);

item1->setTag(1);

item2->setTag(2);

item3->setTag(3);


pMenu->alignItemsVertically();

this->addChild(pMenu);

//bool값 테스트

if(stage2)

{

CCLog("Stage2 O 11");

}else{

CCLog("Stage2 X  11111");

}


    return true;

}

//bool 값 


void StageSelect::doClick(CCObject* pSender){


//bool값 테스트

if(stage2)

{

CCLog("Stage2 O 2222");

}else{

CCLog("Stage2 X  2222");

}


CCMenuItem *titem=(CCMenuItem*)pSender;

int i=titem->getTag();


//bool 테스트

if(stage2)

{

CCLog("Stage2 O 3333");

}else{

CCLog("Stage2 X  3333");

}


switch(i){

case 1:

{

CCScene* pScenes=Stage1Layer::scene();

CCDirector::sharedDirector()->replaceScene(pScenes);

break;

}

case 2:

{

if(stage2){

CCScene* pScenes=Stage2Layer::scene();

CCDirector::sharedDirector()->replaceScene(pScenes);

CCLog("stage2 o rrrr");

break;


}else{

CCLog("stage2 x rrrr");

break;

}

}


case 3:

{

CCScene* pScene=MainMenuLayer::scene();

CCDirector::sharedDirector()->replaceScene(pScene);

break;

}


}//switch end




}


void StageSelect::doClickX(CCObject* pSender){


}











반응형

댓글