0

I'm trying to use SKAction.repeatActionForever to run a sequence consisting of an SKAction and a random delay between repeats.

override func didMoveToView(view: SKView) {
    let sequence = [
            SKAction.runBlock(spawnObstacle), //Missing argument for parameter 'queue' in call
            SKAction.waitForDuration(self.random(min: player.speed, max: player.speed * 2 as CGFloat))
    ]

    runAction(SKAction.repeatActionForever(SKAction.sequence(sequence)))

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



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

spawnObstacle is a function with no parameters that returns void, and player.speed is a CGFloat.

I'm getting the error Missing argument for parameter 'queue' in call on SKAction.runBlock(spawnObstacle). I think this has something to do with types, and that for some reason runBlock doesn't like spawnObstacle(), and is trying to call

func runBlock(_ block: dispatch_block_t,queue queue: dispatch_queue_t?) -> SKAction

instead of

func runBlock(_ block: dispatch_block_t) -> SKAction

I tried call SKAction.runBlock(spawnObstacle as dispatch_block_t) to see what would happen, and it got rid of the error, only to bring up another one:

SKAction.waitForDuration(self.random(min: player.speed, max: player.speed * 2 as CGFloat)) //Extra argument 'min' in call

I think this one is similar to the other one (which I'm not sure I actually solved, other than faking out the compiler). It seems like the compiler thinks that player.speed is not a CGFloat so it goes to random() instead of random(#min:CGFloat, max:CGFloat).

My question is: Why are these two errors occurring when the types are, as far as I can tell, correct?

EDIT: Here is spawnObstacle()

func spawnObstacle() -> Void{

    let obst = Obstacle(
        color: UIColor.redColor(),
        size: CGSize(width: 50.0,height: 50.0))

    let node = SKSpriteNode(color: obst.color, size: obst.size)

    node.position = CGPoint(x: size.width, y: 0 + obst.size.height / 2)

    addChild(node)

    let duration = player.speed

    let actionMove = SKAction.moveTo(CGPoint(x:0 - obst.size.width / 2,y:node.position.y), duration: NSTimeInterval(duration))
    let actionMoveDone = SKAction.removeFromParent()

    node.runAction(SKAction.sequence([actionMove,actionMoveDone]))
}
2
  • how is spawnObstacle declared and implemented?
    – CodeSmile
    Nov 20, 2014 at 23:46
  • Ok, I added spawnObstacle()
    – Iinferno1
    Nov 21, 2014 at 0:28

1 Answer 1

2

Swift sometimes emits errors that don't have anything to do with the real problem. In this case your problem is that SKAction.waitForDuration expects an NSTimeInterval but you are passing in a CGFloat. You need to construct an NSTimeInterval from the CGFloat.

let sequence = [
    SKAction.runBlock(spawnObstacle),
    SKAction.waitForDuration(NSTimeInterval(random(min: player.speed, max: player.speed * 2)))
]
1
  • Thanks a lot! I get so many nonsense debugger errors and couldn't find anything wrong with what the debugger found. I never thought that there actually were errors, but different from those shown! Thank god for the newer versions with those bugs fixed (currently coding on an older version of xcode)! Jul 12, 2015 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.