Last active
August 29, 2015 13:56
-
-
Save akehoyayoi/9153866 to your computer and use it in GitHub Desktop.
cocos2d-x v3.0 SceneEditorの読み込みサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool HelloWorld::init() | |
{ | |
if (!Layer::init()) return false; | |
auto reader = cocostudio::SceneReader::getInstance(); | |
// SceneEditorデフォルトに存在するサンプルを読み込む | |
auto node = reader->createNodeWithSceneFile("FightScene.json"); | |
auto hero = node->getChildByTag(10005); // サンプル上のheroに設定されていたtagの値 | |
// タッチでアニメーションを切り替える実装 | |
auto listener = EventListenerTouchOneByOne::create(); | |
listener->onTouchBegan = [](Touch* touch,Event* event) { | |
auto target = event->getCurrentTarget(); | |
// アニメーション再生のためにArmatureを取得する必要あり | |
auto children = target->getChildren(); | |
if(children.empty() == false) { | |
auto armature = dynamic_cast<cocostudio::Armature*>(children.front()); | |
if(armature) { | |
// 厳密ではないが、なんとなくタッチ検出したいので、Armatureよりサイズ取得してタッチ判定する | |
Point locationInNode = target->convertToNodeSpace(touch->getLocation()); | |
Size s = armature->getContentSize(); | |
Rect rect = Rect(0, 0, s.width, s.height); | |
if (rect.containsPoint(locationInNode)) { | |
armature->getAnimation()->play("attack"); | |
return true; | |
} | |
} | |
} | |
return false; | |
}; | |
listener->onTouchEnded = [](Touch* touch, Event* event){ | |
auto children = event->getCurrentTarget()->getChildren(); | |
if(children.empty() == false) { | |
auto armature = dynamic_cast<cocostudio::Armature*>(children.front()); | |
if(armature) armature->getAnimation()->play("run"); | |
} | |
}; | |
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, hero); | |
this->addChild(node); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment