Search code examples
iphoneobjective-cipadcocos2d-iphonesprite

Move object to touch using a transition in an iPhone app


I have a sprite in my iPhone game, how can make it so that that sprite moves to the location of my touch? It needs to transition there, not just switch immediately. My sprite is a CCSprite from cocos2d.


Solution

  • Assuming you want to implement this using UIKit's animation support (and your sprite is implemented as a view), this is one way of doing it.

    • on a touch event, get the location of the touch, and based on the current location of your sprite, determine how long it would take for your sprite to get there

    • inside an animation block, change the frame of your sprite view to the touch location, and also specify the duration and other options you want.

    Apple's 'View Programming Guide' will basically explains everything you need to know to take this approach.