Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift spritekit - cannot invoke 'runAction' with an argument of type 'SKAction!'

I am trying to make a SKSpriteNode bounce across the screen, but when I run it, I get an error (on the first line):

cannot invoke 'runAction' with an argument of type 'SKAction!'

I have no idea what is causing this. I've pasted the entire runAction code below:

self.runAction(SKAction.sequence(
        [ SKAction.repeatAction(SKAction.sequence(
            [
                {self.updatePath()},
                SKAction.followPath(path.CGPath, duration: 3.0)
            ]), count: numberOfBounces),
            {actionMoveDone()}
        ]
    ))

Thanks in advance!

like image 863
Sarah Pierson Avatar asked Dec 05 '25 13:12

Sarah Pierson


2 Answers

Changing an action's argument will have no effect on the action once it has started. As such, your actions will use the same, original path repeatedly during the execution of the series of actions. Also, you are running the action on self, which I assume is an SKScene subclass, when followPath actions are typically run on sprites.

If you want a sprite to follow a different path during each iteration, you will need to create a new action. One way to do that is to create a function that updates the path, creates a new action, and then calls itself at the completion of the current action. For example,

func createAction() {
    updatePath()
    sprite.runAction(SKAction.followPath(path.CGPath, asOffset: false, orientToPath: true, duration: 3.0),
        completion: {
            if (++self.count < numberOfBounces) {
                self.createAction()
            }
            else {
              // Run this after running the action numberOfBounces times
              self.actionMoveDone()
           }
        }
    )
}

and start the series by calling the function

createAction()
like image 90
0x141E Avatar answered Dec 07 '25 17:12

0x141E


Your code is a little messy.

First of all you dont really need that first sequence. It makes more sense to use a completion block.

Secondly, why the curly braces inside your inner sequence? Is self.updatePath() an SKAction? A sequence can only be an array of SKActions. If it's an SKAction you don't need any curly braces, otherwise you need to use SKAction.runBlock({})

So based off your code.. I'm thinking this is what you're trying to do..

self.runAction(
    SKAction.repeatAction(
        SKAction.sequence([
            // guessing self.updatePath isnt an SKAction, but I don't know
            SKAction.runBlock({ self.updatePath() }),

            SKAction.followPath(path.CGPath, duration: 3.0)

        ]), count: numberOfBounces),
    completion:{
        // also not sure if this is an SKAction, or something else
        actionMoveDone()
})
like image 44
hamobi Avatar answered Dec 07 '25 15:12

hamobi