gpt4 book ai didi

ios - 如何以正确的顺序创建多个SKShapeNode?

转载 作者:行者123 更新时间:2023-11-29 05:17:25 24 4
gpt4 key购买 nike

创建多个 SKShapeNode 如下:

for i in 1...20 {
let waitAct = SKAction.wait(forDuration: delay)
self.run(waitAct) {
let disc = SKShapeNode(rectOf: CGSize(width: width, height: height), cornerRadius: height/2)
disc.position = CGPoint(x: self.frame.midX, y: self.frame.maxY)
disc.fillColor = self.discColors[i]
disc.strokeColor = .clear
disc.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: width, height: height))
disc.physicsBody?.affectedByGravity = true
disc.physicsBody?.density = 0.5
disc.physicsBody?.mass = 0.2
disc.physicsBody?.friction = 1.0
disc.physicsBody?.restitution = 0
disc.physicsBody?.isDynamic = true
disc.physicsBody?.allowsRotation = false
disc.name = "\(i)"
self.addChild(disc)

let label = SKLabelNode(text: "\(i)")
label.fontColor = .white
label.fontName = "Arial Rounded MT Bold"
label.fontSize = fontSize
label.verticalAlignmentMode = .center
disc.addChild(label)

print(i)
}
delay += 0.2
}

所有光盘均在屏幕顶部创建,然后自动下拉,所有光盘垂直堆叠。

这里有两个问题:

  1. 光盘应按 1 至 20 的顺序堆叠。打印时,它们是正确的。但是当检查labelNode文本时,它们在真实设备上工作,但对于几张光盘,有时在模拟器上失败(特别是在iPad Pro,iPhone 11 Pro等大屏幕设备上运行开始时),我认为这是一个问题电脑速度:-(。为了安全起见,现在我设置了'delay += 0.2'。我认为这也是下拉动画的问题。我如何确保它们能够按顺序放置?(尽可能保持短延迟)

  2. 当圆盘下落完成并堆叠起来时,它们会继续非常轻微且持续地弹跳。更多的光盘,更多的弹跳。我认为这是 disc.physicalsBody?.affectedByGravity = true 的问题,但必须启用此功能才能稍后移动。如何让他们保持静止?

谢谢。

===已编辑===

问题2的解决方案:圆盘下面有一层,它们的恢复必须都设置为0。

最佳答案

要解决 SpriteKit 在模拟器上的性能问题,我建议查看此页面:

Developing Metal Apps that Run in Simulator

值得注意的是,本节:

Simulator doesn't try to exactly simulate the GPU from the iOS or tvOS device you are simulating. For example, if you are simulating the iPhone XS, Simulator does not try to emulate the capabilities of an A12 GPU. Instead, Simulator translates any calls you make and directs them to the selected GPU on the host Mac.

如果启用以下功能,您可能会发现模拟器的帧速率低于 60.0 fps,具体取决于主机的性能:

skView.showsFPS = true

但是,将其设置为 true 并在硬件上使用它应该始终提供 60.0 fps。

最终,当开发任何基于 Metal 的东西(SpriteKit 就是其中之一)时,您无法保证模拟器上的性能。毕竟,它是一个模拟器,而不是模拟器

最后一次引用苹果的话:

Simulator is best used to rapidly prototype and test application behavior, and to develop the basic rendering or compute capabilities of your app.

当然,您可以想出一些技巧来解决这些限制,但是这样您就不会测试您发布的代码(当针对发布而不是调试进行优化时,这些代码将再次表现不同)。

关于ios - 如何以正确的顺序创建多个SKShapeNode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59054246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com