user3765290
user3765290

Reputation: 21

Make a SKAction with perform selector BUT with parameters

I want to create a wall that increases size. But when I increase the size the Physics body dosn´t follow (It keeps the same size). So I have an idea that a SKAction that repeats forever perform a selector that resizes my physics body depending of the size of the object.
So I wanted to make a code like:

-(void) sizeChange:(SKSpriteNode *)sprite{
    sprite.physicsbody = [SKPhysicsBody bodyWithRectangleOfSize: sprite.size];
}

Now I want to run a SKAction that perform selector with this. So I wrote this:

SKAction *perform = [SKAction performSelector:@selector(sizeChange:) withObject: sprite onTarget: self];
[self runAction:[SKAction repeatActionForever:perform]];


The SKAction down´t work with "WithObject". How do I add a parameter to a performSelector SKAction? Thank you!

EDIT:
Basically I want the SKAction equivalence of:

[self performSelector:@selector(sizeChange:) withObject:sprite];

Or a way to repeat the code before forever. Thank You!

Upvotes: 2

Views: 1151

Answers (2)

John Houston
John Houston

Reputation: 97

This was bothering me for a while, but there is a simple workaround for this problem.

First create SKAction to repeat forever like this (in the case you need to delay method execution):

-(void)resizingMySprite
{ 
  //Set duration as long as you want. In my case I needed as low as possible)
  SKAction *wait = [SKAction waitForDuration:0.01];
  SKAction *performSelector = [SKAction performSelector:@selector(resizing)];
  SKAction *sequence = [SKAction sequence:@[performSelector, wait];
  SKAction *repeat = [SKAction repeatActionForever:sequence];

  [self runAction:repeat];
}

Then create method resizing and in it you run your method with arguments:

-(void)resizing
{
  //Let's say you wish to run your method on sprite called mySprite
 [self sizeChange:mySprite];
}

Upvotes: 0

Rafał Sroka
Rafał Sroka

Reputation: 40030

Check out customActionWithDuration:actionBlock: method of SKAction that creates an action that executes a block over a duration. You can specify your custom code in the action block.

+ (SKAction *)customActionWithDuration:(NSTimeInterval)seconds 
                           actionBlock:(void (^)(SKNode *node, 
                                          CGFloat elapsedTime))block

Alternatively, you can also use runBlock: method.

SKAction* blockAction = [SKAction runBlock:^
{
    // your code here 
}];

[someNode runAction:blockAction];

Upvotes: 4

Related Questions