VioletaBabel
16일 : 오디오, 3:3 AI 탱크게임 본문
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <AudioEngine.h> using namespace cocos2d::experimental; //... bool A::init() { AudioEngine::play2d("res/a.mp3", true); // 배경음악이 나옴, true는 루프. int AudioID = AudioEngine::play2d("res/a.mp3"); // 재생중인 음악의 ID가 나옴 AudioEngine::setVolume(AudioID, 0.5f)//볼륨이 0.5로 줄어듦 AudioEngine::setCurrentTime(audioID,1.0f); // 1초부터 재생 } | cs |
--
탱크게임
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //GameScene.h #pragma once #include <cocos2d.h> #include "BackGround.h" #include "MenuLayer.h" #include "GameLayer.h" USING_NS_CC; class GameScene : public Scene { public: GameScene(); ~GameScene(); bool init(); CREATE_FUNC(GameScene); void StartP(); void GameP(); void BackP(); void MenuP(); }; | cs |
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 | //GameScene.cpp #include "GameScene.h" #include <AudioEngine.h> using namespace cocos2d::experimental; GameScene::GameScene() { srand(time(NULL)); } GameScene::~GameScene() { } bool GameScene::init() { AudioEngine::play2d("res/150708_extreme_race.mp3", true); BackP(); MenuP(); return true; } void GameScene::StartP() { } void GameScene::GameP() { GameLayer *pGame = GameLayer::create(); addChild(pGame, 10, "Scene_Game"); pGame->makeTanks(1, 2, 3); } void GameScene::BackP() { BackGround *pback = BackGround::create(); addChild(pback, 10, "Scene_Back"); } void GameScene::MenuP() { MenuLayer *pMenu = MenuLayer::create(); addChild(pMenu, 10, "Scene_Menu"); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //BackGround.h #pragma once #include <cocos2d.h> USING_NS_CC; class BackGround : public Layer { public: BackGround(); ~BackGround(); bool init(); CREATE_FUNC(BackGround); }; | cs |
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 | //BackGround.cpp #include "BackGround.h" BackGround::BackGround() { } BackGround::~BackGround() { } bool BackGround::init() { Sprite *pDirt[3][6], *pGrass[4][6], *pSand[3][6], *pSideDirt[6], *pSideSand[6]; std::string dirtName, grassName, sandName, sideName; for (int i = 0; i < 4; ++i) for (int j = 0; j < 6; ++j) { if (i < 3) { dirtName = StringUtils::format("Back_Dirt%d%d", i, j); pDirt[i][j] = Sprite::create("res/Environment/dirt.png"); addChild(pDirt[i][j], 10, dirtName); pDirt[i][j]->setPosition(64 + (128 * i), 656 - (128 * j)); sandName = StringUtils::format("Back_Sand%d%d", i, j); pSand[i][j] = Sprite::create("res/Environment/sand.png"); addChild(pSand[i][j], 10, sandName); pSand[i][j]->setPosition(960 + (128 * i), 656 - (128 * j)); } grassName = StringUtils::format("Back_Grass%d%d", i, j); pGrass[i][j] = Sprite::create("res/Environment/grass.png"); addChild(pGrass[i][j], 0, grassName); pGrass[i][j]->setPosition(448 + (128 * i), 656 - (128 * j)); if (i == 0) { sideName = StringUtils::format("res/Environment/side_dirt%d.png", j % 2); pSideDirt[j] = Sprite::create(sideName); sideName = StringUtils::format("Back_SideDirt%d", j); addChild(pSideDirt[j], 20, sideName); pSideDirt[j]->setPosition(448, 656 - (128 * j)); sideName = StringUtils::format("res/Environment/side_sand%d.png", j % 2); pSideSand[j] = Sprite::create(sideName); sideName = StringUtils::format("Back_SideSand%d", j); addChild(pSideSand[j], 20, sideName); pSideSand[j]->setPosition(832, 656 - (128 * j)); } } return true; } | cs |
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 | //GameLayer.h #pragma once #include <cocos2d.h> #include "Tank.h" #include "GameScene.h" #include "Bullet.h" #include "launchAni.h" #include "railAni.h" #include "Drum.h" USING_NS_CC; class GameLayer : public Layer { private: bool pUp, pDown, pLeft, pRight; float deg = 0, railT = 0.665f, continueLaunch = 1.0f, finalTime = 0; bool win = 1; public: bool live[6] = { 1,1,1,1,1,1 }; bool gameover = 0; GameLayer(); ~GameLayer(); bool init(); CREATE_FUNC(GameLayer); void makeTanks(int player, int ally, int enemy); virtual void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event); virtual void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event); void update(float dt); void onMouseMove(EventMouse* event); void onMouseDown(EventMouse* event); void bombEffect(Point a); void railAnimation(Point a, float d); void aiBullet(int ty, float r, int n, int tag); }; | cs |
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 | //GameLayer.cpp #include "GameLayer.h" GameLayer::GameLayer() { pUp = 0; pDown = 0; pLeft = 0; pRight = 0; } GameLayer::~GameLayer() { } bool GameLayer::init() { EventListenerKeyboard *listener = EventListenerKeyboard::create(); listener->onKeyPressed = CC_CALLBACK_2(GameLayer::onKeyPressed, this); listener->onKeyReleased = CC_CALLBACK_2(GameLayer::onKeyReleased, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); auto Mouse = EventListenerMouse::create(); Mouse->onMouseMove = CC_CALLBACK_1(GameLayer::onMouseMove, this); Mouse->onMouseDown = CC_CALLBACK_1(GameLayer::onMouseDown, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(Mouse, this); Drum *d[4]; for (int i = 0; i < 4; ++i) { std::string name = StringUtils::format("Game_Drum%d", i); d[i] = Drum::create(); addChild(d[i], 1000, name); d[i]->setPosition(rand() % 200 + 600, rand() % 100 + 150 * i); } scheduleUpdate(); return true; } void GameLayer::makeTanks(int player, int ally, int enemy) { Tank *p; p = Tank::create(0, 2, 0); addChild(p, 50, "Game_Player"); p->makeOneTank(); std::string name; for (int i = 0; ally > 0 || enemy > 0; ++i) { if (ally-- > 0) p = Tank::create(1, i % 3, i + 1); else if (enemy-- > 0) p = Tank::create(2, i % 3, i + 1); name = StringUtils::format("Game_Tank%d", i); addChild(p, 50, name); p->makeOneTank(); } } void GameLayer::onKeyPressed(EventKeyboard::KeyCode keyCode, Event * event) { int a = 0; if ((Tank*)getChildByName("Game_Player")) { auto player = (Tank*)getChildByName("Game_Player")->getChildByName("Tank_Tank"); pUp = (keyCode == EventKeyboard::KeyCode::KEY_W) ? 1 : pUp; pDown = (keyCode == EventKeyboard::KeyCode::KEY_S) ? 1 : pDown; pLeft = (keyCode == EventKeyboard::KeyCode::KEY_A) ? 1 : pLeft; pRight = (keyCode == EventKeyboard::KeyCode::KEY_D) ? 1 : pRight; a += pUp; a += pDown; a += pLeft; a += pRight; if (a > 2 || (pUp&&pDown) || (pLeft&&pRight)) { pUp = 0; pDown = 0; pLeft = 0; pRight = 0; } } } void GameLayer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event * event) { pUp = (keyCode == EventKeyboard::KeyCode::KEY_W) ? 0 : pUp; pDown = (keyCode == EventKeyboard::KeyCode::KEY_S) ? 0 : pDown; pLeft = (keyCode == EventKeyboard::KeyCode::KEY_A) ? 0 : pLeft; pRight = (keyCode == EventKeyboard::KeyCode::KEY_D) ? 0 : pRight; } void GameLayer::update(float dt) { if (!live[0] && !live[1] && !live[2]) { gameover = 1; win = 0; } else if (!live[3] && !live[4] && !live[5]) gameover = 1; if (!gameover) { railT += dt; continueLaunch += dt; float x = 0, y = 0; auto player = (Tank*)getChildByName("Game_Player"); if (player) { if (pLeft || pRight) { if (railT >= 0.2f) { railAni *pRail = railAni::create(); addChild(pRail, 1, "Game_Rail"); pRail->setPosition(((Tank*)getChildByName("Game_Player"))->getPosition()); pRail->setRotation(deg); railT -= 0.2f; } if (pLeft) { if (pDown) deg += 90.0f*dt; else deg -= 90.0f*dt; } else { if (pDown) deg -= 90.0f*dt; else deg += 90.0f*dt; } } if (deg >= 360.0f) deg -= 360.0f; else if (deg < 0) deg += 360.0f; if (!player) return; player->getChildByName("Tank_Tank")->setRotation(deg); float rad = CC_DEGREES_TO_RADIANS(deg); if (pUp) { x = 2.0f*sinf(rad); y = 2.0f * cosf(rad); } else if (pDown) { x = -2.0f*sinf(rad); y = -2.0f * cosf(rad); } player->setPosition(player->getPosition() + Vec2(x, y)); if (railT >= 0.665f) { railAni *pRail = railAni::create(); addChild(pRail, 1, "Game_Rail"); pRail->setPosition(player->getPosition()); pRail->setRotation(deg); railT -= 0.665f; } Point playerNow = player->getPosition(); if (playerNow.x < 39) player->setPositionX(39); else if (playerNow.x > 1241) player->setPositionX(1241); if (playerNow.y < 39) player->setPositionY(39); else if (playerNow.y > 681) player->setPositionY(681); } } else { if (finalTime == 0) if (win) { auto label = LabelTTF::create("Win!!!!", "arial", 100); label->setPosition(640, 360); label->setColor(Color3B::BLACK); addChild(label, 2000, 10); } else { auto label = LabelTTF::create("Lose...", "arial", 100); label->setPosition(640, 360); label->setColor(Color3B::BLACK); addChild(label, 2000, 10); } finalTime += dt; if (finalTime >= 3.0f) { auto gs = (GameScene*)getParent(); gs->MenuP(); removeFromParent(); } } } void GameLayer::onMouseMove(EventMouse * event) { if (!gameover) { auto tank = ((Tank*)getChildByName("Game_Player")); if (tank) { Point c = Director::getInstance()->convertToGL(event->getLocation()); Point p = tank->getPosition(); Point d = c - p; //CCLOG("GameLayer_MouseMovenormali"); d.normalize(); float r = atan2f(d.x, d.y); tank->getChildByName("Tank_Barrel")->setRotation(CC_RADIANS_TO_DEGREES(r)); } } } void GameLayer::onMouseDown(EventMouse * event) { if (!gameover) if (continueLaunch >= 1.0f) { continueLaunch = 0; auto tank = ((Tank*)getChildByName("Game_Player")); Point c = Director::getInstance()->convertToGL(event->getLocation()); Point p = tank->getPosition(); Point d = c - p; //CCLOG("GAMELAYER_MOUSEDOWNnormali"); d.normalize(); float r = atan2f(d.x, d.y); Bullet *myBullet; myBullet = Bullet::create(0, r, 2); addChild(myBullet, 49, "Game_PlayerBullet"); myBullet->setRotation(CC_RADIANS_TO_DEGREES(r)); myBullet->setPosition(tank->getPosition()); launchAni *pLaunch = launchAni::create(); addChild(pLaunch, 100, "Game_Launch"); pLaunch->setPosition(tank->getPosition()); } } void GameLayer::bombEffect(Point a) { launchAni *pLaunch = launchAni::create(); addChild(pLaunch, 1000, "Bullet_Launch"); pLaunch->setPosition(a); } void GameLayer::railAnimation(Point a, float d) { railAni *pRail = railAni::create(); addChild(pRail, 1, "Game_Other_Rail"); pRail->setPosition(a); pRail->setRotation(d); } void GameLayer::aiBullet(int ty, float r, int n, int tag) { if (!gameover) { std::string name = StringUtils::format("Game_Tank%d", tag); auto tank = ((Tank*)getChildByName(name)); Bullet *aiBullet; aiBullet = Bullet::create(ty, r, n); addChild(aiBullet, 49, name + "_Bullet"); aiBullet->setRotation(CC_RADIANS_TO_DEGREES(r)); aiBullet->setPosition(tank->getPosition()); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 | //MenuLayer.h #pragma once #include<cocos2d.h> USING_NS_CC; class MenuLayer : public Layer { public: MenuLayer(); ~MenuLayer(); bool init(); void StartGame(Object *sender); CREATE_FUNC(MenuLayer); }; | cs |
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 | //MenuLayer.cpp #include "MenuLayer.h" #include "GameScene.h" MenuLayer::MenuLayer() { } MenuLayer::~MenuLayer() { } bool MenuLayer::init() { auto mStart = MenuItemImage::create("res/buttonStart.png", "res/buttonStart.png", CC_CALLBACK_1(MenuLayer::StartGame, this)); mStart->setScale(1.5f); auto menu = Menu::create(mStart, NULL); menu->alignItemsHorizontally(); menu->alignItemsVertically(); this->addChild(menu); return true; } void MenuLayer::StartGame(Object *sender) { auto a = (GameScene*)getParent(); a->GameP(); removeFromParent(); auto item = (MenuItem *)sender; } | cs |
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 | //Tank.h #pragma once #include <cocos2d.h> #include "GameLayer.h" USING_NS_CC; class Tank : public Node { private: int type; // 0은 주인공, 1은 아군, 2는 적 int num; int arrow = 1; float railT = 0.665f, deg = 0, continueLaunch = 0, changeArrow = 0, randomTime = 0; bool enemy[3] = { 0,0,0 }; public: int tagNum; const int r = /*57*/39; Tank(); ~Tank(); bool init(); static Tank *create(int t, int n, int an); void makeOneTank(); int getType(); int getNum(); CREATE_FUNC(Tank); void update(float dt); }; | cs |
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 | //Tank.cpp #include "Tank.h" Tank::Tank() { } Tank::~Tank() { } bool Tank::init() { if (this->type != 0) scheduleUpdate(); return true; } Tank * Tank::create(int t, int n, int an) { Tank *pNewTank = new Tank; pNewTank->autorelease(); pNewTank->type = t; pNewTank->num = n; pNewTank->tagNum = an; pNewTank->init(); return pNewTank; } void Tank::makeOneTank() { int x, y; std::string tankName = StringUtils::format("res/Tanks/tank%d.png", this->type); Sprite *thisTank = Sprite::create(tankName); tankName = StringUtils::format("res/Tanks/barrel%d.png", this->type); Sprite *thisBarrel = Sprite::create(tankName); addChild(thisTank, 100, "Tank_Tank"); addChild(thisBarrel, 101, "Tank_Barrel"); if (this->type == 2) x = rand() % 250 + 100; else x = rand() % 250 + 950; y = rand() % 120 + 150 * (num + 1); setPosition(x, y); thisBarrel->setAnchorPoint(Point::ANCHOR_MIDDLE_BOTTOM); } int Tank::getType() { return this->type; } int Tank::getNum() { return this->num; } void Tank::update(float dt) { auto gameL = (GameLayer*)getParent(); Tank* allTank[6], myEnemy[3]; allTank[0] = (Tank*)(gameL->getChildByName("Game_Player")); for (int i = 1; i < 6; ++i) { std::string name = StringUtils::format("Game_Tank%d", i - 1); allTank[i] = (Tank*)(gameL->getChildByName(name)); } for (int i = 0; i < 6; ++i) { if (allTank[i] != NULL && this != NULL) { //CCLOG("Tank"); if (allTank[i]->getType() != this->type || allTank[i]->getNum() != this->num) { Point tank = allTank[i]->getPosition(); Point me = getPosition(); Point distance = allTank[i]->getPosition() - this->getPosition(); float fDistance = distance.length(); if (fDistance < this->r + allTank[i]->r) { CCLOG("Tank:normal"); distance.normalize(); allTank[i]->setPosition(allTank[i]->getPosition() + distance * 2.0f); this->setPosition(this->getPosition() - distance * 2.0f); break; } } } } railT += dt; continueLaunch += dt; float x = 0, y = 0; int random = rand() % 11 + 10; if (railT >= 0.665f) { gameL->railAnimation(getPosition(), deg); railT -= 0.665f; } if (changeArrow == 0) { if (rand() % 2 == 0) arrow = -1; randomTime = (float)random / 10.0f; } if (changeArrow <= randomTime) { deg += 90.0f*dt*arrow; getChildByName("Tank_Tank")->setRotation(deg); } else { float rad = CC_DEGREES_TO_RADIANS(deg); setPosition(getPosition() + Vec2(2.0f*sinf(rad), 2.0f*cosf(rad))); } Point now = getPosition(); if (now.x < 39) this->setPositionX(39); else if (now.x > 1241) this->setPositionX(1241); if (now.y < 39) this->setPositionY(39); else if (now.y > 681) this->setPositionY(681); changeArrow += dt; if (changeArrow >= 4.0f) changeArrow = 0; //타겟팅코드 int ans, t; Point d[3]; float fD[3] = { 100000.0f, 100000.0f, 100000.0f }, rr; if (type == 1) t = 3; else if (type == 2) t = 0; std::string name; for (int i = 0; i < 3; ++i) { if (gameL->live[i + t] && allTank[i + t] != NULL) { if (i+t == 0) name = StringUtils::format("Game_Player"); else name = StringUtils::format("Game_Tank%d", i + t - 1); if ((Tank*)gameL->getChildByName(name) != NULL) { d[i] = allTank[i + t]->getPosition() - getPosition(); d->normalize(); fD[i] = atan2f(d[i].x, d[i].y); } } } if (fD[0] > fD[1]) { if (fD[1] > fD[2]) ans = 2; else ans = 1; } else { if (fD[0] > fD[2]) ans = 2; else ans = 0; } rr = fD[ans]; getChildByName("Tank_Barrel")->setRotation(CC_RADIANS_TO_DEGREES(rr)); //발싸코드 if (continueLaunch >= 1.0f) { continueLaunch = 0; gameL->aiBullet(type, rr, num, tagNum-1); } } | cs |
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 | //Bullet.h #pragma once #include <cocos2d.h> #include "GameLayer.h" #include "Tank.h" #include "Drum.h" #include "launchAni.h" USING_NS_CC; class Bullet : public Node { private: int type; float rad; int num; bool del = 0; bool t[6]; public: const int r = 10; // 반지름 Bullet(); ~Bullet(); bool init(); void update(float dt); CREATE_FUNC(Bullet); static Bullet *create(int t, float r, int n); }; | cs |
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 | //Bullet.cpp #include "Bullet.h" #include <AudioEngine.h> using namespace cocos2d::experimental; Bullet::Bullet() { } Bullet::~Bullet() { } bool Bullet::init() { int id = AudioEngine::play2d("res/bomb1.ogg", false); AudioEngine::setVolume(id, 0.3f); for (int i = 0; i < 6; ++i) t[i] = 0; std::string bulletName = StringUtils::format("res/Bullets/bullet%d.png", this->type); Sprite *thisBullet = Sprite::create(bulletName); addChild(thisBullet, 100, "Bullet_Bullet"); scheduleUpdate(); return true; } void Bullet::update(float dt) { float x = 3.0f * sinf(rad); float y = 3.0f * cosf(rad); setPosition(getPosition() + Vec2(x, y)); Tank *allTank[6]; Drum *allDrum[4]; GameLayer* gameL; if (this->type == 0)//플레이어가 만든 탄환일 때 { } else//컴퓨터가 만든 탄환일 때 { //gameL = (GameLayer*)(getParent()->getParent()); } gameL = (GameLayer*)getParent(); allTank[0] = (Tank*)(gameL->getChildByName("Game_Player")); std::string name; for (int i = 0; i < 6; ++i) { if (i > 0 && i < 6) { name = StringUtils::format("Game_Tank%d", i - 1); allTank[i] = (Tank*)(gameL->getChildByName(name)); } if (i < 4) { name = StringUtils::format("Game_Drum%d", i); allDrum[i] = (Drum*)(gameL->getChildByName(name)); } } for (int i = 0; i < 6; ++i) { if (!t[i]) { if (allTank[i] != NULL && this != NULL) { //CCLOG("Bullet"); if (allTank[i]->getType() != this->type || allTank[i]->getNum() != this->num) { Point tank = allTank[i]->getPosition(); Point me = getPosition(); Point distance = allTank[i]->getPosition() - this->getPosition(); float fDistance = distance.length(); if (fDistance < this->r + allTank[i]->r) { auto a = (GameLayer*)getParent(); a->bombEffect(me); CCLOG("tagNum:%d", allTank[i]->tagNum); auto gameL = (GameLayer*)getParent(); gameL->live[(allTank[i]->tagNum)] = 0; int dead = AudioEngine::play2d("res/bomb2.ogg", false); AudioEngine::setVolume(dead, 1.0f); allTank[i]->removeFromParent(); allTank[i] = NULL; t[i] = 1; this->removeFromParent(); ++del; break; } } } } } for (int i = 0; i < 4 && !del; ++i) { if (allDrum[i] != NULL && this != NULL) { Point drum = allDrum[i]->getPosition(); Point me = getPosition(); Point distance = allDrum[i]->getPosition() - this->getPosition(); float fDistance = distance.length(); if (fDistance < this->r + allDrum[i]->r) { auto a = (GameLayer*)getParent(); a->bombEffect(me); this->removeFromParent(); break; } } } if(!del) if (getPositionX() > 1380 || getPositionX() < -100 || getPositionY() > 820 || getPositionY() < -100) removeFromParent(); } Bullet * Bullet::create(int t, float r, int n) { Bullet *pNewBullet = new Bullet; pNewBullet->autorelease(); pNewBullet->type = t; pNewBullet->rad = r; pNewBullet->num = n; pNewBullet->init(); return pNewBullet; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //Drum.h #pragma once #include <cocos2d.h> USING_NS_CC; class Drum : public Node { public: const int r = 24; Drum(); ~Drum(); bool init(); CREATE_FUNC(Drum); void update(float dt); }; | cs |
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 | //Drum.cpp #include "Drum.h" #include "GameScene.h" #include "GameLayer.h" Drum::Drum() { } Drum::~Drum() { } bool Drum::init() { int i = rand() % 3; std::string drumName = StringUtils::format("res/Obstacles/drum%d.png", i); Sprite *pDrum = Sprite::create(drumName); addChild(pDrum, 1000, "Drum_Drum"); scheduleUpdate(); return true; } void Drum::update(float dt) { auto gameL = (GameLayer*)getParent(); auto player = (Tank*)(gameL->getChildByName("Game_Player")); auto thisDrum = getChildByName("Drum_Drum"); if (gameL&&player&&thisDrum) { Point my = player->getPosition(); Point drum = getPosition(); Point distance = my - drum; float fDistance = distance.length(); if (fDistance < this->r + player->r) { distance.normalize(); player->setPosition(player->getPosition() + distance * 2.0f); } } Tank* allTank[5]; bool tankLive[5]; for (int i = 0; i < 5; ++i) { if (gameL) { if (gameL->live[i+1] && allTank[i] != NULL) { std::string name = StringUtils::format("Game_Tank%d", i); allTank[i] = (Tank*)(gameL->getChildByName(name)); if ((Tank*)(gameL->getChildByName(name)) != NULL) { //CCLOG("%d / %d", i, allTank[i]->tagNum); Point distance = allTank[i]->getPosition() - getPosition(); float fDistance = distance.length(); if (fDistance < this->r + allTank[i]->r) { distance.normalize(); allTank[i]->setPosition(allTank[i]->getPosition() + distance * 2.0f); } } } } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //launchAni.h #pragma once #include <cocos2d.h> USING_NS_CC; class launchAni : public Node { public: launchAni(); ~launchAni(); bool init(); CREATE_FUNC(launchAni); }; | cs |
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 | //launchAni.cpp #include "launchAni.h" launchAni::launchAni() { } launchAni::~launchAni() { } bool launchAni::init() { Rect rt(0, 0, 100, 107); SpriteFrame* pSF[6]; std::string sfName; Animation* ani = Animation::create(); for (int i = 0; i < 6; ++i) { sfName = StringUtils::format("res/Smoke/smokeGrey%d.png", i); pSF[i] = SpriteFrame::create(sfName, rt); ani->addSpriteFrame(pSF[i]); } ani->setLoops(true); ani->setDelayPerUnit(0.064f); Animate* animate = Animate::create(ani); Sprite* pChar = Sprite::create("res/Smoke/SmokeGrey0.png"); addChild(pChar, 0, "launchAni_Launch"); auto sp = Spawn::create(animate, FadeOut::create(1.0f), NULL); auto seq = Sequence::create(sp, RemoveSelf::create(true), NULL); pChar->runAction(seq); return true; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //railAni.h #pragma once #include <cocos2d.h> USING_NS_CC; class railAni : public Node { public: railAni(); ~railAni(); bool init(); CREATE_FUNC(railAni); }; | cs |
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 | //railAni.cpp #include "railAni.h" railAni::railAni() { } railAni::~railAni() { } bool railAni::init() { Rect rt(0, 0, 74, 104); SpriteFrame* pSF; Animation* ani = Animation::create(); pSF = SpriteFrame::create("res/Tanks/mytrack.png", rt); ani->addSpriteFrame(pSF); ani->setLoops(true); Animate* animate = Animate::create(ani); Sprite* pChar = Sprite::create("res/Tanks/mytrack.png"); addChild(pChar, 0, "railAni_Rail"); auto sp = Spawn::create(animate, FadeOut::create(2.0f), NULL); auto seq = Sequence::create(sp, RemoveSelf::create(true), NULL); pChar->runAction(seq); return true; } | cs |
'BCA > 2. Cocos2d-x' 카테고리의 다른 글
16일 : 파일 입출력 - 바이너리 저장, JSON (0) | 2018.03.02 |
---|---|
15일 : 파일 입출력, 오디오, 러닝게임 완성 (0) | 2018.02.28 |
14일 : 애니메이션 (0) | 2018.02.27 |
13일 : 복습 겸 아주 자잘한 팁 (0) | 2018.02.26 |
12일 : 플래피 버드 완성하기 (0) | 2018.02.23 |
Comments