gpt4 book ai didi

swift - 球在 Pong Clone 中不动

转载 作者:搜寻专家 更新时间:2023-11-01 07:18:18 28 4
gpt4 key购买 nike

我的 Pong 版本有问题。当我输入这段代码时,球不会在屏幕上移动。问题出在哪儿?请帮助我!

class GameScene: SKScene {

var ball = SKSpriteNode()
var enemy = SKSpriteNode()
var main = SKSpriteNode()

override func didMove(to view: SKView) {

ball = self.childNode(withName: "ball") as! SKSpriteNode
enemy = self.childNode(withName: "enemy") as! SKSpriteNode
main = self.childNode(withName: "main") as! SKSpriteNode

ball.physicsBody?.applyImpulse(CGVector(dx: 20, dy: 20))

let border = SKPhysicsBody(edgeLoopFrom: self.frame)

border.friction = 0
border.restitution = 1

self.physicsBody = border

}
}

最佳答案

你知道这行代码

ball.physicsBody?.applyImpulse(CGVector(dx: 20, dy: 20))

表示

“如果球的属性physicsBody不为nil,调用方法applyImpulse()

不是吗?好的。如果 physicsBody nil,这段代码会做什么?是的,它什么都不做。那么,您是否在执行此代码时检查过 physicsBody 不是 nil

根据属性physicsBody的文档:

The default value is nil, which indicates that the node does not participate in the physics simulation at all. If a physics body is provided, when the scene’s physics are simulated, the physics body updates the node’s position and rotates the node.

没有代码显示您在球对象上设置了 physicsBody

关于swift - 球在 Pong Clone 中不动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40619797/

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