2015-11-17 4 views
0

Я пытаюсь сделать простую игру с пузырьками. У меня есть код 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])) 
} 

Я действительно пытаюсь выполнить эту работу. Заранее спасибо!

+0

Вы видите печать? что именно не работает? компиляции или времени выполнения? – giorashc

+0

Он компилируется и работает. Но всякий раз, когда я нажимаю спрайт (пузырь), он не исчезает. –

+0

Вы видите печать в консоли? вы уверены, что правильно именовали узел? Это поможет, если вы разместите код создания узла – giorashc

ответ

1

Если вы хотите использовать свойство имени узла, вы должны установить его на что-то. Время выполнения не может определить имя переменной как имя. Таким образом, в функции bubblesInView после

let bubble1 = SKSpriteNode(imageNamed: "bubble_green.png") 

вы должны сделать

bubble1.name = "bubble1" 
+0

Большое спасибо! –

0

Вы просто забыли имя узла:

//create the sprite 
let bubble1 = SKSpriteNode(imageNamed: "bubble_green.png") 
bubble1.name = "bubble1" 
+0

Спасибо большое! –

Смежные вопросы