Hi
Is it possible to have a list of actions (say about 4 different actions) that can be run randomly after touching a sprite. The key would be for them to be random.
Thanks.
-alex
is it possible???
(7 posts) (5 voices)-
Posted 2 years ago #
-
I'm not sure if you can store actions in an Array, basically actions are objects so you could store them...
I'm guessing here, but if it's possible it would look something like this:-(id)init { ....................... actionArray = [[NSMutableArray alloc] init]; id action1 = [MoveTo actionWithDuration:0.1 ....]; id action2 = ....; .... [actionArray addObjects: action1, action2, action3, action4, nil]; .............. - (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { .......... [mySprite runAction:[actionArray objectAtIndex: arc4random()%actionArray.count]]; ........... }Posted 2 years ago # -
natanavra's idea works well.
If the number of actions is fixed you can use a standard array. I also found that I needed to retain the array. Something like this:
actionArray = [[NSArray arrayWithObjects: action1, action2, ..., nil] retain];Then release the array in your dealloc method.
Posted 2 years ago # -
could you elaborate on "realease the array in your dealloc metod - I am a newb and its all foreign ;)
Posted 2 years ago # -
Sure thing. I'm still getting the hang of the retain/release stuff but it's my understanding that the array is automatically released/deallocated/... unless you mark it as being retained. Once you do that, it's your responsibility to release it when you are finished. Your chance to do that is in your class' dealloc method. So, you'd want something like:
-(id)init { ............... id action1 = [MoveTo actionWithDuration:0.1 ....]; id action2 = ....; .... actionArray = [[NSArray arrayWithObjects: action1, action2, action3, action4, nil] retain]; .............. } -(void)dealloc { [actionArray release]; [super dealloc]; } - (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { .......... int randomIndex = arc4random()%actionArray.count [mySprite runAction:[actionArray objectAtIndex: randomIndex]]; ........... }Posted 2 years ago # -
Couldn't you also just call a method in ccTouchesBegan which randomly picks an action, then creates it and runs it. That way you don't have to retain all the actions throughout your program. Would performance be worse though? I'm thinking like this:
- (void)runRandomActionForSprite(Sprite*)sp { int r = arc4random() %4; if (r == 1) id myAction = [MoveTo actionWithDuration:0.1 ....]; else if (r == 2) id myAction = [.... ..... [sp runAction:myAction]; } - (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { .......... [self runRandomActionForSprite:mySprite]; ........... }Now, keep in mind, I'm fairly new at this, so I'm partially writing this as a question if people think this is an OK way to do it, because that's probably what I'd do.
Posted 2 years ago # -
It would be better to init those actions once, then reuse them to avoid havnt so many slow mallocs.
Posted 2 years ago #
Reply
You must log in to post.