检测 SKSpriteNode 是否在底部离开屏幕并调用函数
Detect if a SKSpriteNode left the screen at the bottom and call a function
如果 SKSpriteNode left the screen(我想调用一个Game Over函数)。
我已经在一个函数中声明了节点(如果我做对了,它不是全局的?)并制作了 SKAction 将节点移出屏幕并随后将其删除。
这是我想出的:
var node = SKSpriteNode()
let nodeTexture = SKTexture(imageNamed: "node")
nodeTexture.filteringMode = .Nearest
node = SKSpriteNode(texture: nodeTexture)
let nodeFalling = SKAction.moveToY(-70, duration: 1.6)
let nodeRemoving = SKAction.removeFromParent()
node.runAction(SKAction.sequence([nodeFalling, nodeRemoving]))
self.addChild(node)
现在我需要的是在节点离开屏幕时调用 GameOver()。
非常感谢每一个回答!
在您的场景中,您必须记住要检查的节点的引用,然后在 update
方法中,您只需执行以下操作:
if node.position.y < -node.size.height/2.0 {
node.removeFromParent()
gameOver()
}
编辑:
class MyScene: SKScene {
// ....
//here is the list of nodes which you want to check
var nodesToCheck = [SKSpriteNode]()
//here is your spawn function
func spawnNewNode() {
var node = SKSpriteNode()
let nodeTexture = SKTexture(imageNamed: "node")
nodeTexture.filteringMode = .Nearest
node = SKSpriteNode(texture: nodeTexture)
let nodeFalling = SKAction.moveToY(-70, duration: 1.6)
let nodeRemoving = SKAction.removeFromParent()
node.runAction(SKAction.sequence([nodeFalling, nodeRemoving]))
self.addChild(node)
nodesToCheck.append(node)
}
//and here is the update method
override func update(currentTime: NSTimeInterval) {
super.update(currentTime)
// ... every other update logic
for node in nodesToCheck {
if node.position.y < -node.size.height/2.0 {
node.removeFromParent()
gameOver()
}
}
}
func gameOver() {
println("Damn!")
}
}
当节点不再是场景成员时,不要忘记从 nodeToCheck
数组中删除节点。
如果 SKSpriteNode left the screen(我想调用一个Game Over函数)。
我已经在一个函数中声明了节点(如果我做对了,它不是全局的?)并制作了 SKAction 将节点移出屏幕并随后将其删除。
这是我想出的:
var node = SKSpriteNode()
let nodeTexture = SKTexture(imageNamed: "node")
nodeTexture.filteringMode = .Nearest
node = SKSpriteNode(texture: nodeTexture)
let nodeFalling = SKAction.moveToY(-70, duration: 1.6)
let nodeRemoving = SKAction.removeFromParent()
node.runAction(SKAction.sequence([nodeFalling, nodeRemoving]))
self.addChild(node)
现在我需要的是在节点离开屏幕时调用 GameOver()。
非常感谢每一个回答!
在您的场景中,您必须记住要检查的节点的引用,然后在 update
方法中,您只需执行以下操作:
if node.position.y < -node.size.height/2.0 {
node.removeFromParent()
gameOver()
}
编辑:
class MyScene: SKScene {
// ....
//here is the list of nodes which you want to check
var nodesToCheck = [SKSpriteNode]()
//here is your spawn function
func spawnNewNode() {
var node = SKSpriteNode()
let nodeTexture = SKTexture(imageNamed: "node")
nodeTexture.filteringMode = .Nearest
node = SKSpriteNode(texture: nodeTexture)
let nodeFalling = SKAction.moveToY(-70, duration: 1.6)
let nodeRemoving = SKAction.removeFromParent()
node.runAction(SKAction.sequence([nodeFalling, nodeRemoving]))
self.addChild(node)
nodesToCheck.append(node)
}
//and here is the update method
override func update(currentTime: NSTimeInterval) {
super.update(currentTime)
// ... every other update logic
for node in nodesToCheck {
if node.position.y < -node.size.height/2.0 {
node.removeFromParent()
gameOver()
}
}
}
func gameOver() {
println("Damn!")
}
}
当节点不再是场景成员时,不要忘记从 nodeToCheck
数组中删除节点。