从函数Swift外部访问常量

时间:2016-07-03 00:08:26

标签: ios swift func

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

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

}

1 个答案:

答案 0 :(得分:0)

有几种不同的方法可以正确实现它。一种选择是创建一个类级别变量,它是一个类型为.et_mobile_menu { overflow-y:scroll!important; max-height:80vh!important; -overflow-scrolling:touch!important; -webkit-overflow-scrolling:touch!important; } 的数组,并在您在该函数中创建它们时将对象附加到它。

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) 的类级变量并在addObject函数中设置

例如:

SKSpriteNode

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

// Declaring class level variable
var myNode: SKSpriteNode?