2014. 11. 20. 11:57
[D_008] 비행기 게임 by touch event DATA_OLD/Cocos2d-x 기능성 게임 개발과정2014. 11. 20. 11:57
HelloWorldScene.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | #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"); } |
HelloWorldScene.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #define TAG_SPRITE 1000 //메뉴 태그 #define TAG_SPRITE_RED 1001 #define TAG_SPRITE_BLUE 1002 #define TAG_SPRITE_YELLOW 1003 #define TAG_SPRITE_GREEN 1004 #define TAG_SPRITE_TAP 1005 #define TAG_GAMEOVER 2000 #define TAG_PLANE 3000 #define TAG_SCORE 4000 USING_NS_CC; class HelloWorld : public cocos2d::Layer { public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); EventListenerTouchOneByOne* listener; int planeNum; int score; bool tapFlag; //탭키 사용가능여부 bool btnFlag; //버튼 사용가능여부 void initBG(); void initBtn(); void initReady(); //레디 세팅 void initGameOver(); //게임오버 세팅 void initScore(); //스코어 세팅 void setGameOver(bool isShow); void setTap(bool isEnabled); //탭키 클릭여부 설정 void setBtn(bool isEnabled); //버튼 클릭여부 설정 void setScore(int score); void menuCallback(int tag); void tapCallback(); virtual bool onTouchBegan(Touch* touch, Event* unused_event); virtual void onTouchEnded(Touch* touch, Event* unused_event); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__ |
'DATA_OLD > Cocos2d-x 기능성 게임 개발과정' 카테고리의 다른 글
[D_008] Scene전환 - replace / push,pop (0) | 2014.11.20 |
---|---|
[D_008] 새로운 Scene 생성 by 프로젝트 기본형 (0) | 2014.11.20 |
[D_007] 터치 판정 예제 (0) | 2014.11.19 |
[D_007] 충돌체크 및 예제 (0) | 2014.11.19 |
[D_007] Touch event (0) | 2014.11.19 |