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];
user2121776
  • 123
  • 9
  • I don't think this is possible. Perhaps you can use tags instead. – aqua Mar 08 '13 at 11:34
  • @aqua: It is surely possilbe. – Anoop Vaidya Mar 08 '13 at 11:51
  • possible duplicate of [ObjC equivalent of PHP's "Variable Variables"](http://stackoverflow.com/questions/2283374/objective-c-equivalent-of-phps-variable-variables), [Create multiple variables based on an int count](http://stackoverflow.com/q/2231783), [Syntax help: variable as object name](http://stackoverflow.com/q/7940809), [Is it possible to reference a variable with a string and an int?](http://stackoverflow.com/q/6049175) – jscs Mar 08 '13 at 19:26

4 Answers4

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

Ultrakorne
  • 1,303
  • 11
  • 18
  • Can you please explain what is bullet1 and bubble2, and how runAction: calls that bulletActions[0] ?? – Anoop Vaidya Mar 08 '13 at 11:53
  • @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 – Ultrakorne Mar 08 '13 at 12:01
  • I dont have much idea about CCActions :( – Anoop Vaidya Mar 08 '13 at 12:05
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";
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • 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 Mar 08 '13 at 11:52
  • @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. – Anoop Vaidya Mar 08 '13 at 11:55
  • 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 Mar 08 '13 at 12:01
  • @Ultrakorne: I would say, this is a given feature of obj-c, we should use it. – Anoop Vaidya Mar 08 '13 at 12:04
  • 2
    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. – Ultrakorne Mar 08 '13 at 12:10
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.

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.

Fonix
  • 11,447
  • 3
  • 45
  • 74