#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; } } } } |