我正在尝试制作一个简单的泡泡点击游戏。我在代码中创建了一个 SKSpriteKid 节点。问题是我希望当用户点击 Sprite 时 Sprite 会消失。我似乎无法删除节点。我该如何解决这个问题?
我创建了一个名为 bubble1 的 Sprite 。
func bubblePressed(bubble1: SKSpriteNode) {
print("Tapped")
bubble1.removeFromParent()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let touchLocation = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(touchLocation)
if (touchedNode.name == "bubble1") {
touchedNode.removeFromParent()
print("hit")
}
}
}
//the node's creation.
func bubblesinView() {
//create the sprite
let bubble1 = SKSpriteNode(imageNamed: "bubble_green.png")
//spawn in the X axis min is size, max is the total height minus size bubble
let width_bubble_1 = random(min: bubble1.size.width/2, max: size.width - bubble1.size.height/2)
//y: size.height * X, X is 0 at bottom page to max
//spawn position, let x be random
bubble1.position = CGPoint(x: width_bubble_1, y: size.height)
// Add the monster to the scene
addChild(bubble1)
// Determine speed of the monster from start to end
let bubblespeed = random(min: CGFloat(1.1), max: CGFloat(4.0))
//this tell the bubble to move to the given position, changeble x must be a random value
let moveAction = SKAction.moveTo(CGPoint(x: width_bubble_1, y: size.height * 0.1), duration: NSTimeInterval(bubblespeed))
let actionMoveDone = SKAction.removeFromParent()
bubble1.runAction(SKAction.sequence([moveAction, actionMoveDone]))
}
我真的很努力让这项工作成功。提前致谢!
最佳答案
如果你想使用节点的 name 属性,你应该将其设置为某个值。运行时无法将变量名称识别为名称。所以在bubblesInView函数之后
let bubble1 = SKSpriteNode(imageNamed: "bubble_green.png")
你应该这样做
bubble1.name = "bubble1"
关于ios - (SpriteKit + Swift 2) - 当用户点击它时移除 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33762221/