如何使用具有相同名称的多个节点,每个节点与环境分别交互

时间:2016-05-16 11:14:48

标签: swift sprite-kit

我的问题是,每次生成新笔记时都会更改上一个笔记的名称,以便我无法将其删除或更改其操作如何解决此问题我的目标是触摸屏幕上任何位置的每个点都将被删除。但是目前它只能用1个点才能生成下一个Dot,但是我只想到一种方法可以解决这个问题,连续每5个点写一个单独的注释但是这更像是一个复制和粘贴代码然后其他任何东西我宁可避免这个

enter image description here

  var enemy1 = SKSpriteNode()
var enemy2 = SKSpriteNode()
var enemy3 = SKSpriteNode()
var enemy4 = SKSpriteNode()
let wate = SKAction.waitForDuration(3)

func dot (){


    let SK = SKAction.runBlock{
    self.runAction(SKAction.repeatActionForever(
        SKAction.sequence([
            SKAction.runBlock{self.spawnBlueDot()},
            SKAction.waitForDuration(1)])))

    self.runAction(SKAction.repeatActionForever(
        SKAction.sequence([
            SKAction.runBlock{self.spawnBluelop()},
            SKAction.waitForDuration(1)])))

    self.runAction(SKAction.repeatActionForever(
        SKAction.sequence([
            SKAction.runBlock{self.spawnGrennDot()},
            SKAction.waitForDuration(1)])))

    self.runAction(SKAction.repeatActionForever(
        SKAction.sequence([
            SKAction.runBlock{self.spawnGrennlop()},
            SKAction.waitForDuration(1)])))


    }



 runAction(SKAction.sequence([wate,SK]))






}



func spawnBlueDot() {
    // 2
    enemy1 = SKSpriteNode(imageNamed: "Oval 2@1,7x")
    // 3
    enemy1.name = "enemy1"
    // 4
    enemy1.position = CGPoint(x: frame.size.width / 2 - 400, y: frame.size.height)
    // 5
    addChild(enemy1)
    scor += 1
    enemy1.runAction(SKAction.moveToY(-100, duration: 4))
    label?.text = "\(scor)"




}

func spawnBluelop() {
    // 2
    enemy2 = SKSpriteNode(imageNamed: "2@1,7xj")
    // 3
    enemy2.name = "enemy2"
    // 4
    enemy2.position = CGPoint(x: frame.size.width / 2 - 100, y: frame.size.height / -900)
    // 5
    addChild(enemy2)

    enemy2.runAction(SKAction.moveToY(2000, duration: 4))



}

func spawnGrennDot() {
    // 2
    enemy3 = SKSpriteNode(imageNamed: "Oval 3@1,7x")
    // 3
    enemy3.name = "enemy3"
    // 4
    enemy3.position = CGPoint(x: frame.size.width / 2 + 400, y: frame.size.height)
    // 5
    addChild(enemy3)

    enemy3.runAction(SKAction.moveToY(-100, duration: 4))



}



func spawnGrennlop() {
    // 2
    enemy4 = SKSpriteNode(imageNamed: "3@vjhv")
    // 3
    enemy4.name = "enemy4"
    // 4
    enemy4.position = CGPoint(x: frame.size.width / 2 + 100 , y: frame.size.height / -900)
    // 5
    addChild(enemy4)

    enemy4.runAction(SKAction.moveToY(2000, duration: 4))



 }
 override func didMoveToView(view: SKView) {

    dot()

}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


  for touch in touches{

        let location = touch.locationInNode(self)
        if pause!.containsPoint(location){
            print("help")


            enemy2.removeAllActions()
            enemy3.removeAllActions()
            enemy3.removeAllActions()
            enemy4.removeAllActions()

            pause?.runAction(SKAction.sequence([ac,ac1]))


        }


    }




    for touch in touches{

        let location = touch.locationInNode(self)
        if enemy1.containsPoint(location){
            print("help")


            enemy1.removeFromParent()


        }
    }







    }

1 个答案:

答案 0 :(得分:1)

使用

let touchedNode = nodeAtPoint(location)

而不是

if enemy1.containsPoint(location)

这将返回一个SKNode,您可以删除它,而无需单独检查每个敌人。