Manish Agrawal
Manish Agrawal

Reputation: 11026

schedule event with multiple arguments in cocos2D

I need to schedule an event in cosos2D with various arguments, I have tried searching on net but did not able to get any solution out of it. I can use

[self schedule:@selector(move:) interval:0.3];

but How should I pass arguments to it like:

[self schedule:@selector(move:withPoint:) interval:0.3];

to access this function.

-(void)move:(id)object withPoint:(CGPoint)point{
}

Upvotes: 1

Views: 3220

Answers (2)

CodeSmile
CodeSmile

Reputation: 64477

What you want to do is not possible.

Since the method you want to schedule is in the same class, you can just use an instance variable. You can schedule the selector like so and the method needs to have this signature (takes only a ccTime parameter):

[self schedule:@selector(move:) interval:0.3];

-(void)move:(ccTime)delta
{
}

To get access to the variables you need in this method you add them as instance variables:

@interface MyClass : CCNode
{
   id moveObject;
   CGPoint movePoint;
}
@end

Then you can use this variables in the update method, and/or modify them in other methods as well.

-(void)move:(ccTime)delta
{
    // read or modify moveObject and movePoint as needed
}

Upvotes: 2

user529758
user529758

Reputation:

You should use some kind of enclosure (an array/dictionary, a struct, etc.) in an instance variable to achieve this. For example:

placeholderDict = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSValue valueWithCGPoint:point], @"point",
    theObject, @"object",
    nil];
[self schedule:@selector(moveWithPointWrapper) interval:0.3];

- (void)moveWithPointWrapper
{
    CGPoint pt = [(NSValue *)[placeholderDict objectForKey:@"point"] CGPointValue];
    id obj = [placeholderDict objectForKey:@"object"];
    [self move:obj withPoint:pt];
}

Hope this helps.

EDIT: as @learncocos2d pointed out, it's also sufficient to create single instance variables (the object, the CGPoint...) and you don't even need the overhead of the dictionary.

Upvotes: 2

Related Questions