PDA

View Full Version : Loading an image from the HD and displaying it in a MovieClip


xwielder
12-18-2007, 03:55 PM
graphicAnswerCONTAINER_mc.visible = true;
trace (graphicFilePath[theNum]); // C:/test/images/graphic.gif
var myRequest:URLRequest = new URLRequest( graphicFilePath[theNum] );
var loader:Loader = new Loader();
loader.load( myRequest );

graphicAnswerCONTAINER_mc.addChild(myRequest);


Trying to load a graphic into a MovieClip that's on my stage.

Receiving this error before the file publishes:
1067: Implicit coercion of a value of type flash.net:URLRequest to an unrelated type flash.display:DisplayObject.


If I do this:

graphicAnswerCONTAINER_mc.visible = true;
trace (graphicFilePath[theNum]); // C:/test/images/graphic.gif
var myRequest:URLRequest = new URLRequest( graphicFilePath[theNum] );
var loader:Loader = new Loader();
loader.load( myRequest );

graphicAnswerCONTAINER_mc.addChild(loader.content) ; // loader.content


It will publish, but when I come to this part of the code, it throws this error:
Error #2007: Parameter child must be non-null at flash.display::DisplayObjectContainer/addChild()


All suggestions and/or corrections are welcome. :)

xwielder
12-18-2007, 04:09 PM
graphicAnswerCONTAINER_mc.visible = true;
trace (graphicFilePath[theNum]); // C:/test/images/graphic.gif
var myRequest:URLRequest = new URLRequest( graphicFilePath[theNum] );
var loader:Loader = new Loader();
loader.load( myRequest );

graphicAnswerCONTAINER_mc.addChild(loader);


This works. <blushes> I just needed to use "loader" instead of "loader.content".

xwielder
12-18-2007, 04:51 PM
And just when I thought I had figured it out... <grrrr>


I'm now attempting to manipulate the graphic that was loaded.


graphicAnswerCONTAINER_mc.visible = true;

var myRequest:URLRequest = new URLRequest( graphicFilePath[theNum] );
var myLoader:Loader = new Loader();
myLoader.load( myRequest );
graphicAnswerCONTAINER_mc.addChild(myLoader);

graphicAnswerCONTAINER_mc.myLoader.x = 10; // nope
graphicAnswerCONTAINER_mc.myLoader.y = 20; // nope


Nope, isn't happening.

Now I realzie that "myLoader" is a Loader object and not a movieclip. I've tried a number of ways to make myLoader a movieclip but failed everytime. So now that I have my image on the stage (inside my mc container), how can I reference and manipulate that image?

dr_zeus
12-18-2007, 05:20 PM
When you add myLoader as a child to graphicAnswerCONTAINER_mc, it doesn't create a property named myLoader.

You still have a reference to myLoader, though, so it's no big deal.

myLoader.x = 10; // yep
myLoader.y = 20; // yep

If you need to access it somewhere else, you might want to give your Loader object a name.

myLoader.name = "myLoader";
graphicAnswerCONTAINER_mc.addChild(myLoader);

Then you can use getChildByName() to access it. Be sure to cast it as Loader.
var myLoader:Loader = Loader(graphicAnswerCONTAINER_mc.getChildByName("myLoader"));

xwielder
12-18-2007, 06:57 PM
Gread advice, dr_zeus. It works wonderfully.

Now, onto my third (and hopefully last) stumbling block.

Okay, I've got the image stuffed into my MC container, and I can set the x and y coordinates.

Now I want to record the size of that image that's showing. Why do I get the following?

trace ("myLoader.width = " + myLoader.width); // myLoader.width = 0
trace ("myLoader.height = " + myLoader.height); // myLoader.height = 0


The image being displayed certainly has a width and a height. Why am I getting 0?


Basically, I'm trying to center that small imported image within it's container (graphicAnswerCONTAINER_mc). Since AS3 has no way to set the registration point on runtime, here's what I'm doing:

graphicAnswerCONTAINER_mc.visible = true;

var myContainerWidth:Number = (graphicAnswerCONTAINER_mc.width / 2);
var myContainerHeight:Number = (graphicAnswerCONTAINER_mc.height / 2);

var myRequest:URLRequest = new URLRequest( graphicFilePath[theNum] );
var myLoader:Loader = new Loader();
myLoader.load( myRequest );
graphicAnswerCONTAINER_mc.addChild(myLoader);

myLoader.x = (myContainerWidth - (myLoader.width / 2));
myLoader.y = (myContainerHeight - (myLoader.height / 2));


Since myLoader.width and myLoader.height are strangly set at 0, myLoader isn't quite center of graphicAnswerCONTAINER_mc. The native registration point is set upper top left.

jaga
12-18-2007, 07:45 PM
dontcha have to wait til its finished loading to ask it for the width/height?

dr_zeus
12-18-2007, 09:14 PM
dontcha have to wait til its finished loading to ask it for the width/height?

Yes.

Listen for the complete event and get the width and height in the event handler function.

myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE, myCompleteHandler);

xwielder
12-19-2007, 11:23 AM
Today just isn't my day. :)


var myContainerWidth:Number = (graphicAnswerCONTAINER_mc.width / 2);
var myContainerHeight:Number = (graphicAnswerCONTAINER_mc.height / 2);

var myRequest:URLRequest = new URLRequest( graphicFilePath[theNum] );
var myLoader:Loader = new Loader();
myLoader.load( myRequest );
graphicAnswerCONTAINER_mc.addChild(myLoader);

myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE, myCompleteHandler(myLoader, myContainerWidth, myContainerHeight));

function myCompleteHandler(ldr:Loader, w:Number, h:Number)
{
ldr.x = (w - (ldr.width / 2));
ldr.y = (h - (ldr.height / 2));
}




Error #2007: Parameter listener must be non-null at flash.events::EventDispatcher/addEventListener()


It appears that it's firing the "myCompleteHandler" function before it completes.

xwielder
12-19-2007, 12:30 PM
I got it to work. Here's the code:


// Global vars
var myContainerWidth:Number = (graphicAnswerCONTAINER_mc.width / 2);
var myContainerHeight:Number = (graphicAnswerCONTAINER_mc.height / 2);
var myLoader:Loader = new Loader();

function fooBar()
{
var myRequest:URLRequest = new URLRequest( graphicFilePath[theNum] );
myLoader.load( myRequest );
graphicAnswerCONTAINER_mc.addChild(myLoader);
myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE, myCompleteHandler);
}

function myCompleteHandler(event:Event):void
{
myLoader.x = (myContainerWidth - (myLoader.width / 2));
myLoader.y = (myContainerHeight - (myLoader.height / 2));
}


Perfect. Code does exaclty what I need it to do.

THANK YOU FOR ALL THE HELP!