cocos2d iphone - Delegate only triggering one class, but not other classes that implement same protocol -
i need in understanding how implement delegate classes implement protocol gets change perform actions based on protocol method(s) gets triggered.
i have inputhandler class(ccnode subclass) takes care of input , passes states (e.g.: fire, stop, move_left, move_right, etc). inputhandler class has protocol has methods triggers based on ui controls player touches.
the problem right have 2 gameobjs implement protocol , perform custom actions, 1 gameobject triggering actions while other gameobj class not performing action.
if comment 2nd class' controller.delegate = secondgameobj; first gameobj performs actions suppose when delegate triggers protocol methods.
//inputhandler class @protocol inputhandlerdelegate <nsobject> @optional - (void)onpress; - (void)onmove: (controlleraction)state_; - (void)onrelease; @end @class inputhandler: ccnode<cctouchonebyonedelegate> @property (assign, nonatomic) id<inputhandlerdelegate> delegate; //.....more inputhandler class details... @end //implementation of game objects , assigning(registering) controller's delegate inputhandler *controller = [inputhandler node]; [self addchild: controller]; gameobject1 *gameobj1 = [gameobject1 node]; [self addchild: gameobj1]; controller.delegate = gameobj1; //does not trigger, unless comment out gameobj2 gameobject2 *gameobj2 = [gameobject2 node]; [self addchild: gameobj2]; controller.delegate = gameobj2; //this game obj triggers protocol custom methods
your delegate property naturally stores reference single object. when assign second delegate replaces first one. if have int variable x , assign value 10 followed 5 value 5, not 10 and 5.
to allow multiple delegates, add them array adddelegate , removedelegate methods. send delegate message enumerate array , send message each object. see example cocos2d's cctouchdispatcher.
or @ least need 2 separate delegate properties.
Comments
Post a Comment