- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个继承自 SKNode 的自定义类,我想在该类上实现 SKSpriteNode 的类似 anchor 的功能。具体来说,我想要实现的是自动将自定义节点置于父节点的中心(anchorPoint 为 0.5、0.5),而不必将其偏移其宽度/高度的一半。
有什么建议吗?
编辑:我最终首先创建了一个 anchorPoint 变量,然后覆盖了 position 变量并设置了由 anchorPoint 修改的 super 位置。
var anchorPoint = CGPoint(x: 0.5, y: 0.5)
override var position: CGPoint {
set {
super.position = CGPoint(x: self.position.x - self.size.width * self.anchorPoint.x, y: self.position.y - self.size.height * self.anchorPoint.y)
}
get {
return super.position
}
}
最佳答案
您可以覆盖继承类的 setPosition
函数。在此函数中,您将针对偏移 anchor 进行调整。
例如,如果您的 Sprite 是 100x100。并且你已经设置了它的anchor为(.5,.5),当有人设置位置时,你应该从中添加或减去sprite尺寸然后设置位置。在这种情况下,如果用户将 Sprite 位置设置为 (0,0),您将添加 (100*.5,100*.5) = (50,50)。加上 (0,0) 就是 (50,50)。
这是您应该设置的新位置。
关于swift - 在 SKNode 子类中实现 anchorPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29507027/
我发现如果你想在 Sprite Kit 中使用物理,你必须在世界坐标中指定 anchor 。假设您有一个大节点树,这里有一辆汽车,那里有一个轮子。您只知道它们在父节点中的坐标。您想要在汽车和车轮之间定
我正在为场景创建一个 SKNode,并在每个单独的 SKNode 上记录触摸事件。我可以根据需要添加任意数量的内容,如果我触摸节点并且只有可见的节点,我才能看到日志消息,并且触摸会按预期工作。现在,如
基本上我想实现这样的目标:https://github.com/moqod/iOS-Scratch-n-See在 SpriteKit 中。 在 SKScene 中,我有一张汽车图像,2-3 种不同类型
我从一个函数返回一个 SKNode,我需要将它转换为自定义 SKNode。我收到错误消息无法将 SKNode的值分配给类型GroundNode。如果我强制转换,它会编译,但在运行时失败。我错过了什么?
我正在尝试使用 SpriteKit 模拟一只眼睛。 当用户的手指在屏幕上移动时,眼睛的瞳孔会跟踪用户的手指,但必须保持在眼睛的范围内。 我曾尝试使用 SKConstraint 解决此问题,但未成功。
我不明白为什么我在最后一行收到这个错误:'SKNode!'不是'SKNode'的子类型 func move_background(){ let enumerationBlock: (SKNod
我有一个场景,其中有许多子节点,它们是 SKNode 的子类。我正在尝试访问每个子节点的方法,但收到错误“‘SKNode’类型的值没有成员‘[成员名称]”。 全部基于 Xcode 10.2 上的 Sw
我正在使用 Sprite Kit 创建一个 iOS 应用程序,并且我正在尝试选择 (onTouchBegan) 位于另一个 SKNode 下方的 SKNode。结构是:第一层(SKNode)-第二层(
我在带有 SKScene 的 UIView 上有一个 UITapGestureRecognizer 和一个 UIPanGestureRecognizer。平移手势识别器从左到右移动一个 SKNode,
当事件发生时,SKNode 子类应该如何通知父场景? 我有一个 SKNode 子类,它根据游戏逻辑通过适本地重绘或删除节点来处理触摸事件。通知父场景(或其他同级 SKNode 实例)事件发生的 iOS
我有正确的字符串格式的 SKNODE。我想将字符串名称“A2Square”转换为名为 A2Square 的节点。 这可能吗? 是否有办法将字符串“A2Square”并以某种方式将其转换为同名的 SKN
我已经设置了场景,一切都运行良好。它看起来像这样: 但是 Controller 是在 Main.storyboard 中设置的,我想摆脱它。所以我做了这样的事情: window = UIWin
我知道为什么会出现该错误,但我无法找到解决方法。我试图让对象出现然后被删除,玩家应该在它们被删除之前尝试点击它们,但每次下一个节点即将出现时它都会崩溃。如果我在它的 func 中声明它,那么一切都会出
我的场景是,我正在沿着拱形移动现有节点,并希望它在到达另一侧时停止。一个简单的比喻是,我想让一些东西沿着彩虹移动。 虽然我当前的代码确实执行了正确的椭圆形,但它随后在我的 y 原点下方继续返回到其起点
我正在获取 int SpriteKit。并且想知道如何在 SKNode 对象上创建运动效果。 对于 UIView,我使用以下方法: +(void)registerEffectForView:(UIVi
我在不同的文件中有两个类。 在我的场景所在的“主”文件中,我调用此函数: override func touchesBegan(touches: Set, withEvent event: UIEve
好的,所以我有代码会在 1 和 2 之间选择一个随机数,并基于该数生成一个 metalCrate: 或只是一个 crate :。我想,金属 crate 应该需要轻按 2 次才能消失。不幸的是,使用我的
我目前正在同时使用 ARKit 和 CoreLocation 将元素放置在屏幕上。这些元素(例如建筑物、房屋等)基于它们自己的坐标(经度/纬度)和用户坐标放置。如您所知,即使设置了最佳精度,CoreL
我创建了一个运行良好的 SKNode 类。但是,我无法在实例化后更改其 .position。 按钮确实出现并正常工作,但在编程时不会改变位置。我已经包含了为下面的按钮制作的类,我还添加了实例化该类并调
出于学习目的,我正在使用 SpriteKit 和 Swift 5 编写一个类似 Pong 的游戏。我试图让玩家的 Racket (一个 SKNode 矩形)在 x 坐标的屏幕上移动但不能。 我试过这个
我是一名优秀的程序员,十分优秀!