gpt4 book ai didi

ios - 我似乎无法让物理机构在 Playground 内工作

转载 作者:行者123 更新时间:2023-11-28 12:06:47 25 4
gpt4 key购买 nike

我真的很想在 Playground 上调试我的一些游戏物理,这样可以更轻松地查看正在发生的事情并运行一些测试代码。我已将我的问题简化为一个小片段,您可以将其复制并粘贴到 playground 中,看看是否可以重现此问题并可能提供解决方案:

import SpriteKit

import PlaygroundSupport

let sceneView = SKView(frame: CGRect(x: 0, y: 0, width: 480,
height: 320))
let scene = SKScene(size: CGSize(width: 480, height: 320))
sceneView.showsFPS = true
sceneView.showsPhysics = true
sceneView.presentScene(scene)
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = sceneView

let test = SKShapeNode(rectOf: CGSize(width:100,height:100))
test.fillColor = UIColor.red
test.position = CGPoint(x:scene.size.width/2, y: scene.size.height/2)
test.zPosition = 1

test.physicsBody? = SKPhysicsBody(circleOfRadius: 50)

scene.addChild(test)

您应该会在 playground 输出的中心看到一个正方形。

问题是创建圆形物理体的 SKPhysicsBody 调用似乎返回 nil。如果我删除可选符号:

test.physicsBody = .....

调用失败,节点无法出现在场景中。

有人知道我在 playground 设置或代码中缺少什么来创建和查看物理体吗?理想情况下,我想添加几个更复杂的角色,然后在 playground 中调整物理 body 、关节等,以便在将修改添加回游戏之前轻松观察它们。

最佳答案

删除 ?来自:

test.physicsBody? =

发生的事情是 Swift 正在查看 test.physicsBody 是否存在,因为它是可选的,而 '?'并发现它没有,所以不理会该行的其余部分(= SKPhysicsBody(circleOfRadius: 50))

您可以在 playground 中使用以下代码验证这一点:

var myOpt: Int?
myOpt? = 4
print((myOpt) // Prints nil.
myOpt = 5
print(myOpt) // Prints Otional(5)

提示 - 在处理可选项时,当我看到“?”时,我会用质疑的语气自言自语地阅读我的代码。以及当我看到“!”时语调有力,这有助于我记住这一点?意思是“这存在吗?”和 !意思是“JUST DO IT!”

编辑 - 刚刚看到您确实在没有“?”的情况下进行了尝试,但是您说“调用失败并且节点未能出现在场景中。”

我刚刚在 Playground 中尝试了您的代码,它运行良好。正方形在消失之前出现,因为它在重力的作用下无休止地向下坠落。 :-)

添加:

scene.physicsWorld.gravity = CGVector(dx:0, dy:0) 在你做你的 addChild

之前

关于ios - 我似乎无法让物理机构在 Playground 内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49270039/

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