Astrum
Astrum

Reputation: 375

detecting if a spritenode exists in swift

I am trying to detect if one of the nodes that I have made through a subclass of SKNode (called Achievements) exists and if it doesn't exist then i'm trying to turn off a boolean variable.

What I use to locate the SKShapeNode (called "Indicator")

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

            }
        }
    }
}

I have enumerated through the nodes specifically and tried searching for it but it doesn't seem to do anything. what am I doing wrong?

Here is my subclass. I have many of them named achievement displayed in my scene.

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)
    }

  }
}  

EDIT 1

As you can see from the image below, there are a number of different achievement nodes each with their individual names and unlock criteria, when an achievement becomes unlocked it makes an indicator and changes the graphic, which is seen for the two X nodes here with the coloured red circle in the top right corners of each of them which is the "indicator" (if you look at the subclass at the bottom of it the creation of the indicator is there)

Now as you can seen the big red button in the bottom right hand corner of the picture is the achievement menu button which also has an indicator which is controlled by the bool variable (menuAchieveNotificationOn) what i'm trying to achieve is once the achievements with the indicators have each been pressed they are removed from the node.

what i'm trying to do is search each of the nodes to see if the indicator still exists if not I want to turn the variable (menuAchieveNotificationOn) to false.

enter image description here

Upvotes: 1

Views: 1150

Answers (1)

Steve Ives
Steve Ives

Reputation: 8134

You should be able to use this:

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

EDIT: to run some code for EVERY "indicator" node in the scene. If any are found, achievementsFound is set to true:

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

Although this seems too simple, so I might have misunderstood your aim.

Upvotes: 3

Related Questions