2
votes

Again, probably a newbie question. I'm following the instructions I found in these two links, since they solve exactly the problem I have (modify the speed of a SKAction from outside its own method):

How to change duration of executed SpriteKit action

How to run or execute an SKAction from outside of the object?

In my case, have this SKAction:

SKAction * moveBall = [SKAction moveToY:0 duration:1];

[ball runAction:moveBall withKey:@"ball falling"]; 

I create the property, like this:

@property SKAction * moveBall;

And then I want to call it from the touchesBegan after touching a button, like this:

if ([node.name isEqualToString:@"Slow Down Button"]) {

        self.moveBall.speed = 0.5;

    }

moveBall.speed inside its own method is 1.0, but self.moveBall.speed indicates a speed of 0.0 (same for _moveBall.speed), so the property declaration is not working correctly. I tried several things, but so far I couldn't find what is missing.

Thanks in advance!

2

2 Answers

0
votes

This:

SKAction * moveBall = [SKAction moveToY:0 duration:1];

creates a local variable named moveBall. But your property is accessed through self.moveBall respetively directly through its auto-synthesized ivar named _moveBall.

The solution is one of these two:

self.moveBall = [SKAction moveToY:0 duration:1];

or

_moveBall = [SKAction moveToY:0 duration:1];

and then continue to use that variable for addChild: and other uses.

0
votes

You most likely need to spend some time reading about properties and pointers. But it's possible that you just aren't showing all the code you are using. The phrase "inside its own method" is confused.

SKAction *moveBall = is not the same as _moveBall, unless you have assigned that pointer to the property. But you are using the same name for a pointer and a property in your code, confusing things further.

I suspect you need to change:

SKAction * moveBall = [SKAction moveToY:0 duration:1];

to

_moveBall = [SKAction moveToY:0 duration:1];

and then only use _moveBall throughout your code. But make sure you read up on pointers and properties and understand the difference.