CC_CALLBACK_N and CallFunc / CallFuncN question

  • MoseHas
    Likes 0

    Problem Description

    Hi there.

    Recently I need to use CallFunc to control the action sequence so that it could play the animation in proper way.

    But now I can only use CallFunc and CC_CALLBACK_0 with non-return function. Every time I try to use CallFuncN and CC_CALLBACK_N I always fail to do so.

    ...
    	sprite = Sprite::create("mysprite.png");
    	sprite->setPosition(visibleSize.width / 2, visibleSize.height / 2);
    	this->addChild(sprite);
    
    	auto moveDown = MoveTo::create(1.0f, Point(visibleSize.width / 2, 0));
    	CallFunc* removeSprite = CallFunc::create(CC_CALLBACK_0(HelloWorld::removeFunc,this));
    	auto seq = Sequence::create(moveDown, removeSprite, NULL);
    }
    
    void HelloWorld::removeFunc()
    {
    	sprite->removeFromParentAndCleanup(true);
    }

    The above will work. But what if I need to pass some parameter into the function? I have not succeeded yet.

    void HelloWorld::myFunc(int size)
    {
    	Hello2::otherFunc(size);
    	Hello3::anotherFunc(size);
    }

    Like this one, how should I use CallFunc (or CallFuncN) to run it correctly?

    Thanks.


  • barabasandras1
    Likes 0

    Why don’t you just create a simple function to do the stuff? Instead of this:

    CallFunc* removeSprite = CallFunc::create(CC_CALLBACK_0(HelloWorld::removeFunc,this));

    use:

    removeFunc(int value);


    This reply has been verified.
  • Sonar Systems admin
    Likes 0

    Right on @barabasandras1


    This reply has been verified.
  • MoseHas
    Likes 0

    The reason why I did not just simply create a function for use is because I have no choice.

    For example, I need to play several particle effects and then delete the particles.

    The original function will automatically delete it, but it did not work. I suppose it is because I always display particle effect more than twice within single click, and as long as I create a new particle, the old pointer will redirect to the new particle and make memory leak. So I have to do a sequence action. Create it and delete it. However, both creating and deleting are not an action. I cannot use sequence to control their running timeline.(If they run together the particle effect will disappear immediately after creating.)

    Thus, I use CallFunc to implement creating and deleting so that I can use a simple sequence to do such things without memory leaking.


    This reply has been verified.
  • MoseHas
    Likes 0

    For another example:

    void GameLayer::showLinkNum(int size)
    {
    	Size visibleSize = Director::getInstance()->getVisibleSize();
    	string s;
    	if (Global::isDoubled) s = "+" + String::createWithFormat("%d", size * size * 10)->_string;//String->_string: convert cocos2d::String to std::string
    	else s= "+" + String::createWithFormat("%d", size * size * 5)->_string;
    	linkNum->setString(s);
    	linkNum->setVisible(true);
    	linkNum->setScale(1.0f);
    	
    	linkNum->setPosition(touchPos);
    	auto flyup = MoveTo::create(1.8f, Point(visibleSize.width / 2 + origin.x, visibleSize.height * 0.9 + origin.y));
    	auto shrink = ScaleTo::create(1.8f, 0.8f);
    	CallFunc* invisible = CallFunc::create(CC_CALLBACK_0(GameLayer::hideLinkNum,this));
    
    	Sequence* seq = Sequence::create(Spawn::create(flyup, shrink, NULL), invisible/*,refreshScoreF*/, NULL);
    	linkNum->runAction(seq);
    }

    This is an effect that displays words and flies up.

    I cannot let the word be invisible as long as it is still flying.

    I need to wait until the animation is done.

    In such case, I cannot just use simple function to do what I need to.


    This reply has been verified.
  • Sonar Systems admin
    Likes 0

    Got just the thing for you

     

    use the Callfunc action, you can check it out on our Cocos API Guide http://cocos.sonarlearning.co.uk/v1.0/docs/actions

     

    You can use it like any other action, even in a sequence :D


    This reply has been verified.
  • MoseHas
    Likes 0

    Thank you for the reply. But sadly this is not what I need exactly.

    I think I need the example of CallFuncN and CC_CALLBACK_1、CC_CALLBACK_2, etc.

    Still appreciate your effort!

  • Sonar Systems admin
    Likes 0

    Whats wrong with the CallFuncN method

  • MoseHas
    Likes 0

    I don’t think I really understand CallFuncN because I never succeed using it.

    Would you mind illustrating it with a simple example, please?

    Thank you.


  • Sonar Systems admin
    Likes 0

    From the link I gave you, put that CallFuncN action inside a sequence, repeat, anywhere you would put another action such as the MoveTo

  • MoseHas
    Likes 0

    For example,

    //ShopMenu.cpp
    auto closebtn = MenuItemImage::create("close.png", "close.png", CC_CALLBACK_1(ShopMenu::closeLayer, node));

    ps. node is this pointer in GameLayer.

    void ShopMenu::closeLayer(Layer* shopLayer)
    {
    	shopLayer->removeFromParentAndCleanup(true);
    }

    How do I send shoplayer into closeLayer() and let the layer be deleted?


  • Sonar Systems admin
    Likes 0

    I don’t understand?

  • MoseHas
    Likes 0

    I apologize for my pathetic English.

    What I mean is that I need to design a menu button, but somehow I do not know how to pass parameters in CC_CALLBACK_N. I succeed creating a menu button only if the function is really simple and does not  have parameters.

    I need a simple tutorial. Thanks.

  • MoseHas
    Likes 0

    Thank you! It really helps.

  • Sonar Systems admin
    Likes 0

    Great to hear :D

Login to reply