ios - Cocos2d 2.0 SimpleAudioEngine stop sound effect in another object -
i trying stop sound effect using simpleaudioengine in cocos2d 2.0.
i have next situation;
farm scene 2 sky layers day/night. these placed sprites, created within layer in scene. have sprite classes other characters (using ray wenderlichs space viking code) placed.
upon touching sky have day alter night, , vice versa, , can start , stop night crickets sound effect using aluint id in farm layer (pollitoslayer.m) .
touching sun sprite (class) has it's own aluint reference can start. want stop when touching sky sprite.
is there way reference aluint of sun class when touching simple non-class sky sprite in same pollitoslayer.m?
my code excerpts below.
#import "sun.h" #import "pollitoslayer.h" @implementation sun @synthesize sunanim; @synthesize settinganim; -(void) dealloc { } -(void)playsunsound { sunsound = playsoundeffect(sun_spin); } -(void)changestate:(characterstates)newstate { [self stopallactions]; id action = nil; characterstate = newstate; switch (newstate) { case kstateglowing: cclog(@"sun->changing state glowing"); [self setdisplayframe: [[ccspriteframecache sharedspriteframecache] spriteframebyname:@"sun_1.png"]]; action = [ccspawn actions:[ccrepeatforever actionwithaction: [ccanimate actionwithanimation:sunanim]],nil]; //[self playsunsound]; break; case kstatesetting: cclog(@"sun->changing state setting"); [self setdisplayframe: [[ccspriteframecache sharedspriteframecache] spriteframebyname:@"sun_1.png"]]; action = [ccmoveby actionwithduration:3.0f position:cgpointmake(0.0f,-400.0f)]; [[simpleaudioengine sharedengine] stopeffect:sunsound]; break; default: cclog(@"sun -> unknown charstate %d", characterstate); break; } if (action != nil) [self runaction:action]; } #pragma mark - -(id) init { if( (self=[super init]) ) { //cgsize screensize = [[ccdirector shareddirector] winsize]; [self initanimations]; [[[ccdirector shareddirector] touchdispatcher] addtargeteddelegate:self priority:-1 swallowstouches:yes]; } homecoming self; } -(void) cleanup { // must manually remove class touch input receiver! [[ccdirector shareddirector].touchdispatcher removedelegate:self]; [super cleanup]; } #pragma mark - #pragma mark initanimations -(void)initanimations { [self setsunanim: [self loadplistforanimationwithname:@"sunanim" andclassname:nsstringfromclass([self class])]]; } -(void) update:(cctime)delta { } -(void) registerwithtouchdispatcher { [[[ccdirector shareddirector] touchdispatcher] addtargeteddelegate:self priority:-1 swallowstouches:yes]; } -(bool) cctouchbegan:(uitouch *)touch withevent:(uievent *)event { cgpoint touchlocation = [pollitoslayer locationfromtouch:touch]; // check if touch on sun's sprite. bool istouchhandled = cgrectcontainspoint([self boundingbox], touchlocation); if (istouchhandled) { [self changestate:kstateglowing]; [self runaction:[ccrotateby actionwithduration:1.0f angle:360]]; } homecoming istouchhandled; } -(void) cctouchended:(uitouch *)touch withevent:(uievent *)event { [self playsunsound]; } @end farm layer below pollitoslayer.m #import <foundation/foundation.h> #import "cocos2d.h" #import "constants.h" #import "gamemanager.h" #import "pollitoslayer.h" #import "pollito.h" #import "gameobject.h" #import "owl.h" #import "sun.h" #import "chicks.h" #import "mainscene.h" @implementation pollitoslayer @synthesize backbuttonsprite; @synthesize hennightsprite; @synthesize hendaysprite; @synthesize skyday; @synthesize skynight; @synthesize moon; aluint nightcrickets; aluint sunsoundlayer; aluint sunsound; aluint pollitosound; +(cgpoint) locationfromtouch:(uitouch*)touch { cgpoint touchlocation = [touch locationinview: [touch view]]; homecoming [[ccdirector shareddirector] converttogl:touchlocation]; } +(cgpoint) locationfromtouches:(nsset*)touches { homecoming [self locationfromtouch:[touches anyobject]]; } -(void)playnightsound { nightcrickets = playsoundeffect(night_crickets); } -(void)playsunsoundlayer { sunsoundlayer = playsoundeffect(sun_spin); } -(void)playpollitosound { pollitosound = playsoundeffect(pollitos_chirp); } -(id)init { self = [super init]; if (self != nil) { cgsize screensize = [ccdirector shareddirector].winsize; // enable touches self.istouchenabled = yes; srandom(time(null)); // seeds random number generator if (ui_user_interface_idiom() == uiuserinterfaceidiompad) { [[ccspriteframecache sharedspriteframecache] addspriteframeswithfile:@"sceneatlas1_default.plist"]; // 1 scenespritebatchnode = [ccspritebatchnode batchnodewithfile:@"sceneatlas1_default.png"]; // 2 } else { [[ccspriteframecache sharedspriteframecache] addspriteframeswithfile:@"sceneatlas1_default.plist"]; // 1 scenespritebatchnode = [ccspritebatchnode batchnodewithfile:@"sceneatlas1_default.png"];// 2 } [self addchild:scenespritebatchnode z:70 tag:100]; if (ui_user_interface_idiom() == uiuserinterfaceidiompad) { [[ccspriteframecache sharedspriteframecache] addspriteframeswithfile:@"sunsheet_default.plist"]; // 1 sunspritebatchnode = [ccspritebatchnode batchnodewithfile:@"sunsheet_default.png"]; // 2 } else { [[ccspriteframecache sharedspriteframecache] addspriteframeswithfile:@"sunsheet_default.plist"]; // 1 sunspritebatchnode = [ccspritebatchnode batchnodewithfile:@"sunsheet_default.png"];// 2 } [self addchild:sunspritebatchnode z:6 tag:101]; [self createobjectoftype:ksun //withhealth:100 atlocation:ccp(screensize.width * 0.18f, screensize.height * 0.79f) withzvalue:10]; ccsprite *backgroundimage; if (ui_user_interface_idiom() == uiuserinterfaceidiompad) { // indicates game running on ipad backgroundimage = [ccsprite spritewithfile:@"pollitosbackground.png"]; } else { backgroundimage = [ccsprite spritewithfile:@"pollitosbackground.png"]; } [backgroundimage setposition:ccp(screensize.width/2.0f, screensize.height/2.0f)]; [self addchild:backgroundimage z:20 tag:69]; } if (ui_user_interface_idiom() == uiuserinterfaceidiompad) { // indicates game running on ipad skyday = [ccsprite spritewithfile:@"skyday.png"]; } else { skyday = [ccsprite spritewithfile:@"skyday.png"]; } cgsize screensize = [ccdirector shareddirector].winsize; [skyday setposition:ccp(screensize.width * 0.5f, screensize.height* 0.75f)]; [self addchild:skyday z:0 tag:59]; //skyday.visible=yes; if (ui_user_interface_idiom() == uiuserinterfaceidiompad) { // indicates game running on ipad skynight = [ccsprite spritewithfile:@"skynight.png"]; } else { skynight = [ccsprite spritewithfile:@"skynight.png"]; } [skynight setposition:ccp(screensize.width * 0.5f, screensize.height * 0.75f)]; [self addchild:skynight z:2 tag:51]; skynight.visible = no; if (ui_user_interface_idiom() == uiuserinterfaceidiompad) { // indicates game running on ipad moon = [ccsprite spritewithfile:@"moon.png"]; } else { moon = [ccsprite spritewithfile:@"moon.png"]; } [moon setposition:ccp(screensize.width * 0.87f, screensize.height * 0.51f)]; [self addchild:moon z:5 tag:52]; backbuttonsprite =[ccsprite spritewithfile:@"back.png"]; [backbuttonsprite setposition:ccp(screensize.width * 0.88f, screensize.height * 0.95f)]; [self addchild:backbuttonsprite z:75 tag:75]; hendaysprite =[ccsprite spritewithfile:@"henday.png"]; [hendaysprite setposition:ccp(screensize.width * 0.70f, screensize.height * 0.37f)]; [self addchild:hendaysprite z:60 tag:60]; hennightsprite =[ccsprite spritewithfile:@"hennight.png"]; [hennightsprite setposition:ccp(screensize.width * 0.70f, screensize.height * 0.37f)]; [self addchild:hennightsprite z:61 tag:61]; hennightsprite.visible = no; [self createobjectoftype:kpollito atlocation:ccp(screensize.width * 0.378f, screensize.height * 0.13f) withzvalue:21]; [self createobjectoftype:kpollito2 atlocation:ccp(screensize.width * 0.578f, screensize.height * 0.18f) withzvalue:22]; [self createobjectoftype:kpollito3 atlocation:ccp(screensize.width * 0.450f, screensize.height * 0.20f) withzvalue:23]; [self createobjectoftype:kpollito4 atlocation:ccp(screensize.width * 0.3f, screensize.height * 0.43f) withzvalue:24]; [self createobjectoftype:kpollito5 atlocation:ccp(screensize.width * 0.10f, screensize.height * 0.27f) withzvalue:25]; [self createobjectoftype:kpollito6 atlocation:ccp(screensize.width * 0.25f, screensize.height * 0.19f) withzvalue:26]; [self createobjectoftype:kpollito7 atlocation:ccp(screensize.width * 0.77f, screensize.height * 0.12f) withzvalue:27]; [self createobjectoftype:kpollito8 atlocation:ccp(screensize.width * 0.17f, screensize.height * 0.42f) withzvalue:28]; [self createobjectoftype:kchicks atlocation:ccp(screensize.width * 0.73f, screensize.height * 0.25f) withzvalue:75]; [self createobjectoftype:kowl atlocation:ccp(screensize.width * 0.897f, screensize.height * 0.727f) withzvalue:29]; homecoming self; } #pragma mark - -(void)createobjectoftype:(gameobjecttype)objecttype atlocation:(cgpoint)spawnlocation withzvalue:(int)zvalue { if (objecttype == kpollito) { cclog(@"creating pollito1"); pollito *pollito = [[[pollito alloc] init] initwithspriteframename:@"pollito_1.png"]; [pollito setposition:spawnlocation]; [scenespritebatchnode addchild:pollito z:zvalue tag:1]; [pollito changestate:kstatepecking1]; } else if (objecttype == kpollito2) { cclog(@"creating pollito2"); pollito *pollito2 = [[[pollito alloc]init ]initwithspriteframename:@"pollito_1.png"]; [pollito2 setposition:spawnlocation]; [scenespritebatchnode addchild:pollito2 z:zvalue tag:2]; [pollito2 changestate:kstatepecking2]; }else if (objecttype == kpollito3) { cclog(@"creating pollito3"); pollito *pollito3 = [[[pollito alloc] init ]initwithspriteframename:@"pollito_1.png"]; [pollito3 setposition:spawnlocation]; [scenespritebatchnode addchild:pollito3 z:zvalue tag:3]; [pollito3 changestate:kstatepecking3]; }else if (objecttype == kpollito4) { cclog(@"creating pollito4"); pollito *pollito4 = [[[pollito alloc] init ]initwithspriteframename:@"pollito_1.png"]; [pollito4 setposition:spawnlocation]; [scenespritebatchnode addchild:pollito4 z:zvalue tag:4]; [pollito4 changestate:kstatepecking4]; [pollito4 setscale:0.8f]; }else if (objecttype == kpollito5) { cclog(@"creating pollito5"); pollito *pollito5 = [[[pollito alloc] init ]initwithspriteframename:@"pollito_1.png"]; [pollito5 setposition:spawnlocation]; [scenespritebatchnode addchild:pollito5 z:zvalue tag:5]; [pollito5 changestate:kstatepecking5]; }else if (objecttype == kpollito6) { cclog(@"creating pollito6"); pollito *pollito6 = [[[pollito alloc] init] initwithspriteframename:@"pollito_1.png"]; [pollito6 setposition:spawnlocation]; [scenespritebatchnode addchild:pollito6 z:zvalue tag:6]; [pollito6 changestate:kstatepecking6]; }else if (objecttype == kpollito7) { cclog(@"creating pollito7"); pollito *pollito7 = [[[pollito alloc] init ]initwithspriteframename:@"pollito_1.png"]; [pollito7 setposition:spawnlocation]; [scenespritebatchnode addchild:pollito7 z:zvalue tag:7]; [pollito7 changestate:kstatepecking7]; }else if (objecttype == kpollito8) { cclog(@"creating pollito8"); pollito *pollito8 = [[[pollito alloc] init] initwithspriteframename:@"pollito_1.png"]; [pollito8 setposition:spawnlocation]; [scenespritebatchnode addchild:pollito8 z:zvalue tag:8]; [pollito8 changestate:kstatepecking8]; [pollito8 setscale:0.8f]; }else if (objecttype == kowl) { cclog(@"creating owl"); owl *owl = [[[owl alloc] init ]initwithspriteframename:@"owl_1.png"]; [owl setposition:spawnlocation]; [scenespritebatchnode addchild:owl z:zvalue tag:9]; owl.visible = no; }else if (objecttype == kchicks) { cclog(@"creating chicks"); chicks *chicks = [[[chicks alloc] init] initwithspriteframename:@"pollitossleeping_1.png"]; [chicks setposition:spawnlocation]; [scenespritebatchnode addchild:chicks z:zvalue tag:11]; chicks.visible = no; }else if (objecttype == ksun) { cclog(@"here comes sun"); ccsprite *sun = [[[sun alloc] init ]initwithspriteframename:@"sun_1.png"]; [sun setposition:spawnlocation]; [sunspritebatchnode addchild:sun z:zvalue tag:12]; } } -(void) registerwithtouchdispatcher { [[[ccdirector shareddirector] touchdispatcher] addtargeteddelegate:self priority:0 swallowstouches:no]; } -(void) dealloc { cclog(@"%@:worked %@", nsstringfromselector(_cmd), self); } -(bool) cctouchbegan:(uitouch *)touch withevent:(uievent *)event { cgsize screensize = [ccdirector shareddirector].winsize; chicks *chicks = (chicks*)[scenespritebatchnode getchildbytag:11]; sun *sun = (sun*)[sunspritebatchnode getchildbytag:12]; owl *owl = (owl*)[scenespritebatchnode getchildbytag:9]; cgpoint touchlocation = [pollitoslayer locationfromtouch:touch]; // check if touch on pollito sprite. if (cgrectcontainspoint([backbuttonsprite boundingbox], touchlocation)) { stopsoundeffect(nightcrickets); stopsoundeffect(pollitosound); stopsoundeffect(sunsound); stopsoundeffect(sunsoundlayer); [[gamemanager sharedgamemanager] runscenewithid:kmainscene]; } else if ((cgrectcontainspoint([hendaysprite boundingbox], touchlocation))&&(hennightsprite.visible == no)) { hendaysprite.visible = no; hennightsprite.visible = yes; chicks.visible = yes; [chicks changestate:kstatechirping]; } else if ((cgrectcontainspoint([hennightsprite boundingbox], touchlocation))&&(hendaysprite.visible == no)) { hendaysprite.visible = yes; hennightsprite.visible = no; chicks.visible = no; } else if ((cgrectcontainspoint([skyday boundingbox], touchlocation)) && (skynight.visible == no)) { skynight.visible = yes; owl.visible = yes; ccaction *moveup = [ccmoveto actionwithduration:2.0f position:cgpointmake(screensize.width * 0.87f, screensize.height * 0.91f)]; [moon runaction:moveup]; [[simpleaudioengine sharedengine] stopeffect:sunsoundlayer]; ccaction *sundown = [ccmoveto actionwithduration:2.0f position:cgpointmake(screensize.width * 0.18f, screensize.height * 0.40f)]; [sun runaction:sundown]; stopsoundeffect(sunsound); [self playnightsound]; //[[simpleaudioengine sharedengine] stopeffect:sunsound]; stopsoundeffect(sunsoundlayer); }else if (((cgrectcontainspoint([skynight boundingbox], touchlocation)) && (skynight.visible == yes) )){ skynight.visible = no; ccaction *moveaction = [ccmoveto actionwithduration:2.0f position:cgpointmake(screensize.width * 0.87f, screensize.height * 0.57f)]; [moon runaction:moveaction]; owl.visible = no; ccaction *sunup = [ccmoveto actionwithduration:2.0f position:cgpointmake(screensize.width * 0.18f, screensize.height * 0.79f)]; [sun runaction:sunup]; stopsoundeffect(nightcrickets); } homecoming yes; } -(void) cctouchmoved:(uitouch *)touches withevent:(uievent *)event{ } -(void) cctouchcancelled:(uitouch *)touch withevent:(uievent *)event { } -(void) cctouchended:(uitouch *)touch withevent:(uievent *)event { } @end
to maintain simple, 1 thing can have farm layer own 2 skies (day , night) , have day sky own sun. way when determining touched, can see if current sky's heavily body (e.g. sun) touched. if stop necessary effects , play sun effect. if sky touched can stop heavily body's sound effect (which nil if 1 not playing) before changing day/night cycle , continuing play next set of sound effects. example:
@intereface farmlayer() @property (nonatomic, strong) sky* day; @property (nonatomic, strong) sky* night; @property (nonatomic, weak) sky* currentsky; @end @implementation farmlayer - (void)onenter { [super onenter]; self.day = [...]; self.night = [...]; self.currentsky = self.day; // couldn't think of improve name mean. self.day.skybody = [sun ...]; ... } - (void)cctouchesbegan:(nsset *)touches withevent:(uievent *)event { // pseudo-ish code... // if self.currentsky && self.currentsky.skybody touched // - stop necessary sound effects if playing. // - play sky body audio. // else if self.currentsky touched // - stop necessary sound effects (including self.currentsky.skybody's sound effect). // - if self.currentsky == self.day switch night (and vice versa). // - play necessary sound effects new sky. // - create self.currentsky equal the new sky. } @end i hope helped. illustration quick 1 assumes lot of stuff , not meant copied , pasted should give thought on how go solving dilemma. allow me know if misunderstood question.
edited:
also forgot mention, each object can have own array of sound files , can have convenience methods start , stop them. illustration assume had array property called audioeffects:
@interface sky() @property (nonatomic, strong) nsmutablearray* audiofilenames; @property (nonatomic, strong) nsmutablearray* audioids; @end @implementation sky - (void)addsoundeffectwithfilename:(nsstring *)filename { if (!filename) { // handle error. return; } if (!self.audiofilenames) { self.audiofilenames = [nsmutablearray array]; } [self.audiofilenames addobject:filename]; if (!self.audioids) { self.audioids = [nsmutablearray array]; } [self.audioids addobject:@0]; } - (void)playallaudioeffects { (int = 0; < [self.audiofilenames count]; i++) { nsstring* audiofile = self.audiofilenames[i]; aluint audioid = [[simpleaudioengine sharedengine] playeffect:audiofile]; self.audioids[i] = [nsnumber numberwithunsignedint:audioid]; } } - (void)stopallaudioeffects { (int = 0; < [self.audioids count]; i++) { aluint audioid = [self.audioids[i] unsignedintegervalue]; [[simpleaudioengine sharedengine] stopeffect:audioid]; } } @end ios objective-c cocos2d-iphone sprite simpleaudioengine
No comments:
Post a Comment