检测spritenode是否存在于swift中

时间:2017-06-14 09:47:21

标签: ios swift xcode sprite-kit

我正在尝试检测我通过SKNode (称为成就)的子类创建的其中一个节点是否存在,如果它不存在,那么我正在尝试关闭布尔变量。

我用什么来定位SKShapeNode(名为"指标")

func checkIndicatorStatus() {

    moveableArea.enumerateChildNodes(withName: "Achievement") {
        (node, stop) in

         let Indicate = node

        Indicate.enumerateChildNodes(withName: "Indicator") {
            node, stop in

            if let Achievement = node as? Achievements {

                    menuAchieveNotificationOn = false

            }
        }
    }
}

我已经通过节点专门枚举并尝试搜索它但它似​​乎没有做任何事情。我做错了什么?

这是我的子类。我的很多人都在我的场景中展示了成就。

class Achievements: SKNode {

//Nodes used throughout the SKNode class
var achievementLabel = SKLabelNode()
var achievementTitleLabel = SKLabelNode()
var achievementNode = SKSpriteNode()
var notifyCircle = SKShapeNode()

//Amount Variables used as Achievement Properties
var image: String = ""
var information: String = ""
var title: String = ""
var amount = 0
var neededAmount = 0
var notification:Bool = false
var stage = 0


func getachievementData(AchName: String) {

    let getDataRequest:NSFetchRequest<Achievement> = Achievement.fetchRequest()
    getDataRequest.predicate = NSPredicate(format: "theSearchName == %@" , AchName)

    do {

        let searchResults = try CoreDatabaseContoller.getContext().fetch(getDataRequest)

        //print("number of results: \(searchResults.count)")
        for result in searchResults as [Achievement] {

            title = result.theName!
            information = result.theDescription!
            image = result.theImage!
            amount = Int(result.aAmount!)
            neededAmount = Int(result.aNeededAmount!)
            stage = Int(result.aStage!)

            if result.aHasBeenAchieved!.intValue == 1 {

                notification = true
            }

        }
    }

    catch {

            print("ERROR: \(error)")

    }

    createAchievement()
}

func createAchievement() {

    let tex:SKTexture = SKTexture(imageNamed: image)

    achievementNode = SKSpriteNode(texture: tex, color: SKColor.black, size: CGSize(width: 85, height: 85))  //frame.maxX / 20, height: frame.maxY / 20))
    achievementNode.zPosition = -10
    achievementSprites.append(achievementNode)


    self.name = "Achievement"
    self.addChild(achievementNode)
    self.zPosition = -11

    createAchievementLabels()

}

func createAchievementLabels() {

    achievementTitleLabel = SKLabelNode(fontNamed: "Avenir-Black")
    achievementTitleLabel.fontColor = UIColor.black;
    achievementTitleLabel.fontSize = 15 //self.frame.maxY/30
    achievementTitleLabel.position = CGPoint (x: 0, y: 50)
    achievementTitleLabel.text = "\(title)"
    achievementTitleLabel.zPosition = -9
    addChild(achievementTitleLabel)

    achievementLabel = SKLabelNode(fontNamed: "Avenir-Black")
    achievementLabel.fontColor = UIColor.black;
    achievementLabel.fontSize = 13 //self.frame.maxY/30
    achievementLabel.position = CGPoint (x: 0, y: -55)
    achievementLabel.text = ("\(amount) / \(neededAmount)")
    achievementLabel.zPosition = -9
    addChild(achievementLabel)

    if notification == true {

        notifyCircle = SKShapeNode(circleOfRadius: 10)
        notifyCircle.fillColor = .red
        notifyCircle.position = CGPoint(x: 30 , y: 35)
        notifyCircle.zPosition = 1000
        notifyCircle.name = "Indicator"
        addChild(notifyCircle)
    }

  }
}  

编辑1

从下图中可以看出,有许多不同的成就节点,每个节点都有各自的名称和解锁标准,当成就解锁后,它会生成一个指示器并更改图形,这可以看作是两个X这里的节点在每个节点的右上角有彩色的红色圆圈,这是&#34;指示符&#34; (如果你看一下它底部的子类,指标的创建就在那里)

现在您可以看到图片右下角的大红色按钮是成就菜单按钮,它还有一个指示器,由bool变量(menuAchieveNotificationOn)控制我试图实现的目标是一旦成就与指标都被按下,他们从节点中删除。

我想要做的是搜索每个节点,看看指标是否仍然存在,如果不是我想将变量(menuAchieveNotificationOn)变为false。

enter image description here

1 个答案:

答案 0 :(得分:3)

你应该能够使用它:

if let indicatorNode = childNode(withName: "//Indicator") as! SKShapeNode? {
        menuAchieveNotificationOn = false
    } else {
        menuAchieveNotificationOn = true
    }

编辑:为每个&#34;指标&#34;运行一些代码场景中的节点。如果找到任何,则achievementFound设置为true:

achievementsFound = false
enumerateChildNodes(withName: "//Indicator") { indicatorNode, _ in
    // Do something with indicatorNode
    achievementsFound = true
}

虽然这看起来太简单了,但我可能误解了你的目标。