Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SKAction run method with argument?

I am trying to run SKAction *action = [SKAction performSelector:(SEL) onTarget:(id)]; and pass in a selector, which is a method that takes one argument. However, XCode is only allowing me to put in performSelector:@selector(placeCoin:) but it will not allow me to pass a CGPoint argument in with the selector. How am I supposed to do this? Do I seriously have to run it with an SKAction runBlock statement? Seems like there should be a way to do this..


2 Answers

As far as I know, you cannot pass any arguments with performSelector:onTarget: in SpriteKit. Using blocks is the better way to go.

like image 142
sangony Avatar answered May 19 '26 23:05

sangony


This question is a bit old, but I found a simple way to do this.

First create your action (in my case I needed action that repeats forever):

-(void)MyAction
{
   SKAction *wait = [SKAction waitForDuration: 0.01];
   SKAction *performSelector = [SKAction performSelector:@selector(myMethod1) onTarget:self];
   SKAction *sequence = [SKAction sequence:@[performSelector, wait]];
   SKAction *repeat = [SKAction repeatActionForever: sequence];
}

Then create myMethod1 and run desired method with arguments:

-(void)myMethod1
{
  [self myMethod2Arg1:something1 Arg2:something2 Arg3:something3];
}

Now, all you need to do is run action on anything and method with arguments will fire up.

like image 23
John Houston Avatar answered May 19 '26 23:05

John Houston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!