简体   繁体   English

从外部函数Swift访问常量

[英]Access Constant from outside Function Swift

I have a game that runs like this: The class file is run and uses the function "addObject()" to an add an object to the screen. 我有一个像这样运行的游戏:运行类文件,并使用函数“ addObject()”向屏幕添加对象。 The object then falls from the top of the screen to the bottom and the player has to try to catch the object by tapping on it. 然后,该对象从屏幕顶部掉落到底部,玩家必须尝试通过点击它来捕获该对象。 However, the object is declared (as an SKSpriteNode)in the addObject function. 但是,该对象在addObject函数中声明为SKSpriteNode。 So when I go to add the "touch controls" function to the game, I can't reference the object. 因此,当我向游戏中添加“触摸控件”功能时,无法引用该对象。 What can I do to fix this? 我该怎么做才能解决此问题? Here is some of my code: 这是我的一些代码:

class GameSceneTest: SKScene, SKPhysicsContactDelegate { 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")
    }
} 

} }

There are several different ways that you could correctly implement this. 有几种不同的方法可以正确实现此目的。 One option would be to make an class level variable that is an array of type SKSpriteNode and append the objects to it when you create them in that function. 一种选择是制作一个类级别变量,该变量是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)

Another alternative would be to use a dictionary that you add the new objects to. 另一种选择是使用添加新对象的字典。 It just depends on exactly what you need to do with the objects when you reference them again. 再次引用对象时,它仅取决于您需要如何处理这些对象。

Note: This answer assumes that you will have multiple objects that you need reference to at any given time. 注意:此答案假设您将在任何给定时间拥有多个需要引用的对象。 If you only need reference to one at a time, you can just make a class level variable of type SKSpriteNode and set it in your addObject function 如果您一次只需要引用一个,则只需创建一个类型为SKSpriteNode的类级变量,然后在addObject函数中进行设置即可

For Example: 例如:

// Declaring class level variable
var myNode: SKSpriteNode?

When your addObject method is called, set myNode equal to the object that you create and configure 调用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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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