Wait for an event to happen

I’m on a little vacation, so answers take a little longer.

Forget that new rect code, stay with mine as it guarantees the touch location. The new one only works if you don’t have childs, as touch locations are in screen space but nodes/childs are in node space.

You have a little misunderstanding of how the code works. You don’t want to stay in that function as it should be an asynchronous one and should trigger a callback.

Your layer should react on the dice throw, not your dice!

There are two options how to achieve that.

Option 1:
Create the listener in an init func of your dice and a delegate/callback as another parameter, if you want to rely on a generous callback instead of a hard coded one.

Returning true is just for your listener to know, that you swallow the event. It’s not related to the dice init function as this is a lamba function bound to the listener.

Instead of returning something in the dice init function itself, you call the delegate/callback in your lambda function of your listener. This callback is a callback to a layer function, that moves on with your gameplay code, as soon it is triggered by the listener. If you did not use a parameter for the callback, hard code it as a direct call to your layer.

Option 2:
Create the listener in your layer and add the dice sprite to it.
The other code would be the same. Callback as a formal parameter or a hard coded call to your layer.

Sorry for editing so much. Typing on my pad with the on-screen keyboard sucks.

Thanks for your advice, I solved that problem, But now I have an other one. :smiley:
when my dice was a layer I could easily implement a sprite sheet animation, just like what here says. I want to have an animation inside roll function, I mean when Dice was a layer when roll() executed it showed me an animation but I don’t know how to do it now, because now my Dice is a sprite and I don’t know where to write these lines of codes:

 SpriteBatchNode* spritebatch = SpriteBatchNode::create("sprites.png");
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
    cache->addSpriteFramesWithFile("sprites.plist");

 m_pSprite1 = Sprite::createWithSpriteFrameName("1.png");
    spritebatch->addChild(m_pSprite1);
    addChild(spritebatch);

note that I want to have the animation inside my roll() function.
I want to have a function like " getAnimationWithFrames (int from, int to)" that returns an animation so in my roll() function I could easily wrire:

this->runAction(RepeatForever::create(someClass::getAnimationWithFrames(1,10)));

is it possible?
where should I put the body of getAnimationWithFrames ?

First forget about the old clunky way to do animations. No need for batch nodes, as cocos2d-x 3.x can do auto batching. Use the method described under File animation in your link.

It’s not any different if you have a layer or a sprite, as both are nodes and an animation works on nodes.
Of course it is possible. All things imaginable are possibe. It’s just how hard you try.
Create the animation in your dice init function/constructor.

Then call the spritesheet animation in your roll function: runAction(animate);. No need to create a repeat forever function, as you can define, if your animation will repeat forever in the plist file.

Maybe you/we should make this game project as a cocos2d-x tutorial :slight_smile:

1 Like