【发布时间】:2021-12-13 21:21:08
【问题描述】:
我正在制作一个游戏,在这种情况下,“箭头”是一个节点,它每隔几秒就会从屏幕的顶部到底部的随机区域落下。问题是当节点到达屏幕的最后 10 个左右时,它似乎几乎卡在屏幕上,然后停止,没有完全离开屏幕并消失。有没有办法解决这个问题?
func startTheArrow() {
run(SKAction.repeatForever(SKAction.sequence([SKAction.run(spawnArrow), SKAction.wait(forDuration: 5.0)])))
}
func spawnArrow() {
let arrow = SKSpriteNode(imageNamed: "arrow")
arrow.size = CGSize(width: 50, height: 50)
arrow.physicsBody = SKPhysicsBody(rectangleOf: arrow.size)
arrow.physicsBody?.affectedByGravity = false
arrow.physicsBody?.categoryBitMask = ColliderType.arrow
arrow.name = "Arrow"
arrow.zPosition = 1
arrow.position = CGPoint(x: frame.size.width * random(min: -0.45, max: 0.45), y: frame.size.height * random(min: 0.6, max: 0.7))
addChild(arrow)
arrow.run(
SKAction.moveBy(x: 0.0 , y: -size.height - arrow.size.height,
duration: TimeInterval(random(min: 2, max: 2))))
self.enumerateChildNodes(withName: "Arrow") { (node:SKNode, nil) in
if node.position.y < -500 || node.position.y > self.size.height + 550 {
node.removeFromParent()
}
}
}
【问题讨论】:
-
检查物理体没有与物体发生碰撞,在 ViewController 中使用 'view.showsPhysics = true' 将其打开(与 'view.showsFPS = true' 等相同的位置)。跨度>
-
@JohnL 嘿,所以从来不知道你能做到这一点!我做到了,似乎没有任何东西出现在那里。这很奇怪,我想不出它会停在那里的任何原因。
-
你在使用physicsBody = SKPhysicsBody(edgeLoopFrom: frame) ???
-
@SimonePistecchia 不!这就是太奇怪了!那里不应该有任何抵抗。也许是我在 viewController 中设置 SKView 和 presentScene 的方式?
标签: swift sprite-kit skphysicsbody skaction