-
cocos2d-x 프로젝트 1Pause @/Cocos2d-x2 2013. 7. 3. 23:15
#include "GameScene.h"
#include "MenuScene.h"
#include "SimpleAudioEngine.h"using namespace cocos2d;
using namespace CocosDenshion;CCScene* Game::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
Game *layer = Game::create();// add layer as a child to scene
scene->addChild(layer);// return the scene
return scene;
}// on "init" you need to initialize your instance
bool Game::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
initData();
initBG();
initCard();
initLabel();
initReady();
initStart();
initO();
initX();
initGameover();
initGameclear();
initMenu();
actionReady();
return true;
}void Game::initData()
{
winSize = CCDirector::sharedDirector()->getWinSize();
nCard = 0;
nGoal = 4;
nLife = 3;
srand(time(NULL));
for (int i=0; i<4; i++) {
Card[i] = rand()%6;
}
CCLog("CARD : %d %d %d %d", Card[0], Card[1], Card[2], Card[3]);nSelectCard = -1;
nCountCard = 0;
}void Game::initBG()
{
CCSprite *pSpr = CCSprite::create("game-bg.png");
pSpr->setPosition(ccp(winSize.width/2, winSize.height/2));
this->addChild(pSpr);
}void Game::initCard()
{
for (int i=0; i<6; i++) {
CCSprite *pSpr_Back = CCSprite::create("card-back.png");
pSpr_Back->setPosition(ccp(winSize.width/2-60+60*2*(i%2), winSize.height/2+120-120*(i/2)));
pSpr_Back->setTag(TAG_CARD_BACK+i);
this->addChild(pSpr_Back);
char buf[64];
sprintf(buf, "card_%d.png", (i+1));
CCSprite *pSpr_Front = CCSprite::create(buf);
pSpr_Front->setPosition(ccp(winSize.width/2-60+60*2*(i%2), winSize.height/2+120-120*(i/2)));
pSpr_Front->setTag(TAG_CARD_FRONT+i);
pSpr_Front->setVisible(false);
this->addChild(pSpr_Front);
}
}void Game::initLabel()
{
CCLabelTTF *pLabel_0 = CCLabelTTF::create("CARD : 0/4", "", 20);
pLabel_0->setAnchorPoint(ccp(0, 1));
pLabel_0->setPosition(ccp(10, winSize.height-10));
pLabel_0->setTag(TAG_LABEL_CARD);
pLabel_0->setColor(ccBLACK);
this->addChild(pLabel_0);
CCLabelTTF *pLabel_1 = CCLabelTTF::create("LIFE : 3", "", 20);
pLabel_1->setAnchorPoint(ccp(1, 1));
pLabel_1->setPosition(ccp(winSize.width-10, winSize.height-10));
pLabel_1->setTag(TAG_LABEL_LIFE);
pLabel_1->setColor(ccBLACK);
this->addChild(pLabel_1);
}void Game::initReady()
{
CCLabelTTF *pLabel = CCLabelTTF::create("READY", "", 70);
pLabel->setPosition(ccp(winSize.width/2, winSize.height/2));
pLabel->setTag(TAG_LABEL_READY);
pLabel->setColor(ccBLACK);
pLabel->setOpacity(0);
this->addChild(pLabel);
}void Game::initStart()
{
CCLabelTTF *pLabel = CCLabelTTF::create("START", "", 70);
pLabel->setPosition(ccp(winSize.width/2, winSize.height/2));
pLabel->setTag(TAG_LABEL_START);
pLabel->setColor(ccBLACK);
pLabel->setOpacity(0);
this->addChild(pLabel);
}void Game::initO()
{
CCSprite *pSpr = CCSprite::create("o.png");
pSpr->setPosition(ccp(winSize.width/2, winSize.height/2));
pSpr->setTag(TAG_SPRITE_O);
pSpr->setVisible(false);
this->addChild(pSpr);
}void Game::initX()
{
CCSprite *pSpr = CCSprite::create("x.png");
pSpr->setPosition(ccp(winSize.width/2, winSize.height/2));
pSpr->setTag(TAG_SPRITE_X);
pSpr->setVisible(false);
this->addChild(pSpr);
}void Game::initGameover()
{
CCLabelTTF *pLabel = CCLabelTTF::create("GAME OVER", "", 50);
pLabel->setPosition(ccp(winSize.width/2, winSize.height+50));
pLabel->setTag(TAG_LABEL_GAMEOVER);
pLabel->setColor(ccRED);
this->addChild(pLabel);
}void Game::initGameclear()
{
CCLabelTTF *pLabel = CCLabelTTF::create("GAME CLEAR", "", 50);
pLabel->setPosition(ccp(winSize.width/2, winSize.height+50));
pLabel->setTag(TAG_LABEL_GAMECLEAR);
pLabel->setColor(ccBLUE);
this->addChild(pLabel);
}void Game::initMenu()
{
CCMenuItemFont *item_0 = CCMenuItemFont::create("Retry", this, menu_selector(Game::menuCallback));
CCMenuItemFont *item_1 = CCMenuItemFont::create("Menu", this, menu_selector(Game::menuCallback));
item_0->setTag(0);
item_1->setTag(1);
CCMenu *pMenu = CCMenu::create(item_0, item_1, NULL);
pMenu->alignItemsHorizontallyWithPadding(100);
pMenu->setPosition(ccp(winSize.width/2, 30));
pMenu->setTag(TAG_MENU);
pMenu->setVisible(false);
this->addChild(pMenu);
}void Game::menuCallback(cocos2d::CCObject *pSender)
{
CCLog("menuCallback");
CCMenuItemFont *item = (CCMenuItemFont *)pSender;
int Tag = item->getTag();
switch (Tag) {
case 0:
{
initData();
CCLabelTTF *pLabel_0 = (CCLabelTTF *)this->getChildByTag(TAG_LABEL_CARD);
pLabel_0->setString("CARD : 0/4");
CCLabelTTF *pLabel_1 = (CCLabelTTF *)this->getChildByTag(TAG_LABEL_LIFE);
pLabel_1->setString("LIFE : 3");
CCLabelTTF *pLabel_2 = (CCLabelTTF *)this->getChildByTag(TAG_LABEL_GAMECLEAR);
pLabel_2->setPosition(ccp(winSize.width/2, winSize.height+50));
CCLabelTTF *pLabel_3 = (CCLabelTTF *)this->getChildByTag(TAG_LABEL_GAMEOVER);
pLabel_3->setPosition(ccp(winSize.width/2, winSize.height+50));
CCMenu *pMenu = (CCMenu *)this->getChildByTag(TAG_MENU);
pMenu->setVisible(false);
actionReady();
}
break;
case 1:
CCDirector::sharedDirector()->replaceScene(Menu::scene());
break;
default:
break;
}
}void Game::actionReady()
{
CCAction *action_0 = CCFadeIn::create(0.5);
CCAction *action_1 = CCDelayTime::create(1.0);
CCAction *action_2 = CCFadeOut::create(0.5);
CCAction *action_3 = CCCallFunc::create(this, callfunc_selector(Game::endReady));
CCAction *action_10 = CCSequence::create((CCFiniteTimeAction *)action_0, action_1, action_2, action_3, NULL);
CCLabelTTF *pLabel = (CCLabelTTF *)this->getChildByTag(TAG_LABEL_READY);
pLabel->runAction(action_10);
}void Game::endReady(CCObject *pSender)
{
actionStart();
}void Game::actionStart()
{
CCAction *action_0 = CCFadeIn::create(0.5);
CCAction *action_1 = CCDelayTime::create(1.0);
CCAction *action_2 = CCFadeOut::create(0.5);
CCAction *action_3 = CCCallFunc::create(this, callfunc_selector(Game::endStart));
CCAction *action_10 = CCSequence::create((CCFiniteTimeAction *)action_0, action_1, action_2, action_3, NULL);
CCLabelTTF *pLabel = (CCLabelTTF *)this->getChildByTag(TAG_LABEL_START);
pLabel->runAction(action_10);
}void Game::endStart(CCObject *pSender)
{
actionCard();
}void Game::actionCard()
{
for (int i=0; i<4; i++) {
CCSprite *spr = (CCSprite *)this->getChildByTag(TAG_CARD_FRONT+Card[i]);
CCAction *action_0 = CCDelayTime::create(4.0f*i);
CCAction *action_1 = CCShow::create();
CCAction *action_2 = CCDelayTime::create(3.0f);
CCAction *action_3 = CCHide::create();
CCAction *action_4 = CCDelayTime::create(1.0f);
CCAction *action_10 = CCSequence::create((CCFiniteTimeAction *)action_0, action_1, action_2, action_3, action_4, NULL);
spr->runAction(action_10);
}
CCAction *action_0 = CCDelayTime::create(4.0*4);
CCAction *action_1 = CCCallFunc::create(this, callfunc_selector(Game::endCard));
CCAction *action_10 = CCSequence::create((CCFiniteTimeAction *)action_0, action_1, NULL);
this->runAction(action_10);
}void Game::endCard(cocos2d::CCObject *pSender)
{
CCLog("endCard");
this->setTouchEnabled(true);
}void Game::actionSelectCard()
{
CCSprite *pSpr = (CCSprite *)this->getChildByTag(TAG_CARD_FRONT+ nSelectCard);
pSpr->setVisible(true);
this->setTouchEnabled(false);
CCAction *action_0 = CCDelayTime::create(1.0f);
CCAction *action_1 = CCHide::create();
CCAction *action_2 = CCCallFunc::create(this, callfunc_selector(Game::endSelectCard));
CCAction *action_10 = CCSequence::create((CCFiniteTimeAction *)action_0, action_1, action_2, NULL);
pSpr->runAction(action_10);
}void Game::endSelectCard(cocos2d::CCObject *pSender)
{
this->setTouchEnabled(true);
if (nCountCard==nGoal) {
CCLog("GAMECLEAR");
actionGameResult(TAG_LABEL_GAMECLEAR);
}
else if (nLife==0) {
CCLog("GAMEOVER");
actionGameResult(TAG_LABEL_GAMEOVER);
}
}void Game::actionGameResult(int Tag)
{
this->setTouchEnabled(false);
CCLabelTTF *pLabel = (CCLabelTTF *)this->getChildByTag(Tag);
CCMoveTo *action_0 = CCMoveTo::create(0.5, ccp(winSize.width/2, winSize.height/2));
CCAction *action_1 = CCEaseBounceOut::create(action_0);
CCAction *action_2 = CCCallFunc::create(this, callfunc_selector(Game::endGameResult));
CCAction *action_10 = CCSequence::create((CCFiniteTimeAction *)action_1, action_2, NULL);
pLabel->runAction(action_10);
}void Game::endGameResult(CCObject *pSender)
{
CCMenu *pMenu = (CCMenu *)this->getChildByTag(TAG_MENU);
pMenu->setVisible(true);
}void Game::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
CCLog("ccTouchesBegan");
CCTouch *pTouch = (CCTouch *)pTouches->anyObject();
CCPoint location = pTouch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
for (int i=0; i<6; i++) {
CCSprite *pSpr = (CCSprite *)this->getChildByTag(TAG_CARD_FRONT+i);
CCRect rect = pSpr->boundingBox();
if (rect.containsPoint(location)) {
nSelectCard = i;
actionSelectCard();
int Tag;
if (i==Card[nCountCard]) {
// 맞는 카드를 선택했을 경우
// O 이미지 그리기
// card 0/4 증가
nCountCard++;
CCLabelTTF *pLabel = (CCLabelTTF *)this->getChildByTag(TAG_LABEL_CARD);
char buf[64];
sprintf(buf, "CARD : %d/%d", nCountCard, nGoal);
pLabel->setString(buf);
Tag = TAG_SPRITE_O;
}
else {
// 틀린 카드를 선택했을 경우
// x 이미지 그리기
// life 감소
nLife--;
CCLabelTTF *pLabel = (CCLabelTTF *)this->getChildByTag(TAG_LABEL_LIFE);
char buf[64];
sprintf(buf, "LIFE : %d", nLife);
pLabel->setString(buf);
Tag = TAG_SPRITE_X;
}
CCSprite *pSpr = (CCSprite *)this->getChildByTag(Tag);
pSpr->setVisible(true);
CCAction *action_0 = CCDelayTime::create(1.0f);
CCAction *action_1 = CCHide::create();
CCAction *action_2 = CCSequence::create((CCFiniteTimeAction *)action_0, action_1, NULL);
pSpr->runAction(action_2);
}
}
}'Pause @ > Cocos2d-x2' 카테고리의 다른 글
cocos2d-x 메뉴전환정리 (1) 2013.07.07 cocos2d-x 프로젝트1 정리 (0) 2013.07.03 cocos2d-x 사운드 및 (0) 2013.06.30 cocos2d-x transition, parallaxnode (0) 2013.06.30 cocos2d-x 스케쥴update, CCSpriteBatchNode (0) 2013.06.30