简体   繁体   中英

Problems making a property from a SKAction (SpriteKit)

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM