简体   繁体   中英

don't know how to update the score label node , when all my nodes are declared locally

i created a game where a ball passes through obstacles that fall down in random positions , and when the ball passes through an obstacle the score should be increased by one , but all the nodes of objects are declared locally and don't how to increase the score with out creating so many nodes.

here is an example: of one of my functions:

func leftObject(){
        var rand = arc4random_uniform(2) + 1
        if rand == 1
        {
            var bigWall = SKSpriteNode(imageNamed: "shortwall")
             bigWall.position = CGPointMake(CGRectGetMinX(self.frame) + bigWall.size.width / 2, CGRectGetMaxY(self.frame))

            var moveObjects = SKAction.moveByX(0, y: -self.frame.size.height * 2, duration: NSTimeInterval(self.frame.size.height / 100))
            var removeObjects = SKAction.removeFromParent()
            var moveAndRemoveObjects = SKAction.sequence([moveObjects,removeObjects])
            bigWall.runAction(moveAndRemoveObjects)
            bigWall.physicsBody = SKPhysicsBody(rectangleOfSize: bigWall.size)
            bigWall.physicsBody?.dynamic = false
            bigWall.physicsBody?.categoryBitMask = objectGroup



        }

        else
        {
            var tallWall = SKSpriteNode(imageNamed: "shortwall")
            tallWall.position = CGPointMake(CGRectGetMinX(self.frame) + tallWall.size.width / 2, CGRectGetMaxY(self.frame))
            var moveObjects = SKAction.moveByX(0, y: -self.frame.size.height * 2, duration: NSTimeInterval(self.frame.size.height / 100))
            var removeObjects = SKAction.removeFromParent()
            var moveAndRemoveObjects = SKAction.sequence([moveObjects,removeObjects])
            tallWall.runAction(moveAndRemoveObjects)
            tallWall.physicsBody = SKPhysicsBody(rectangleOfSize: tallWall.size)
            tallWall.physicsBody?.categoryBitMask = objectGroup
            tallWall.physicsBody?.dynamic = false



            movingObjects.addChild(tallWall)
        }

and then i have a function that is being called every 1 second that generates a random number to call the 6 functions that displays the objects randomly. if you know a way to do it even if i have to change my code , please help. Thank you.

you have a few options here, as well as some code design decisions.

First, any code that is duplicate should be put into a separate method instead of sitting in your if/else. From the looks of it, your first 8 lines or so are duplicate. so make a new method:

func setUpWall() -> SKSpriteNode {
   var tallWall = SKSpriteNode(imageNamed: "shortwall")
   //all other lines that are duplicate;
}

this way you call this method in both your if/else conditions:

var rand = arc4random_uniform(2) + 1

var wall = setUpWall()

if rand == 1 {
 //any unique stuff
} else {
 //other unique stuff
}

secondly, to be able to access your wall or any variable outside of the method you can declare them as instance variables at the top of the class

 var wall = SKSpriteNode() //its declared as var, so you can update it inside the method call and still access it outside the method

this is one way, another way would be to have a map or dictionary of some sort to hold your local variable in it. The dictionary has a key value pair, so you can assign a specific key as the name of your variable, and assign the right object to it as a value

you can read about them here, but I'll provide an example below: https://developer.apple.com/library/mac/documentation//General/Reference/SwiftStandardLibraryReference/Dictionary.html

//declare your dictionary as an instance variable (a class variable, not inside a method)
//pseudocode, you gotta make sure the syntax is correct and it compiles

var myDict = Dictionary<String: SKSpriteNode>()

and in your method when you create any sprites that you want to access outside of the method, add them to the dictionary and give them a name that you will know:

var bigWall = SKSpriteNode(imageNamed: "shortwall")
myDict["myWall1"] = bigWall

//then to get it later you can say:
let myWallFromOtherMethod = myDict["myWall1"]


//but I would check if the key exists first
// you don't want to get a nil error
// so use this syntax

if let myWallFromOtherMethod = myDict["myWall1"] {
// now val is not nil and the Optional has been unwrapped, so use it
}

Like I said before, you need to make sure this all complies with the syntax I've added because I haven't personally tested it in Xcode. Also, since I don't know how your overall project is designed, one method may work better for you than the other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM