使用waitforduration函数时出错

时间:2015-06-26 21:41:28

标签: swift sprite-kit

import SpriteKit

class GameScene: SKScene {


var greenBall = SKSpriteNode(imageNamed: "greenball")
 var redBall = SKSpriteNode(imageNamed: "redball")


override func didMoveToView(view: SKView) {

let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    borderBody.friction = 0
    self.physicsBody = borderBody

 backgroundColor = SKColor.purpleColor()




    //
    var bar0 = SKSpriteNode(imageNamed: "bar0")
     var bar1 = SKSpriteNode(imageNamed: "bar1")
     var bar2 = SKSpriteNode(imageNamed: "bar2")
     var bar3 = SKSpriteNode(imageNamed: "bar3")
     var bar4 = SKSpriteNode(imageNamed: "bar4")
     var bar5 = SKSpriteNode(imageNamed: "bar5")

   var bars = SKSpriteNode(imageNamed: "bar\(arc4random_uniform(6))")




    var bartest = SKSpriteNode(imageNamed: "bar0")
    bartest.position = CGPoint(x: frame.midX, y: frame.size.height * 0.633)
    addChild(bartest)







    var barra = SKShapeNode(rectOfSize: CGSize(width: frame.size.width, height: 10))
    barra.name = "bar"
    barra.fillColor = SKColor.whiteColor()
    barra.position = CGPoint(x: frame.midX, y: frame.midY)




    greenBall.position = CGPoint(x: frame.midX-100, y: frame.midY)
    greenBall.yScale = 2
    greenBall.xScale = 2

    redBall.position = CGPoint(x: frame.midX+100, y: frame.midY)
    redBall.yScale = 2
    redBall.xScale = 2


    self.addChild(redBall)
    self.addChild(greenBall)
    self.addChild(barra)




    func makeBars() {

        var randomTopOrBot = arc4random_uniform(2)
        if randomTopOrBot == 0 {

            var MaxY = frame.size.height * 0.951
            var MinY = (frame.size.height * 0.633)-bars.size.height
            var Range = CGFloat(MaxY - MinY)
            var barsSpawnX = (frame.midX)
            var barSpawnY =  CGFloat(arc4random_uniform(UInt32(Range)))
            bars.position = CGPoint(x: frame.midX, y: barSpawnY)
            addChild(bars)

        } else {

            var MaxY = frame.size.height * 0.951
            var MinY = (frame.size.height * 0.633)-bars.size.height
            var Range = CGFloat(MaxY - MinY)
            var barsSpawnX = (frame.midX)
            var randomBetweenRange =  CGFloat(arc4random_uniform(UInt32((Range))))
            bars.position = CGPoint(x: frame.midX, y: (MinY+randomBetweenRange))
            println(frame.size.height * 0.633)
            addChild(bars)

        }
    }



   let wait = SKAction.waitForDuration(3, withRange: 2)
    let spawn = SKAction.runBlock { makeBars <--- this is where it's saying the problem is()

    }

   let sequence = SKAction.sequence([wait, spawn])
    self.runAction(SKAction.repeatActionForever(sequence))

编辑:所以基本上我有2个产卵位置我选择,我随机决定两者之间的随机数,现在我想要将定时器应用于该功能makeBars,但它告诉我,我无法引用本地函数

这给了我一个错误,说明&#34;不能用来自另一个本地函数的捕获来引用本地函数&#34;。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您在makerbars()方法中声明了didMoveToView方法。所以这毕竟是一个函数嵌套问题,考虑到完整的代码,我能够看到真正的问题。 将您的代码更改为以下修改和更正的版本:

导入SpriteKit

类GameScene:SKScene {

var greenBall = SKSpriteNode(imageNamed: "greenball")
var redBall = SKSpriteNode(imageNamed: "redball")


override func didMoveToView(view: SKView) {

    let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    borderBody.friction = 0
    self.physicsBody = borderBody

    backgroundColor = SKColor.purpleColor()




    //
    var bar0 = SKSpriteNode(imageNamed: "bar0")
    var bar1 = SKSpriteNode(imageNamed: "bar1")
    var bar2 = SKSpriteNode(imageNamed: "bar2")
    var bar3 = SKSpriteNode(imageNamed: "bar3")
    var bar4 = SKSpriteNode(imageNamed: "bar4")
    var bar5 = SKSpriteNode(imageNamed: "bar5")






    var bartest = SKSpriteNode(imageNamed: "bar0")
    bartest.position = CGPoint(x: frame.midX, y: frame.size.height * 0.633)
    addChild(bartest)







    var barra = SKShapeNode(rectOfSize: CGSize(width: frame.size.width, height: 10))
    barra.name = "bar"
    barra.fillColor = SKColor.whiteColor()
    barra.position = CGPoint(x: frame.midX, y: frame.midY)




    greenBall.position = CGPoint(x: frame.midX-100, y: frame.midY)
    greenBall.yScale = 2
    greenBall.xScale = 2

    redBall.position = CGPoint(x: frame.midX+100, y: frame.midY)
    redBall.yScale = 2
    redBall.xScale = 2


    self.addChild(redBall)
    self.addChild(greenBall)
    self.addChild(barra)



    let wait = SKAction.waitForDuration(3, withRange: 2)
    let spawn = SKAction.runBlock { self.makeBars() }
    let sequence = SKAction.sequence([wait, spawn])
    self.runAction(SKAction.repeatActionForever(sequence))
}


func makeBars() {
    var bars = SKSpriteNode(imageNamed: "bar\(arc4random_uniform(6))")
    var randomTopOrBot = arc4random_uniform(2)
    if randomTopOrBot == 0 {

        var MaxY = frame.size.height * 0.951
        var MinY = (frame.size.height * 0.633)-bars.size.height
        var Range = CGFloat(MaxY - MinY)
        var barsSpawnX = (frame.midX)
        var barSpawnY =  CGFloat(arc4random_uniform(UInt32(Range)))
        bars.position = CGPoint(x: frame.midX, y: barSpawnY)
        addChild(bars)

    } else {

        var MaxY = frame.size.height * 0.951
        var MinY = (frame.size.height * 0.633)-bars.size.height
        var Range = CGFloat(MaxY - MinY)
        var barsSpawnX = (frame.midX)
        var randomBetweenRange =  CGFloat(arc4random_uniform(UInt32((Range))))
        bars.position = CGPoint(x: frame.midX, y: (MinY+randomBetweenRange))
        println(frame.size.height * 0.633)
        addChild(bars)

    }
}


}