[Flashcoders] removeChild error
I'm just starting to fool around with as3 and having a little fun with computeSpectrum. I keep getting an error when I try to removeChild. I'm using the Flash CS3 trial and setting the document class to mySpectrum. Any help/explanation would be greatly appreciated. Thanks in advance Here's my script: package{ import flash.display.*; import flash.media.*; import flash.net.*; import flash.utils.*; import flash.events.*; import flash.display.Stage; public class mySpectrum extends Sprite{ private var _s:Sound; private var _sc:SoundChannel; private var _ba:ByteArray = new ByteArray(); public function mySpectrum(){ stage.frameRate = 31; _s = new Sound(); _s.load(new URLRequest(bad_plus.mp3)); _sc = _s.play(0,1000); addEventListener(Event.ENTER_FRAME, get_spectrum); } private function get_spectrum(event:Event):void{ SoundMixer.computeSpectrum(_ba,true,0); graphics.clear(); graphics.lineStyle(1,0xcc,100); var amp:Number = 0; for(var i:uint = 0; i512; i++){ var level:Number = _ba.readFloat(); amp += level; } graphics.drawCircle(stage.stageWidth/2,stage.stageHeight/2,amp); if(amp 50){ drawBurst(amp); } } private function drawBurst(burst:Number):void{ var burst_sprite:Sprite = new Sprite(); burst_sprite.graphics.lineStyle(1,0xcc,(burst*.01)); burst_sprite.graphics.beginFill(0xcc,(burst*.01)); burst_sprite.graphics.drawCircle(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight, burst); addChild(burst_sprite); burst_sprite.addEventListener(Event.ENTER_FRAME, fade_out); } private function fade_out(event:Event):void{ event.target.alpha -=.05; if(event.target.alpha .1){ removeChild(DisplayObject(event.target)); } } } } And here's the error: ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at mySpectrum/::fade_out() Myles Bieniasz MLB Advanced Media 212.485.6129 [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] MLB.com: Where Baseball is Always On ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] removeChild error
Maybe I'm wrong, but I don't think DisplayObject works like that. You could create a new DisplayObject, but I don't think DisplayObject(param) does anything. I think you're either creating a new DisplayObject or creating a null object, and either way, it's not going to be a child of your mySpectrum Sprite. removeChild(event.target) should be adequate, but if target needs a little help to know what to be you can do this: removeChild(event.target as DisplayObject) or removeChild(event.target as Sprite) Again, maybe I'm wrong, but that's my understanding Bieniasz, Myles wrote: I'm just starting to fool around with as3 and having a little fun with computeSpectrum. I keep getting an error when I try to removeChild. I'm using the Flash CS3 trial and setting the document class to mySpectrum. Any help/explanation would be greatly appreciated. Thanks in advance Here's my script: package{ import flash.display.*; import flash.media.*; import flash.net.*; import flash.utils.*; import flash.events.*; import flash.display.Stage; public class mySpectrum extends Sprite{ private var _s:Sound; private var _sc:SoundChannel; private var _ba:ByteArray = new ByteArray(); public function mySpectrum(){ stage.frameRate = 31; _s = new Sound(); _s.load(new URLRequest(bad_plus.mp3)); _sc = _s.play(0,1000); addEventListener(Event.ENTER_FRAME, get_spectrum); } private function get_spectrum(event:Event):void{ SoundMixer.computeSpectrum(_ba,true,0); graphics.clear(); graphics.lineStyle(1,0xcc,100); var amp:Number = 0; for(var i:uint = 0; i512; i++){ var level:Number = _ba.readFloat(); amp += level; } graphics.drawCircle(stage.stageWidth/2,stage.stageHeight/2,amp); if(amp 50){ drawBurst(amp); } } private function drawBurst(burst:Number):void{ var burst_sprite:Sprite = new Sprite(); burst_sprite.graphics.lineStyle(1,0xcc,(burst*.01)); burst_sprite.graphics.beginFill(0xcc,(burst*.01)); burst_sprite.graphics.drawCircle(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight, burst); addChild(burst_sprite); burst_sprite.addEventListener(Event.ENTER_FRAME, fade_out); } private function fade_out(event:Event):void{ event.target.alpha -=.05; if(event.target.alpha .1){ removeChild(DisplayObject(event.target)); } } } } And here's the error: ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at mySpectrum/::fade_out() Myles Bieniasz MLB Advanced Media 212.485.6129 [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] MLB.com: Where Baseball is Always On ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] removeChild error
Ugh that's too bad. It looks like in the Event class the target is typed as an object, so you can use it as an object, but not as an InteractiveObject or more specifically a DisplayObject. But the fact you can use it as an object means that you can grab its name (there's an example of this in the Flex docs, so I know I'm not making this up). Anyway, I can do event.target.name to get the name of the object. To get the associated sprite, I can do getChildByName(event.target.name) Also, when the Event class loses its usefulness in practical situations, you can always extend the event class and make a custom event of your own. This custom event listener can have additional parameters that you could specify to be a sprite, and you could refer to it as event.mysprite when receiving the event. There's plenty of online resources to explain this more fully, and you'll need to know it eventually, but in your situation right now it might not be necessary, and you can just get its reference by name, ben Bieniasz, Myles wrote: I originally tried removeChild(event.target) and got this error 1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject. I got the DisplayObject(param) from the help docs in Flash but like I said, that throws me an error too. I tried your suggestion of casting it as a sprite or displayobject and it gives me the same error as DisplayObject(param). I also tried created a baseContainer sprite and adding and removing everything from that but it gave me the same errors. Thanks for your help Ben, I'll keep looking and if you think of anything else please let me know. Thanks! -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of ben gomez farrell Sent: Thursday, April 26, 2007 11:34 AM To: flashcoders@chattyfig.figleaf.com Subject: Re: [Flashcoders] removeChild error Maybe I'm wrong, but I don't think DisplayObject works like that. You could create a new DisplayObject, but I don't think DisplayObject(param) does anything. I think you're either creating a new DisplayObject or creating a null object, and either way, it's not going to be a child of your mySpectrum Sprite. removeChild(event.target) should be adequate, but if target needs a little help to know what to be you can do this: removeChild(event.target as DisplayObject) or removeChild(event.target as Sprite) Again, maybe I'm wrong, but that's my understanding Bieniasz, Myles wrote: I'm just starting to fool around with as3 and having a little fun with computeSpectrum. I keep getting an error when I try to removeChild. I'm using the Flash CS3 trial and setting the document class to mySpectrum. Any help/explanation would be greatly appreciated. Thanks in advance Here's my script: package{ import flash.display.*; import flash.media.*; import flash.net.*; import flash.utils.*; import flash.events.*; import flash.display.Stage; public class mySpectrum extends Sprite{ private var _s:Sound; private var _sc:SoundChannel; private var _ba:ByteArray = new ByteArray(); public function mySpectrum(){ stage.frameRate = 31; _s = new Sound(); _s.load(new URLRequest(bad_plus.mp3)); _sc = _s.play(0,1000); addEventListener(Event.ENTER_FRAME, get_spectrum); } private function get_spectrum(event:Event):void{ SoundMixer.computeSpectrum(_ba,true,0); graphics.clear(); graphics.lineStyle(1,0xcc,100); var amp:Number = 0; for(var i:uint = 0; i512; i++){ var level:Number = _ba.readFloat(); amp += level; } graphics.drawCircle(stage.stageWidth/2,stage.stageHeight/2,amp); if(amp 50){ drawBurst(amp); } } private function drawBurst(burst:Number):void{ var burst_sprite:Sprite = new Sprite(); burst_sprite.graphics.lineStyle(1,0xcc,(burst*.01)); burst_sprite.graphics.beginFill(0xcc,(burst*.01)); burst_sprite.graphics.drawCircle(Math.random() * stage.stageWidth, Math.random
RE: [Flashcoders] removeChild error
Thanks Ben, good to know about getChildByName. I've gotta lot of digging through the docs to do. I think what ended up being the problem was not removing the eventListener on the Sprite. When I added this it worked fine. event.target.removeEventListener(Event.ENTER_FRAME, fade_out); this.removeChild(DisplayObject(event.target)); Thanks for your help and hopefully I'll be contributing a little bit once I get up to speed. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of ben gomez farrell Sent: Thursday, April 26, 2007 1:55 PM To: flashcoders@chattyfig.figleaf.com Subject: Re: [Flashcoders] removeChild error Ugh that's too bad. It looks like in the Event class the target is typed as an object, so you can use it as an object, but not as an InteractiveObject or more specifically a DisplayObject. But the fact you can use it as an object means that you can grab its name (there's an example of this in the Flex docs, so I know I'm not making this up). Anyway, I can do event.target.name to get the name of the object. To get the associated sprite, I can do getChildByName(event.target.name) Also, when the Event class loses its usefulness in practical situations, you can always extend the event class and make a custom event of your own. This custom event listener can have additional parameters that you could specify to be a sprite, and you could refer to it as event.mysprite when receiving the event. There's plenty of online resources to explain this more fully, and you'll need to know it eventually, but in your situation right now it might not be necessary, and you can just get its reference by name, ben Bieniasz, Myles wrote: I originally tried removeChild(event.target) and got this error 1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject. I got the DisplayObject(param) from the help docs in Flash but like I said, that throws me an error too. I tried your suggestion of casting it as a sprite or displayobject and it gives me the same error as DisplayObject(param). I also tried created a baseContainer sprite and adding and removing everything from that but it gave me the same errors. Thanks for your help Ben, I'll keep looking and if you think of anything else please let me know. Thanks! -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of ben gomez farrell Sent: Thursday, April 26, 2007 11:34 AM To: flashcoders@chattyfig.figleaf.com Subject: Re: [Flashcoders] removeChild error Maybe I'm wrong, but I don't think DisplayObject works like that. You could create a new DisplayObject, but I don't think DisplayObject(param) does anything. I think you're either creating a new DisplayObject or creating a null object, and either way, it's not going to be a child of your mySpectrum Sprite. removeChild(event.target) should be adequate, but if target needs a little help to know what to be you can do this: removeChild(event.target as DisplayObject) or removeChild(event.target as Sprite) Again, maybe I'm wrong, but that's my understanding Bieniasz, Myles wrote: I'm just starting to fool around with as3 and having a little fun with computeSpectrum. I keep getting an error when I try to removeChild. I'm using the Flash CS3 trial and setting the document class to mySpectrum. Any help/explanation would be greatly appreciated. Thanks in advance Here's my script: package{ import flash.display.*; import flash.media.*; import flash.net.*; import flash.utils.*; import flash.events.*; import flash.display.Stage; public class mySpectrum extends Sprite{ private var _s:Sound; private var _sc:SoundChannel; private var _ba:ByteArray = new ByteArray(); public function mySpectrum(){ stage.frameRate = 31; _s = new Sound(); _s.load(new URLRequest(bad_plus.mp3)); _sc = _s.play(0,1000); addEventListener(Event.ENTER_FRAME, get_spectrum); } private function get_spectrum(event:Event):void{ SoundMixer.computeSpectrum(_ba,true,0); graphics.clear(); graphics.lineStyle(1,0xcc,100); var amp:Number = 0; for(var i:uint = 0; i512; i++){ var level:Number = _ba.readFloat