How to change a CCSprite's position during a move action
Is it possible to change a CCSprite's
position during a move action, like CCMoveBy?
I have a subclass of CCSprite
that cycles through a bunch of actions, some of which are moves. I check to see if it has gone offscreen, and then attempt to move it directly to the opposite side of the screen. So if it went offscreen left halfway through a move to the left, it would finish the move from the right side of the screen.
But if I use setPosition:
it does开发者_如何转开发n't seem to work, it just finishes it's move action.
CCMoveTo
(which CCMoveBy
is a subclass of) stores the initial position of the object and adds a delta to that stored position at each update, so any other setting of position would be pointless as the next update would put it back on its old path.
You can change your code to use two CCMoveTo
actions - one taking the object off screen, and another to move it back in from the other side. Alternatively, you can create your own action (take a look at the source code for CCMoveTo
if you're unsure how that's done).
[sprite stopAllActions];
sprite.position = rightSideOfScreen;
[sprite runAction:newMoveToLeftAction];
That should work I expect. If you really need to "continue the action" and not start a new one then you will have to obtain how long the action was running and start the new action with the amount of time that should have been left.
精彩评论