Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > Flex > Flex 2, 3 & 4

Reply
 
Thread Tools Rate Thread Display Modes
Old 09-26-2006, 01:09 AM   #1
juworld
Registered User
 
Join Date: Mar 2005
Posts: 44
Default Application.addChild

I am having a problem adding a child. I was reading the documentation and it says that addChild can be used by Application type.

So I did something like this:

Code:
	

	<mx:Script>
	<![CDATA[	
			import...		

			public function loadMySwf():void
			{
				var loader:Loader = new Loader();
				var urlReq:URLRequest = new URLRequest("example1.swf");
				loader.load(urlReq);
				addChild(loader);
				
			}		
	]]>
	</mx:Script>

	<mx:Button x="80" y="313" label="Button" click="loadMySwf()" />
But, its giving me an error. "Error #1034: Type Coercion failed: cannot convert flash.display..."

Please advice,

Thanks

Justin
juworld is offline   Reply With Quote
Old 09-26-2006, 07:29 AM   #2
flexy
Everything about me's...
 
Join Date: Aug 2006
Location: UK
Posts: 198
Default

I suspect you should be using this.rawChildren.addChild(child), as this.addChild is reserved for visual components typed UIComponent.

A topic here covered that with the Sprite object.
flexy is offline   Reply With Quote
Old 09-26-2006, 03:18 PM   #3
nirth
Member
 
Join Date: May 2006
Posts: 46
Send a message via ICQ to nirth Send a message via AIM to nirth Send a message via MSN to nirth Send a message via Yahoo to nirth Send a message via Skype™ to nirth
Default

Quote:
Originally Posted by AS3 Reference, mx.core.Container
addChild () method

public override function addChild(childisplayObject)isplayObject
Adds a child DisplayObject to this Container. The child is added after other existing children, so that the first child added has index 0, the next has index 1, an so on.

Note: While the child argument to the method is specified as of type DisplayObject, the argument must implement the IUIComponent interface to be added as a child of a container. All Flex components implement this interface.
And note, that only IUIComponent implentors use benefits of the auto layouting.
__________________
OrangeBlog finally in english, but really bad one
nirth is offline   Reply With Quote
Old 09-26-2006, 03:29 PM   #4
juworld
Registered User
 
Join Date: Mar 2005
Posts: 44
Default

thanks for the advice...

Someone also told me about this article: http://www.tcoz.com/flex/api/
juworld is offline   Reply With Quote
Old 09-26-2006, 03:41 PM   #5
Tink
Addict
 
Tink's Avatar
 
Join Date: Nov 2001
Location: London
Posts: 2,128
Default

bugger i posted here earlier but it doesn't seem to have shown up.

use SWFLoader instead of Loader
Tink is offline   Reply With Quote
Old 10-01-2006, 01:04 PM   #6
Mortimer Jazz
Thing
 
Mortimer Jazz's Avatar
 
Join Date: Jun 2001
Location: UK
Posts: 2,418
Default

I haven't used very much AS3 yet, but I thought I'd throw in this suggestion. In cases where you don't want to load a swf, but still want to add something to a Canvas which doesn't implement IUIComponent, could you not do it this way (it just seems like a bit less of a hack)
Using the http://www.tcoz.com/flex/api/ example of an image and a canvas....
ActionScript Code:
public function SomeConstructor(){             var loader:Loader = new Loader();             var canvas:Canvas = new Canvas();                         var uiComp:UIComponent = new UIComponent();             uiComp.addChild(loader);                         this.addChild(canvas);             canvas.addChild(uiComp);            }
I knocked a test class together by following the loader example from the dictionary. I couldn't get the image to show using either "rawChildren" or the above method (can someone enlighten me? I'm sure's it's something basic ) but all three methods did seem to output identical events at least:

initHandler: [Event type="init" bubbles=false cancelable=false eventPhase=2]
httpStatusHandler: [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=0]
completeHandler: [Event type="complete" bubbles=false cancelable=false eventPhase=2]


Here's the class I used, in full:
ActionScript Code:
package {     import flash.display.Loader;     import mx.containers.Canvas;     import flash.display.Sprite;     import mx.core.UIComponent;     import flash.events.*;         import flash.net.URLRequest;         public class Bob extends Sprite     {         private var url:String = "Image.gif";         public function Bob(){                         var canvas:Canvas = new Canvas();             var loader:Loader = new Loader();                                                 configureListeners(loader.contentLoaderInfo);                                     var request:URLRequest = new URLRequest(url);             loader.load(request);             var ui:UIComponent = new UIComponent();             ui.addChild(loader);                         this.addChild(canvas);             canvas.addChild(ui);                        }                  private function configureListeners(dispatcher:IEventDispatcher):void {             dispatcher.addEventListener(Event.COMPLETE, completeHandler);             dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);             dispatcher.addEventListener(Event.INIT, initHandler);             dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);             dispatcher.addEventListener(Event.OPEN, openHandler);             dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);             dispatcher.addEventListener(Event.UNLOAD, unLoadHandler);         }         private function completeHandler(event:Event):void {             trace("completeHandler: " + event);         }         private function httpStatusHandler(event:HTTPStatusEvent):void {             trace("httpStatusHandler: " + event);         }         private function initHandler(event:Event):void {             trace("initHandler: " + event);         }         private function ioErrorHandler(event:IOErrorEvent):void {             trace("ioErrorHandler: " + event);         }         private function openHandler(event:Event):void {             trace("openHandler: " + event);         }         private function progressHandler(event:ProgressEvent):void {             trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);         }         private function unLoadHandler(event:Event):void {             trace("unLoadHandler: " + event);         }     } }
__________________
............................
FLEX/FLASH BLOG
Previous Flash Tutorials

............................

Last edited by Mortimer Jazz; 10-01-2006 at 01:09 PM.
Mortimer Jazz is offline   Reply With Quote
Old 10-01-2006, 07:08 PM   #7
Tink
Addict
 
Tink's Avatar
 
Join Date: Nov 2001
Location: London
Posts: 2,128
Default

hey mortimer

are your creating a Flex project or an AS 3.0 project?

If is an AS 3.0 project they you don't have UIComponent or Canvas.

Bob extends Sprite, so there isn't a problem adding your Loader instance as a child to Bob, as a Sprite doesn't require its children to be UIComponents.

If your using Flex then you'd want to extend Canvas or something alike, then you'd use an Image object to load your bitmap.
Tink is offline   Reply With Quote
Old 10-02-2006, 12:34 AM   #8
Mortimer Jazz
Thing
 
Mortimer Jazz's Avatar
 
Join Date: Jun 2001
Location: UK
Posts: 2,418
Default

I used Canvas/Loader because that was the example given in the tcoz link.
I was really just trying to put forward the suggestion that if you are in a situation where you wanted to add something to a Flex container which doesn't implement IUIComponent (as in the tcoz.com example), wouldn't it be better to add it as a child of UIComponent rather than use 'rawChildren' (as suggested by tcoz). Sounds like both ways are wrong. I guess you shouldn't be using loader in the first place if you are using Flex?

So, can I just clarify that ... if I'm using Flex, it would be unlikely I would ever need to use loader?
__________________
............................
FLEX/FLASH BLOG
Previous Flash Tutorials

............................
Mortimer Jazz is offline   Reply With Quote
Old 10-02-2006, 08:02 AM   #9
Tink
Addict
 
Tink's Avatar
 
Join Date: Nov 2001
Location: London
Posts: 2,128
Default

if you are using Flex i can't see any need to be using Loader, you would use SWFLoader or Image.
Quote:
I was really just trying to put forward the suggestion that if you are in a situation where you wanted to add something to a Flex container
yeah the thing was in your example you extended Sprite which isn't a Flex container so it didn't really make sense.
Quote:
wouldn't it be better to add it as a child of UIComponent rather than use 'rawChildren'
there's nothing wrong with using rawChildren, but its only required here because the wrong tool for the job was used to start with.
Tink is offline   Reply With Quote
Old 10-02-2006, 08:23 AM   #10
Mortimer Jazz
Thing
 
Mortimer Jazz's Avatar
 
Join Date: Jun 2001
Location: UK
Posts: 2,418
Default

Heh, no, you're right it doesn't
...and I guess there's never really going to be a time when I'd want to put Flex components inside a Sprite either.

Damn, just did that out of habit - didn't even notice it.

Thanks Tink
__________________
............................
FLEX/FLASH BLOG
Previous Flash Tutorials

............................
Mortimer Jazz is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:00 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.