I'm trying to animate a drawNode declared as "dotNode" from the function "HelloWorld::touchDetector" in the code above.
But I always get EXC_BAD_ACCESS when I click and fired that function. (Debug area shows: "dotNode cocos2d::DrawNode * NULL 0x0000000000000000" / "this cocos2d::Node * NULL 0x0000000000000000" or something like that.)
Does somebody has any idea why does it happens? I thought this code will work because I declared the drawNode public in HelloWorld.h, but it doesn't. (Label worked with almost same code.)
Thanks,
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;
}