我有一个游戏,在游戏过程中你的手指四处移动,应该避免碰到一些障碍物。我知道如何使用碰撞,但我最近听说有一个名为 body(at : point)
的函数。
我应该说我掩盖了我的障碍。因此,我无法使用 contains(point)
函数。
我真的很想使用 body(at : point)
函数,否则我知道如何处理碰撞。
我的一些代码:
play_button = self.childNode(withName: "play_button") as! SKSpriteNode
for t in touches{
let fingerlocation = t.location(in: self)
if physicsworld.body(at : fingerlocation)
{
let so = level6(fileNamed:"level6")
so?.scaleMode = .aspectFill
self.view?.presentScene(so!, transition:
SKTransition.crossFade(withDuration: 1))
}
}
但是它不起作用。
最佳答案
physicalworld.body 如果找到则返回 SKPhysicsBody,因此您只需检查它是否存在
if let _ =Physicsworld.body(at : Fingerlocation)
如果你的目标是制作一个按钮,那么我不会采用这种方法。
我建议仅子类化 SKSpriteNode 并启用 isUserInteractionEnable`
class Button : SKSpriteNode
{
override init()
{
super.init()
isUserInteractionEnabled = true
}
//then in your touchesBegan/touchesEnded code (wherever you placed it)
....
let so = level6(fileNamed:"level6")
so?.scaleMode = .aspectFill
scene.view?.presentScene(so!, transition:
SKTransition.crossFade(withDuration: 1))
....
}
关于swift - 如何使用SpriteKit方法体(位于: CGPoint)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45200968/