ios - How to run the same block of code - Cocos2d -
i using cocos2d , have 2 ai sprites called theevilone , theeviltwo. these 2 sprites call same bloc of code same class sending sprite parameter. game gets buggy run code 1 of sprites preforms actions , code stops working. question is possible call same bloc of code multiple sprites simultaneously or there wrong code. below example of running on program.
-(void)loadingevilactions:(id)sender { //this getting called if(loaded == no) { theevilone = [ccsprite spritewithfile:@"evil_alt_idle_00.png"]; //these sprites have been declared in .h file [self addchild:theevilone z:200]; theeviltwo = [ccsprite spritewithfile:@"evil_alt_idle_00.png"]; [self addchild:theeviltwo z:200]; loaded = yes; } [self checkcollision:theevilone]; [self setcenterofscreen:theevilone]; [self stayonscreen:theevilone]; [self aicharacter:theevilone]; [self checkcollision:theeviltwo]; [self setcenterofscreen:theeviltwo]; [self stayonscreen:theeviltwo]; [self aicharacter:theeviltwo]; } -(void)aicharacter:(id)sender { ccsprite *evilcharacter = (ccsprite *)sender; if (aiactionrunning == no){ .... more stuff // e.g. id jump_up = [ccjumpby actionwithduration:0.6f position:ccp(randomjump, evilcharacter.contentsize.height) height:25 jumps:1]; id jump_down = [ccjumpby actionwithduration:0.42f position:ccp(randomjump,-evilcharacter.contentsize.height) height:25 jumps:1]; id seq = [ccsequence actions:jump_up,jump_down, [cccallfuncn actionwithtarget:self selector:@selector(stopaiaction:)], nil]; [evilcharacter stopallactions]; [evilcharacter runaction:seq]; aiactionrunning = yes; } } -(void)stopaiaction:(id)sender { aiactionrunning = no; }
edit
i'm running random number generators within ai character method , many actions. game works 1 enemy sprite , decided try , implement way create many.
edit 2
i added part of method stops sprites being constantly, trying simplify code irrelevant question , forgot add part. changed way calling methods suggested learncocos2d not solving problem making simpler. not using arc
one of sprites 1 preforming majority of actions in instances other sprite may preform actions aswell, preformed 1 sprite. think main question can call same method using different sprites passing sprite parameter.
edit 3
i have figured out problem there boolean value flag enclosing aicharacter method, when 1 sprite runs through method stops other sprite running method. there way can implement array of records or such each sprite have own boolean flags.
with making 'array' possible change boolean theevilone , theeviltwo using temp sprite evilcharacter without doing both separately.
if understand question correct trying take 1 action , run on more 1 character @ time, like:
id move = [ccmoveto actionwithduration:0.5f position:ccp(0,0)]; [theevilone runaction:move]; [theeviltwo runaction:move];
that wouldn't work because evil 1 not have performed on since moved running on evil 2. when action running running on 1 target. instead would:
id move = [ccmoveto actionwithduration:0.5f position:ccp(0,0)]; [theevilone runaction:move]; [theeviltwo runaction:[move copy]];
assuming using arc. if not you'd want release copy of course. when comes particular example, why trying actions call methods. why not call method? seems pointless learncocos2d has pointed out.
another potential problem have creating more , more sprites each time method called. on purpose or there suppose 1 evil 1 , 1 evil 2? if shouldn't creating more sprites.
edit:
remove bool. instead:
ccsprite *evilcharacter = (ccsprite *)sender; if ([evilcharacter numberofrunningactions] == 0){ ... }
Comments
Post a Comment