簡體   English   中英

當我所有的節點都在本地聲明時,不知道如何更新分數標簽節點

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

我創建了一個游戲,其中球穿過障礙物,障礙物掉落到任意位置,當球穿過障礙物時,分數應增加1,但是對象的所有結點都在局部聲明,而不是如何增加在沒有創建太多節點的情況下得分。

這是一個示例:我的功能之一:

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

然后我有一個函數,該函數每1秒被調用一次,該函數生成一個隨機數,以調用隨機顯示對象的6個函數。 如果即使我必須更改自己的代碼,您仍然知道該怎么做,請提供幫助。 謝謝。

您在這里有一些選擇,以及一些代碼設計決策。

首先,任何重復的代碼都應放入單獨的方法中,而不要放在if / else中。 從外觀上看,您的前8行左右是重復的。 所以做一個新方法:

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

這樣,您可以在兩個if / else條件下都調用此方法:

var rand = arc4random_uniform(2) + 1

var wall = setUpWall()

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

其次,為了能夠訪問牆或方法之外的任何變量,可以在類頂部將它們聲明為實例變量。

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

這是一種方法,另一種方法是使用某種地圖或字典將本地變量保存在其中。 字典具有一個鍵值對,因此您可以將一個特定的鍵分配為變量的名稱,並為它分配一個正確的對象作為值

您可以在此處閱讀有關它們的信息,但我將在下面提供示例: 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>()

在您的方法中,當您創建要在方法之外訪問的任何精靈時,請將它們添加到字典中,並給它們指定一個您將知道的名稱:

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
}

就像我之前說過的那樣,您需要確保所有內容都符合我添加的語法,因為我沒有親自在Xcode中對其進行測試。 另外,由於我不知道整個項目的設計方式,因此一種方法對您而言可能比另一種更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM