Moving a CCSprite and getting a callback

450 views Asked by At

I move an instance of CCSprite in my Cocos2D-based iPhone game like this:

[mySprite runAction:[CCMoveBy actionWithDuration:1.0 
                                        position:ccp(10, 10)]];

How can I get a callback everytime the sprite moves?

I'd like to do something like this:

[self registerForCallbacksFrom:mySprite 
                      selector:@selector(spriteMovedOneStep)];

So spriteMovedOneStep would be called everytime mySprite moves. Would be nice to specify the frequency of the callback too so minimize CPU usage.

2

There are 2 answers

0
andrey.s On

One possible solution is to subclass CCMoveBy and call your callback from it's update method. You also can setup frequency and everything you want with this approach.

1
Shachar On

If I get your question right you would like to have a method called at times when the Sprite is moving, correct ? How about scheduling an update method that performs what you want if a SpriteIsMoving BOOL is set to YES, I'm not sure on what your trying to achieve but this is my take on it.