Skip to content

Instantly share code, notes, and snippets.

@akehoyayoi
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akehoyayoi/9153866 to your computer and use it in GitHub Desktop.
Save akehoyayoi/9153866 to your computer and use it in GitHub Desktop.
cocos2d-x v3.0 SceneEditorの読み込みサンプル
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