Я пытаюсь сделать простую игру с пузырьками. У меня есть код SKSpriteKid в моем коде. Проблема в том, что я хочу, чтобы спрайты исчезли, когда пользователь нажал на нее. Я не могу удалить удаленный() узел. Как мне это решить?(SpriteKit + Swift 2) - Удалите спрайт, когда пользователь нажал на него
Я создал спрайт с bubble1 как имя.
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]))
}
Я действительно пытаюсь выполнить эту работу. Заранее спасибо!
Вы видите печать? что именно не работает? компиляции или времени выполнения? – giorashc
Он компилируется и работает. Но всякий раз, когда я нажимаю спрайт (пузырь), он не исчезает. –
Вы видите печать в консоли? вы уверены, что правильно именовали узел? Это поможет, если вы разместите код создания узла – giorashc