0

I am trying to run an action, but i want to decide which. for example i have

[bullet runAction:bullet1];

I want to be able to manipulate the variable its accessing something like

[bullet runAction:bullet%d, i];
3

4 Answers 4

3

use an array of actions, and use the index to access them

NSArray bulletActions = @[bullet1, bubble2];
[bullet runAction:bulletActions[0]];

I think it will serve your needs

Sign up to request clarification or add additional context in comments.

3 Comments

Can you please explain what is bullet1 and bubble2, and how runAction: calls that bulletActions[0] ??
@Anoop bullet1,bulle2 and so on. i suppose they are CCAction, i guess he has many action and what to run them by indexing, so that's was my proposed solution storing them in an array and runAction with bulletActions[0] will run bullet1 in this case
I dont have much idea about CCActions :(
1

You need to use selector

 SEL selector=NSSelectorFromString([NSString stringWithFormat:@"bullet%d", i]);
[self performSelector:selector];

From this you can call a method named bullet1, buttet2 etc, if i is provided as 1, 2 etc

-(void)bullet1{
    NSLog@"bullet 1 called";
}


-(void)bullet2{
    NSLog@"bullet 2 called";
}

-(void)bullet<your integer value>{
    NSLog@"bullet <your integer value> called";
}

5 Comments

this is correct but he should not use this design to do what he is doing, using runtime features for this is not a nice choice. but +1 anyway
@Ultrakorne: Can you explain why we should not use dynamic method calls? I know dynamism of Obj-C makes it better than many other language.
because he just want to have a collection of variables accessible with an index, it is just that simple that i dont feel the need of overcomplicate that.
@Ultrakorne: I would say, this is a given feature of obj-c, we should use it.
i'm not against the feature, but the use in this particular case. you have to create N methods, where N is the amount of actions. they can be created programmatically in a for loop, added in an array and use the array as index.
1

EDIT: Sorry, after looking at my answer I saw some flaws and wrote this as a better way to accomplish this.

The best possible outcome for this is to create an array that holds all of your actions. i.e.

NSArray actionArray = [[NSArray alloc] initWithItems:bullet1, bullet1, bullet3, nil];

And then you can run create a method to run the action:

- (void)bulletAction:(int)numberToRun {

     [bullet runAction:[actionArray objectAtIndex:numberToRun]];

}

This can be called by using the code:

[self bulletAction:0];

Where 0 is whatever number you want to run.

Comments

0

you cant do what you are trying to do in the question, instead pass it an array of bullet objects and also pass it the value for which element you want to access of that array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.