【发布时间】:2014-06-19 02:15:36
【问题描述】:
我正在尝试为上面代码中的函数 "HelloWorld::touchDetector" 声明为 "dotNode" 的 drawNode 设置动画。
但是当我单击并触发该功能时,我总是得到 EXC_BAD_ACCESS。 (调试区显示:"dotNode cocos2d::DrawNode * NULL 0x0000000000000000" / "this cocos2d::Node * NULL 0x0000000000000000"之类的。)
有人知道为什么会这样吗?我认为这段代码可以工作,因为我在 HelloWorld.h 中声明了 drawNode 公用,但它没有。 (标签使用几乎相同的代码。)
谢谢,
HelloWorld.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::LayerGradient
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(HelloWorld);
cocos2d::DrawNode *dotNode;
void touchDetector(double locationX, double locationY);
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init(){
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
DrawNode *dotNode = DrawNode::create();
dotNode->drawDot(Point(visibleSize.width*.5, visibleSize.height*.5), 10, Color4F(Color3B::WHITE));
this->addChild(dotNode, 1);
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [=](Touch *touch, Event *event) mutable{
Point location = touch->getLocation();
HelloWorld::touchDetector(location.x, location.y);
return true;
};
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, dotNode);
return true;
}
void HelloWorld::touchDetector(double locationX, double locationY){
FiniteTimeAction* scale = EaseBounceOut::create(ScaleTo::create( 0.45f, 1.0f));
dotNode->runAction(scale);
return;
}
【问题讨论】: