1
votes

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;
}
2

2 Answers

1
votes

The error is to declare a local variable DrawNode *dotNode = DrawNode::create(); in the init method in HelloWorld.cpp. Remove the declaration and should work.

0
votes

create Function internally autoreleases the object. So you have to retain that object. I think this is making the problem.

just make retain call after create.

dotNode->retain();