2014. 12. 8. 16:34
[D_019] 멀티터치 예제 DATA_OLD/Cocos2d-x 기능성 게임 개발과정2014. 12. 8. 16:34
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 | #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" USING_NS_CC; #define TAG_SPRITE_PLAYER 1000 #define TAG_SPRITE_KEYPAD 2000 //2000~2005 enum KeyPad { NONE = -1, LEFT, UP, RIGHT, DOWN, ROTATION, ATTACK }; class HelloWorld : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(HelloWorld); Size winSize; bool isAttack; bool isRotate; //방향값을 갖는 변수 KeyPad keyPad; void update(float delta); void setAttack(float delta); void initkeyPad(); void initPlayer(); void onTouchesBegan(const std::vector<Touch*>& touches, Event* unused_event); void onTouchesMoved(const std::vector<Touch*>& touches, Event* unused_event); void onTouchesEnded(const std::vector<Touch*>& touches, Event* unused_event); }; #endif // __HELLOWORLD_SCENE_H__ |
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 | #include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene() { auto scene = Scene::create(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { if ( !Layer::init() ) { return false; } srand(time(NULL)); winSize = Director::getInstance()->getWinSize(); keyPad = KeyPad::NONE; isAttack = false; isRotate = false; auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); initPlayer(); initkeyPad(); this->scheduleUpdate(); this->schedule(schedule_selector(HelloWorld::setAttack), 0.05f); return true; } void HelloWorld::initkeyPad() { int posX[6] = { 100, 200, 300, 200, -400, -200 }; int posY[6] = { 200, 300, 200, 100, 200, 200 }; for (int i = 0; i < 6; i++) { //파일명은 flatDark0.png~flatDark5.png 까지 auto spr = Sprite::create(StringUtils::format("flatDark%d.png", i)); spr->setTag(TAG_SPRITE_KEYPAD + i); if (i >= 4) { spr->setPosition(Vec2(winSize.width + posX[i], posY[i])); } else { spr->setPosition(Vec2(posX[i], posY[i])); } this->addChild(spr); } } void HelloWorld::initPlayer() { auto spr = Sprite::create("keep.png"); spr->setTag(TAG_SPRITE_PLAYER); spr->setPosition(winSize*0.5f); this->addChild(spr); } void HelloWorld::update(float delta) { auto spr = (Sprite*)this->getChildByTag(TAG_SPRITE_PLAYER); if (isRotate) { spr->setRotation(spr->getRotation() + 5); } switch (keyPad) { case KeyPad::LEFT : spr->setPosition(spr->getPosition() + Vec2(-2, 0)); break; case KeyPad::UP: spr->setPosition(spr->getPosition() + Vec2(0, 2)); break; case KeyPad::RIGHT: spr->setPosition(spr->getPosition() + Vec2(2, 0)); break; case KeyPad::DOWN: spr->setPosition(spr->getPosition() + Vec2(0, -2)); break; } } void HelloWorld::setAttack(float delta) { if (!isAttack) return; auto sprPlayer = (Sprite*)this->getChildByTag(TAG_SPRITE_PLAYER); auto sprAttack = Sprite::create(StringUtils::format("box_%d.png", rand()%4)); sprAttack->setPosition(sprPlayer->getPosition()); sprAttack->setRotation(sprPlayer->getRotation()); this->addChild(sprAttack); Vec2 sourcePos, targetPos; sourcePos = sprAttack->getPosition(); targetPos = sourcePos + Vec2(0, winSize.width); float angle = sprPlayer->getRotation(); targetPos.rotate(sourcePos, -angle*M_PI / 180.0f); //각도에 따라 무브액션의 도착위치를 정해주는거 /* //각도에 따라 변하는 도착위치 보정 sprAttack->setRotation(sprPlayer->getRotation()); targetPos = targetPos.rotateByAngle(targetPos, sprAttack->getRotation()); */ sprAttack->runAction(Sequence::create( MoveTo::create(5.0f, targetPos), RemoveSelf::create(), NULL)); } void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event* unused_event) { for (Touch* touch : touches) { auto location = touch->getLocation(); for (int i = 0; i < 6; i++) { auto spr = (Sprite*)this->getChildByTag(TAG_SPRITE_KEYPAD + i); if (spr->getBoundingBox().containsPoint(location)) { if (i == 4) isRotate = true; else if (i == 5) isAttack = true; else { keyPad = (KeyPad)i; } } } } return; } void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event* unused_event) { keyPad = KeyPad::NONE; isRotate = false; isAttack = false; for (Touch* touch : touches) { auto location = touch->getLocation(); for (int i = 0; i < 6; i++) { auto spr = (Sprite*)this->getChildByTag(TAG_SPRITE_KEYPAD + i); if (spr->getBoundingBox().containsPoint(location)) { if (i == 4) isRotate = true; else if (i == 5) isAttack = true; else keyPad = (KeyPad)i; } } } } void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event* unused_event) { for (Touch* touch : touches) { auto location = touch->getLocation(); for (int i = 0; i < 6; i++) { auto spr = (Sprite*)this->getChildByTag(TAG_SPRITE_KEYPAD + i); if (spr->getBoundingBox().containsPoint(location)) { if (i == 4) isRotate = false; else if (i == 5) isAttack = false; else keyPad = KeyPad::NONE; } } } } |
파일
'DATA_OLD > Cocos2d-x 기능성 게임 개발과정' 카테고리의 다른 글
[D_021] cocos studio 연동 - 노드 로딩 및 오브젝트 조작 등 (0) | 2014.12.10 |
---|---|
[D_020] Cocos Studio - 기본 세팅, 예제 (0) | 2014.12.09 |
[D_019] eclipse에서 프로젝트 import하기 (0) | 2014.12.08 |
[D_018] Tiny pang 예제 [미완] (0) | 2014.12.04 |
[D_018] tiny flight 예제 (0) | 2014.12.04 |