ios - Sprite Kit - 绘制带有多色边框的圆

标签 ios swift sprite-kit

我想画一个类似这样的圆:

image desc

我将让不同的对象与圆圈交互(碰撞),但对象将根据触摸的颜色进行不同的交互。

这可以使用贝塞尔曲线路径来完成吗?我希望将圆圈的每种颜色设置为不同的变量。

我最初的想法是在 photoshop 中绘制圆圈并将其导入 Sprite Kit,但我不知道如何通过这种方式将颜色分成不同的变量。

最佳答案

将对象作为 png(来自 photoshop 或其他软件)导入到您的游戏中,当节点与其发生碰撞时,检测它接触的点并计算 x 和 y 与圆心的差异。即使圆圈在旋转,只要您已经将颜色映射到“区域”,这也应该始终告诉您碰撞物体撞击的颜色。有道理吗?

或者,您可以创建一个透明圆 (SKShapeNode) 并将八个 SKShapeNode 弧作为该透明圆的子级。故意放置它们。这增加了一个好处,即每个对象都是一个单独的对象,可以有自己的 strokeColor 和自己的触摸方法。它们需要能够碰撞,而父透明圆圈需要忽略碰撞才能正常工作。

至于如何检测圆的哪个部分被击中,我会这样做:

给定一个被分成 8 个大小相等的饼 block 的圆,您应该能够根据碰撞点相对于圆心的 x 和 y 值来确定哪个部分发生了碰撞。例如,假设 Y 轴是两个部分之间的分界线,每个部分有四个部分(见图),您可以首先按象限缩小碰撞范围,如下所示:

Circle with 8 sections

cx, cy = 碰撞 x 和 y 值 ox, oy = 圆心(原点)的 x 和 y 值

伪代码:

if cx > ox && cy > oy, then the collision occurred in quadrant 1
if cx > ox && cy < oy, then the collision occurred in quadrant 2
if cx < ox && cy < oy, then the collision occurred in quadrant 3
if cx < ox && cy > oy, then the collision occurred in quadrant 4

由于每个象限中有两个部分,因此您可以进一步细化此逻辑,通过比较差异来确定两个部分中的哪个部分发生了碰撞

伪代码:

if the difference between cx and ox > the difference between cy and oy, then the collision occurred in Section 2
else the collision occurred in section 1

将这些逻辑组合到一个嵌套的 if-else 语句中,您将得到如下内容:

伪代码:

if cx > ox && cy > oy, then the collision occurred in quadrant 1
......if the difference between cx and ox > the difference between cy and oy, then the collision occurred in Section 2
......else the collision occurred in section 1
if cx > ox && cy < oy, then the collision occurred in quadrant 2
......if the difference between cx and ox > the difference between cy and oy, then the collision occurred in Section 3
......else the collision occurred in section 4
if cx < ox && cy < oy, then the collision occurred in quadrant 3
......if the difference between cx and ox > the difference between cy and oy, then the collision occurred in Section 6
......else the collision occurred in section 5
if cx < ox && cy > oy, then the collision occurred in quadrant 4
......if the difference between cx and ox > the difference between cy and oy, then the collision occurred in Section 7
......else the collision occurred in section 8

喂!

关于ios - Sprite Kit - 绘制带有多色边框的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34971802/

相关文章:

android - XDK HTML5 应用程序中的 CSS 渐变兼容性问题

ios - 按位置从 iOS 照片库中获取照片

ios - 从 cocos2d-x 代码调用原生 ObjC 类

ios - 带有类型为 "cannot invoke initializer for type ' 的参数列表的 'UITextField' Int' 是什么意思?

ios - 无法将类型 'UILabel!' 的值转换为预期参数 'type inout String'

ios - 如何在 Swift 中查找文件类型

sprite-kit - 使用 Sprite Kit,如何让对象跳跃?

swift - 从 SKNode 成员中删除所有子项时如何异常(exception)?

ios - 将数据从我的 viewcontroller 类传递到放置在同一场景中的自定义类 UIView 时,“包装可选值时出现意外的 nil”

swift - 我应该如何使用 touchesMoved 函数正确更新曲线?