cocos2dx萌新,请教各位大佬,为啥runAction传一个已经创建的动作会出现Segmentation Fault?最简代码如下:
BugScene.h
#pragma once
#include "cocos2d.h"
USING_NS_CC;
class BugScene : public cocos2d::Scene
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(BugScene);
protected:
Sprite* player;
MoveBy* move;
void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event);
};
BugScene.cpp
#include "BugScene.h"
USING_NS_CC;
Scene* BugScene::createScene()
{
return BugScene::create();
}
// on "init" you need to initialize your instance
bool BugScene::init()
{
//////////////////////////////
// 1. super init first
if ( !Scene::init() )
{
return false;
}
this->player = Sprite::create("circle.png");
this->player->setAnchorPoint(Vec2(0, 0));
this->player->setPosition(0, 0);
this->addChild(this->player);
this->move = MoveBy::create(0.1, Vec2(100, 100));
player->runAction(move->clone()); // 这里正常执行动作
/* Register keyboard events */
EventListenerKeyboard* listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(BugScene::onKeyPressed, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
void BugScene::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) {
log("Key Pressed");
player->runAction(move->clone()); //这里通过键盘事件触发的动作就会报错
// player->runAction(MoveBy::create(0.1, Vec2(100, 100))); //每次创建新的动作是正常的
}
