简体   繁体   English

使用 iPhone 应用程序中的过渡将对象移动到触摸

[英]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?我的 iPhone 游戏中有一个精灵,如何让它移动到我触摸的位置? It needs to transition there, not just switch immediately.它需要在那里过渡,而不仅仅是立即切换。 My sprite is a CCSprite from cocos2d.我的精灵是来自 cocos2d 的 CCSprite。

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.假设您想使用 UIKit 的动画支持来实现这一点(并且您的精灵是作为视图实现的),这是一种实现方式。

  • 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. Apple 的“查看编程指南”将基本上解释采取这种方法需要知道的一切。

I hope you are already able to detect the touch location..我希望你已经能够检测到触摸位置..

Assuming you already have the touch location, by using CCMoveTo:假设您已经有了触摸位置,通过使用 CCMoveTo:

id move = [CCMoveTo actionWithDuration:duration position:touchLocation];
[spr runAction:move];

Hope it helps.. =)希望它有帮助.. =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM