Cocos2s: get CCSprite position while animating before reaching the destination

140 views Asked by At

I have a CCSprite that is being subjected to a position animation. It is animating perfectly. At some point of time, between starting the animation and reaching the final position, I want to get the exact position of the CCSprite

I know that the sprite.position will return the final position

Here is the animation code:

sprite = [CCSprite spriteWithFile:@"sprite.png"];
[sprite setPosition:ccp(winSize.width/2, 170)];
[self addChild:sprite];

CCMoveTo *moveSpriteUp = [CCMoveTo actionWithDuration:2.0 position:ccp(winSize.width/2, 210)];
CCEaseOut *upEase = [CCEaseOut actionWithAction:moveSpriteUp rate:2];

CCMoveTo *moveSpriteDown = [CCMoveTo actionWithDuration:2.0 position:ccp(winSize.width/2, 170)];
CCEaseOut *downEase = [CCEaseOut actionWithAction:moveSpriteDown rate:2];

CCSequence * spriteMoveSeq = [CCSequence actions:upEase, downEase, nil];
CCRepeatForever *spriteRepeat = [CCRepeatForever actionWithAction:spriteMoveSeq];

[sprite runAction:spriteRepeat]; 
//1 or 1.x seconds later, the sprite's Y postion will be somewhere between 170 and 210, this is what I want to catch

Any way I can do this?

1

There are 1 answers

0
ssantos On

What about CCDelayTime + CCCallFunc? Something like.-

CCDelayTime *delay = [CCDelayTime actionWithDuration:kDelay];
CCCallFunc *func = [CCCallFunc actionWithTarget:self selector:@selector(getSpritePosition)];

[sprite runAction:spriteRepeat];
[self runAction:[CCSequence actionOne:delay two:func]];

In getSpritePosition you could get the sprite position (assuming sprite is accessible from your scene scope), and do whatever you need with it.