#include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = HelloWorld::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 HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } planeNum = -1; //초기화 score = 0; listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this); //onTouchBegan이 기본으로 넘기는값 2개를 갖기 때문에 CC_CALLBACK_2로 한다 listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); initBG(); initBtn(); initReady(); initGameOver(); initScore(); return true; } void HelloWorld::initBG() { auto sprBG_00 = Sprite::create("bg0.png"); sprBG_00->setAnchorPoint(Vec2(0.5, 0.5)); sprBG_00->setPosition(Vec2(400, 240)); this->addChild(sprBG_00); auto sprBG_01 = Sprite::create("bg1.png"); sprBG_01->setAnchorPoint(Vec2(0.5, 0)); sprBG_01->setPosition(Vec2(400, 0)); this->addChild(sprBG_01); } void HelloWorld::initScore() { auto label = Label::createWithTTF(StringUtils::format("SCORE : %d", score), "kenvector_future.ttf", 32); label->setPosition(Vec2(10, 470)); label->setAnchorPoint(Vec2(0, 1)); label->setTag(TAG_SCORE); label->enableOutline(Color4B::BLACK, 2); this->addChild(label); // setScore(score); //최초 0으로 초기화 } void HelloWorld::initBtn() { btnFlag = false; //첫생성때 버튼클릭안됨 for (int i = 0; i < 4; i++) { auto spr = Sprite::create("btn.png"); spr->setTag(TAG_SPRITE_RED + i); if (i == 1) { spr->setColor(Color3B::BLUE); } else if ( i == 2 ) { spr->setColor(Color3B::YELLOW); } else if (i == 3) { spr->setColor(Color3B::GREEN); } else { spr->setColor(Color3B::RED); } spr->setScale(0.5); spr->setPosition(Vec2(100 +(i*100), 50)); spr->setAnchorPoint(Vec2(0.5, 0.5)); spr->setOpacity(128); this->addChild(spr); } tapFlag = false; //처음에 클릭안되게함 auto spr = Sprite::create("tap.png"); spr->setTag(TAG_SPRITE_TAP); spr->setAnchorPoint(Vec2(0.5, 0.5)); spr->setPosition(Vec2(700, 70)); this->addChild(spr); } void HelloWorld::initReady() { auto spr = Sprite::create("getready.png"); spr->setPosition(Vec2(400, 240)); spr->setScale(0); this->addChild(spr); spr->runAction(Sequence::create( DelayTime::create(1.0f), EaseElasticOut::create( ScaleTo::create(1.0f, 1) ), DelayTime::create(0.5f), ScaleTo::create(0.1f, 0), CallFunc::create( CC_CALLBACK_0(HelloWorld::setBtn, this, true) ), CallFunc::create( CC_CALLBACK_0(HelloWorld::setTap, this, true) ), //레디가 끝나고나서 버튼,탭 다시 사용가능하게 함 NULL )); } void HelloWorld::initGameOver() { auto spr = Sprite::create("gameover.png"); spr->setPosition(Vec2(400, 240)); spr->setVisible(false); spr->setTag(TAG_GAMEOVER); this->addChild(spr); } void HelloWorld::setBtn(bool isEnabled) { btnFlag = isEnabled; } void HelloWorld::setTap(bool isEnabled) { tapFlag = isEnabled; } void HelloWorld::setGameOver(bool isShow) { auto spr = (Sprite*)this->getChildByTag(TAG_GAMEOVER); spr->setVisible(isShow); //게임오버되면 모든 버튼 비활성화 setBtn(!isShow); setTap(!isShow); } void HelloWorld::setScore(int score) { auto label = (Label*)this->getChildByTag(TAG_SCORE); label->setString(StringUtils::format("SCORE : %d", score)); } void HelloWorld::menuCallback(int tag) { if (planeNum == -1) return; //현재 누른 버튼의 태그에서 색깔별 태그(1001~1004)를 빼면 if (planeNum == (tag - TAG_SPRITE_RED)) { auto spr = (Sprite*)this->getChildByTag(TAG_PLANE); this->removeChild(spr); //삭제 setScore(score=score+100); planeNum = -1; //비행기가 없을때 메뉴를 누르면 게임오버되는걸 방지하기위해 세팅 //메뉴 콜백에서 -1이면 return하게함으로써 뜬금사망 방지 setTap(true); //재생성 가능하게 함 } else { setGameOver(true); } } void HelloWorld::tapCallback() //무탈 { //탭하면 비행기 생성 setTap(false); //한번누르고 비행기가 사라질때까지 클릭안되게함 //정상적으로 사라졌을때 다시 터치가능하게 처리해줘야함 planeNum = rand() % 4; //현재 생성된 비행기의 번호 auto spr = Sprite::create(StringUtils::format("plane_%d.png", planeNum)); //랜덤하게 생성하기 spr->setPosition(Vec2(900, 100+(rand()%350))); //랜덤한 y좌표 spr->setTag(TAG_PLANE); spr->setFlippedX(true); //그림 뒤집기 this->addChild(spr); spr->runAction(Sequence::create( //시퀀스로 연속동작 MoveBy::create(1.0f + rand() % 3, Vec2(-1000, 0)), //-1000만큼 이동해서 화면밖으로 나가고나서 CallFunc::create(CC_CALLBACK_0(HelloWorld::setGameOver, this, true)), //RemoveSelf가 먼저 나오면, 해당spr가 죽고나서 action을 수행하는것이기때문에 앞서해준다. RemoveSelf::create(), //삭제 NULL )); } bool HelloWorld::onTouchBegan(Touch* touch, Event* unused_event) { CCLOG("onTouchBegan"); auto touchPos = touch->getLocation(); auto tap = (Sprite*)this->getChildByTag(TAG_SPRITE_TAP); Rect tapRect = tap->getBoundingBox(); auto spr_1 = (Sprite*)this->getChildByTag(TAG_SPRITE_RED); Rect Rect1 = spr_1->getBoundingBox(); auto spr_2 = (Sprite*)this->getChildByTag(TAG_SPRITE_BLUE); Rect Rect2 = spr_2->getBoundingBox(); auto spr_3 = (Sprite*)this->getChildByTag(TAG_SPRITE_YELLOW); Rect Rect3 = spr_3->getBoundingBox(); auto spr_4 = (Sprite*)this->getChildByTag(TAG_SPRITE_GREEN); Rect Rect4 = spr_4->getBoundingBox(); if (tapRect.containsPoint(touchPos) && tapFlag ) //탭버튼을 눌렀으며 그게 누를수있는 상태인가? { tap->setTexture("tapTick.png"); tapCallback(); return true; } else if (Rect1.containsPoint(touchPos) ) { menuCallback(TAG_SPRITE_RED); } else if (Rect2.containsPoint(touchPos)) { menuCallback(TAG_SPRITE_BLUE); } else if ( Rect3.containsPoint(touchPos) ) { menuCallback(TAG_SPRITE_YELLOW); } else if (Rect4.containsPoint(touchPos)) { menuCallback(TAG_SPRITE_GREEN); } else { CCLOG("onTouchBegan::there is no btn"); } return false; } void HelloWorld::onTouchEnded(Touch* touch, Event* unused_event) { auto spr = (Sprite*)this->getChildByTag(TAG_SPRITE_TAP); spr->setTexture("tap.png"); CCLOG("onTouchEnded"); } |