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

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

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



StageIdx.h


#ifndef __StageIdx__H__

#define __StageIdx__H__


#include "cocos2d.h"

//싱글톤 클래스

using namespace cocos2d;


class StageIdx : public cocos2d::CCLayer

{

public:

static StageIdx *getInstance();

void setStage2true();

bool getStage2Bool();


private:

StageIdx();

~StageIdx();

};


#endif  // __StageIdx__H__




StageIdx.cpp


#include "StageIdx.h"


StageIdx::StageIdx(void)

{

CCUserDefault::sharedUserDefault()->setBoolForKey("stage2",false);

CCUserDefault::sharedUserDefault()->flush();

}


StageIdx::~StageIdx(void)

{

}


static StageIdx *gameData;


StageIdx* StageIdx::getInstance()

{

if(!gameData)

{

gameData=new StageIdx();

}

return gameData;

}


bool StageIdx::getStage2Bool()

{

bool stage2= CCUserDefault::sharedUserDefault()->getBoolForKey("stage2");

return stage2;

}


void StageIdx::setStage2true(){

CCUserDefault::sharedUserDefault()->setBoolForKey("stage2",true);

CCUserDefault::sharedUserDefault()->flush();

}










Stage1Layer.h


#ifndef __Stage1Layer__H__

#define __Stage1Layer__H__


#include "cocos2d.h"


using namespace cocos2d;


class Stage1Layer : public cocos2d::CCLayer

{

public:

    virtual bool init();  


    static cocos2d::CCScene* scene();


    CREATE_FUNC(Stage1Layer);


bool jumpYN;


CCSprite *abc;


int score;

//void Stage1Layer::gameMenuStart(CCObject* sender);

void Stage1Layer::createDragon();


CCSprite* dragon;


void Stage1Layer::createBackground();

~Stage1Layer();

CCArray* stoneArray;

CCArray* coinArray;


CCLabelTTF* pLabel;


char coinScore[100];

void Stage1Layer::stoneCollision(float dt);

void Stage1Layer::coinCollision(float dt);


void Stage1Layer::winGame();


CCAction* rep;

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

};


#endif  // __Stage1Layer__H__




Stage1Layer.cpp


#include "Stage1Layer.h"

#include "StageIdx.h"

#include "WinLayer.h"

#include "LoseLayer.h"



CCScene* Stage1Layer::scene()

{

    CCScene *scene = CCScene::create();

    

    Stage1Layer *layer = Stage1Layer::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 Stage1Layer::init()

{

    if ( !CCLayer::init() )

    {

        return false;

    }

    

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

char coinScore[100] = {0};

score=0;

this->setTouchEnabled(true);

stoneArray=CCArray::createWithCapacity(4);

stoneArray->retain();


coinArray=CCArray::createWithCapacity(4);

coinArray->retain();

jumpYN=false;

CCTexture2D* texture1;

texture1=CCTextureCache::sharedTextureCache()->addImage("Images/white-512x512.png"); 

abc=CCSprite::createWithTexture(texture1, CCRectMake(0,0,20,10));

abc->setAnchorPoint(ccp(0,0));

abc->setColor(ccc3(255,255,0));

abc->setOpacity(255);

CCActionInterval* gogo = CCMoveBy::create(10, ccp(700, 0));

abc->runAction(gogo);



this->addChild(abc, 11);

this->createDragon();          //드래곤을 추가한다.

this->createBackground(); //배경 추가




this->schedule(schedule_selector(Stage1Layer::stoneCollision), 2.0f/60.0f);

this->schedule(schedule_selector(Stage1Layer::coinCollision), 2.0f/60.0f);



//레이블 생성 및 초기화

pLabel=CCLabelTTF::create("score:0", "Thonburi", 30);

       //레이블 내용은 HelloWorld이고 폰트는 Thonburi, 크기는 20이다.

//레이블 위치 지정

        pLabel->setPosition(ccp(90,50));

//레이블 색 지정

pLabel->setColor(ccc3(255,255,255));

        //흰색( ccc(255,255,255) )이 기본색이다. 

//레이블의 투명도 지정

pLabel->setOpacity(100.0);

//레이어에 레이블 객체 추가

this->addChild(pLabel, 12);


    return true;

}


Stage1Layer::~Stage1Layer()

{

stoneArray->release();

coinArray->release();

}



void Stage1Layer::stoneCollision(float dt){

CCObject *Obj;

CCLog("stoneCollisition");

CCARRAY_FOREACH(stoneArray, Obj){

CCSprite *stone=(CCSprite*)Obj;

if(255 == stone->getOpacity()){

int abcX = abc->getPositionX();

CCLog("stoneCollisition22222");

CCRect rectA=dragon->boundingBox();

int aa=rectA.getMinX();

int bb=rectA.getMinY();

CCLog("stoneCollisitionaaaa %d %d",aa,bb);

CCRect rectB=stone->boundingBox();

CCLog("stoneCollisitionbbbb");

int cc=rectB.getMinX()-abcX;

int dd=rectB.getMinY();

rectA.setRect(aa+30,bb,100,140);

rectB.setRect(cc,dd,20,10);

CCLog("stoneCollisitioncdcdcd %d %d",cc,dd);

if(rectA.intersectsRect(rectB)){

CCLog("stoneCollisition33333");

this->unscheduleAllSelectors();

CCScene* pScene=LoseLayer::scene();

CCDirector::sharedDirector()->replaceScene(pScene);//기존장면 없애고 새장면 교체

}

}

}

CCLog("stoneCollisition444");

}



void Stage1Layer::coinCollision(float dt){

CCObject *Obj;

CCLog("coinCollisition");

CCARRAY_FOREACH(coinArray, Obj){

CCSprite *coin=(CCSprite*)Obj;

if(255 == coin->getOpacity()){

int abcX = abc->getPositionX();

CCLog("coinCollisition22222");

CCRect rectA=dragon->boundingBox();

int aa=rectA.getMinX();

int bb=rectA.getMinY();

CCLog("coinCollisitionaaaa %d %d",aa,bb);

CCRect rectB=coin->boundingBox();

CCLog("coinCollisitionbbbb");

int cc=rectB.getMinX()-abcX;

int dd=rectB.getMinY();

rectA.setRect(aa+30,bb,100,140);

rectB.setRect(cc,dd,10,10);

CCLog("coinCollisitioncdcdcd %d %d",cc,dd);

if(rectA.intersectsRect(rectB)){

CCLog("coinCollisition33333");

score++;

sprintf(coinScore, "score: %d",score);

coin->setOpacity(0);

pLabel->setString(coinScore);

CCLog("Score %d",score);


}

}

}

CCLog("stoneCollisition444");

}



void Stage1Layer::createBackground()

{

//배경 스프라이트1

CCSprite* background1 = CCSprite::create("Images/game_background1.jpg");

background1->setAnchorPoint(ccp(0,0));

//배경 스프라이트2

CCSprite* background2 = CCSprite::create("Images/game_background2.jpg");

background2->setAnchorPoint(ccp(0,0));


//패럴랙스노드를 만든다.

CCParallaxNode* voidNode = CCParallaxNode::create();


//배경 스트라이트를 패럴랙스노드에 넣는다.

voidNode->addChild(background1, 1, ccp(1.0f, 0.0f), ccp(0,0));

voidNode->addChild(background2, 1, ccp(1.0f, 0.0f), ccp(480,0));


//장애물 추가

CCTexture2D* texture;

texture=CCTextureCache::sharedTextureCache()->addImage("Images/white-512x512.png");

for(int i=1; i<4;i++)

{

CCSprite* stone=CCSprite::createWithTexture(texture, CCRectMake(0,0,20,10));

stone->setAnchorPoint(ccp(0,0));

stone->setColor(ccc3(75,255,0));

voidNode->addChild(stone, 1, ccp(1.0f, 0.0f), ccp(i*250, 200));

stoneArray->addObject(stone);


}

//코인 추가

for(int i=1; i<4;i++)

{

CCSprite* coin=CCSprite::createWithTexture(texture, CCRectMake(0,0,20,10));

coin->setAnchorPoint(ccp(0,0));

coin->setColor(ccc3(255,255,0));

voidNode->addChild(coin, 1, ccp(1.0f, 0.0f), ccp(i*250, 300));

coinArray->addObject(coin);


}

//액션

CCActionInterval* go = CCMoveBy::create(10, ccp(-700, 0));

CCFiniteTimeAction* seq = CCSequence::create(go,

CCCallFunc::create(this, callfunc_selector(Stage1Layer::winGame)),   //끝까지 이동하면 winGame로 이동

NULL);


voidNode->runAction(seq);

this->addChild(voidNode);

}



void Stage1Layer::winGame(){

//winGame화면으로 이동

StageIdx::getInstance()->setStage2true();    //stage2를 열수있게 true로 바꾼다.

CCScene* pScene=WinLayer::scene();

CCDirector::sharedDirector()->replaceScene(pScene);//기존장면 없애고 새장면 교체

}








void Stage1Layer::createDragon()

{ //움직이는 용 추가

jumpYN=false;

CCTexture2D *texture=CCTextureCache::sharedTextureCache()->addImage("Images/dragon_animation.png");

CCAnimation *animation=CCAnimation::create();

animation->setDelayPerUnit(0.1);


for(int i=0; i<6; i++)

{

int index = i%4;

int rowIndex=i/4;


animation->addSpriteFrameWithTexture(texture, CCRectMake(index*130, rowIndex*140, 130, 140));

}

//스프라이트 생성

dragon=CCSprite::createWithTexture(texture, CCRectMake(0,0,130,140));

dragon->setPosition(ccp(100,240));

this->addChild(dragon,10);

dragon->setFlipX(true);


CCAnimate *animate=CCAnimate::create(animation);

CCAction* rep=CCRepeatForever::create(animate);

dragon->runAction(rep);

}


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

{

if(!jumpYN){          //현재 점프중이 아니면 해당 if문 실행

jumpYN=true;      //jumpYN을 true로 바꿔줌.


CCLog("TOUCH");


//추가

this->removeChild(dragon, true);  //

//추가end

//점프하면서 움직이는 용 추가

CCTexture2D *texture=CCTextureCache::sharedTextureCache()->addImage("Images/dragon_animation.png");

CCAnimation *animation=CCAnimation::create();

animation->setDelayPerUnit(0.1);


for(int i=0; i<6; i++)

{

int index = i%4;

int rowIndex=i/4;


animation->addSpriteFrameWithTexture(texture, CCRectMake(index*130, rowIndex*140, 130, 140));

}

//스프라이트 생성

dragon=CCSprite::createWithTexture(texture, CCRectMake(0,0,130,140));

dragon->setPosition(ccp(100,240));

this->addChild(dragon);

dragon->setFlipX(true);


CCAnimate *animate=CCAnimate::create(animation);

CCAction* rep=CCRepeatForever::create(animate);

CCActionInterval* jump=CCJumpBy::create(2.5, ccp(0,0),110, 1 );

CCFiniteTimeAction* spw=CCSpawn::create(jump,rep, NULL);

CCFiniteTimeAction* seq=CCSequence::create(spw,  

CCCallFuncND::create(this, callfuncND_selector(CCNode::removeChild), dragon), 

CCCallFunc::create(this, callfunc_selector(Stage1Layer::createDragon)), 

NULL);

dragon->runAction(seq);

}else{

//jumpYN이 true면 점프 실행 안함

}

}










●Stage2Layer - Stage1Layer 이랑 비슷함








WinLayer.h


#ifndef __WinLayer__H__

#define __WinLayer__H__


#include "cocos2d.h"


using namespace cocos2d;


class WinLayer : public cocos2d::CCLayerColor

{

public:

    virtual bool init();  


    static cocos2d::CCScene* scene();



    CREATE_FUNC(WinLayer);

void WinLayer::mainMenuGo(CCObject* sender);



};


#endif  // __WinLayer__H__




WinLayer.cpp


#include "WinLayer.h"

#include "MainMenuLayer.h"


CCScene* WinLayer::scene()

{

    CCScene *scene = CCScene::create();

    

    WinLayer *layer = WinLayer::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 WinLayer::init()

{

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

    {

        return false;

    }

    

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

CCMenuItemFont *newGame=CCMenuItemFont::create("Win!!! Mene go", this, menu_selector(WinLayer::mainMenuGo));

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

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

menu->setPosition(ccp(240,180));

this->addChild(menu);


    return true;

}



void WinLayer::mainMenuGo(CCObject* sender)

{

CCScene* pScene=MainMenuLayer::scene();

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

}










●LoseLayer.h


#ifndef __LoseLayer__H__

#define __LoseLayer__H__


#include "cocos2d.h"


using namespace cocos2d;


class LoseLayer : public cocos2d::CCLayerColor

{

public:

    virtual bool init();  


    static cocos2d::CCScene* scene();



    CREATE_FUNC(LoseLayer);

void LoseLayer::mainMenuGo(CCObject* sender);



};


#endif  // __LoseLayer__H__





●LoseLayer.cpp


#include "LoseLayer.h"

#include "MainMenuLayer.h"


CCScene* LoseLayer::scene()

{

    CCScene *scene = CCScene::create();

    

    LoseLayer *layer = LoseLayer::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 LoseLayer::init()

{

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

    {

        return false;

    }

    

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

CCMenuItemFont *newGame=CCMenuItemFont::create("Lose...Mene go", this, menu_selector(LoseLayer::mainMenuGo));

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

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

menu->setPosition(ccp(240,180));

this->addChild(menu);


    return true;

}



void LoseLayer::mainMenuGo(CCObject* sender)

{

CCScene* pScene=MainMenuLayer::scene();

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



}











반응형

댓글