从外部函数访问常量 Swift

Access Constant from outside Function Swift

我有一个游戏 运行 是这样的: class 文件是 运行 并使用函数 "addObject()" 将对象添加到屏幕。然后物体从屏幕顶部掉落到底部,玩家必须尝试通过点击它来接住物体。但是,该对象在 addObject 函数中声明(作为 SKSpriteNode)。所以当我去给游戏添加"touch controls"函数的时候,我无法引用这个对象。我该怎么做才能解决这个问题?这是我的一些代码:

class GameSceneTest:SKScene,SKPhysicsContactDelegate {

var ObjectDestroyed = 0
let label = SKLabelNode(fontNamed: "Title 1")
var money: Int = 0

override func didMoveToView(view: SKView) {

    backgroundColor = SKColor.clearColor()
    physicsWorld.gravity = CGVectorMake(0, 0)
    physicsWorld.contactDelegate = self


    //Change duration
    runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(addObject), SKAction.waitForDuration(1)])
        ))

}

func random() -> CGFloat {
    return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}

func random(min min: CGFloat, max: CGFloat) -> CGFloat {
    return random() * (max - min) + min
}

func addObject() {


    let Object = SKSpriteNode(imageNamed: "Object\(arc4random_uniform(6) + 1).png")
    Object.size = CGSize(width: 50, height: 50)
    Object.physicsBody = SKPhysicsBody(rectangleOfSize: Object.size)
    Object.physicsBody?.dynamic = true

    //Determine where to spawn Object across y axis

    let actually = random(min: Object.size.width/2, max: size.width - Object.size.width/2)

    //Position Object slightly off screen along right edge
    //and along random y axis point
    //Object.position = CGPoint(x: size.width + Object.size.width/2 , y: actually)

    Object.position = CGPoint(x: actually, y: size.height + Object.size.height/2)

    //Add the Object to the scene

    addChild(Object)

    //Determines speed of Object (edit later to where speed depends on type of Object)

    let actualDuration = random(min: CGFloat(4), max: CGFloat(5))

    //Create the Actions

    let actionMove = SKAction.moveTo(CGPoint(x: actually, y: gem.size.height/2), duration: NSTimeInterval(actualDuration))

    let actionMoveDone = SKAction.removeFromParent()

    let loseAction = SKAction.runBlock() {
        let reveal = SKTransition.flipHorizontalWithDuration(0.5) //Change to flipe vertical
        let gameOverScene = GameOverScene(size: self.size, won: false)
        self.view?.presentScene(gameOverScene, transition: reveal)
    }



    Object.runAction(SKAction.sequence([actionMove, loseAction, actionMoveDone]))
}



override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   let touch = touches.first!

    if Object.containsPoint(touch.locationInNode(self)) {
    Object.removeFromParent()
        print("touched")
    }
} 

}

您可以通过多种不同的方式正确实施此操作。一种选择是制作一个 class 级别变量,它是类型 SKSpriteNode 的数组,并在您在该函数中创建对象时将对象附加到它。

// Declare an array variable at the class level
var objectArray = [SKSpriteNode]()
// Then in the function after you create the object and configure it
objectArray.append(object)

另一种替代方法是使用将新对象添加到的字典。这仅取决于再次引用对象时需要对对象执行的操作。

注意:此答案假定您在任何给定时间都需要引用多个对象。如果你一次只需要引用一个,你可以只创建一个 class 类型的 SKSpriteNode 级别变量并在你的 addObject 函数中设置它

例如:

// Declaring class level variable
var myNode: SKSpriteNode?

调用 addObject 方法时,将 myNode 设置为您创建和配置的对象

// When you go to use myNode, just unwrap it to make sure that it has a value
if let unwrappedNode = self.myNode {
    // Do something with unwrappedNode
}