在 UIKit 中使用 SpriteKit [英] Using SpriteKit inside UIKit

查看:32
本文介绍了在 UIKit 中使用 SpriteKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑一个简单的应用程序,它有四个字符,您可以在屏幕上拖动它们.拖动它们时,它们会摆动"——这就是动画.如果它们足够接近某个位置,它们就会捕捉到一个位置……就像拼图一样.没有动画,这在 UIKit 中都很简单.

I am contemplating a simple app that has four characters that you can drag around on the screen. While dragging they "wiggle" — that's the animation. And they snap to a position if they get close enough to it... like in a puzzle. Without the animation, this is all simple in UIKit.

我的第一个想法是在普通的旧 UIView 中以自己的 SKView 呈现每个字符.我可以将 UIGestureRecognizers 附加到每个 SKView 以跟踪点击和拖动.但我认为这意味着每个角色/SKView 都有单独的 GameScenes.这似乎与 SpriteKit 背道而驰.

My first thought is to render each character in its own SKView inside a plain old UIView. I could attach UIGestureRecognizers to each SKView to track tapping and dragging. But I think this implies individual GameScenes for each character/SKView. That seems go go against the grain of SpriteKit.

另一种选择是带有四个精灵的单个 GameScene.但我仍然需要跟踪 &拖动它们,我不知道如何在全 SpriteKit 应用程序中执行此操作.

The alternative is a single GameScene with the four sprites. But I would still need to track & drag them and I don't see how to do that within an all-SpriteKit app.

其中一种方法是否比另一种方法更好?

Is either approach better practice than the other?

推荐答案

您应该实现具有单个场景和每个角色单独精灵的方案.精灵将是 SKSpriteNode 实例,它们是 SKNode 实例,它们是 UIResponder 的后代,因此可以响应触摸事件.

You should implement the one that has a single scene and separate sprites for each character. The sprites will be SKSpriteNode instances which are SKNode instances, which descend from UIResponder and can, therefore, respond to touch events.

查看 SKNode 的文档.这是适用于精灵以及任何其他类型节点的丰富信息.特别是它说:

Look at the documentation for SKNode. It is a wealth of information that applies to sprites as well as to any other kind of node. In particular it says:

所有节点都是响应者对象,可以直接响应用户与屏幕上节点的交互......

All nodes are responder objects that can respond directly to user interaction with the node onscreen…

然后是关于Hit-Test Order is the Reverse of Drawing Order"等的部分.

And then later has a section on "The Hit-Test Order is the Reverse of Drawing Order" etc.

这篇关于在 UIKit 中使用 SpriteKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆