View Full Version : Loading images within a class...
Grofit
October 16th, 2007, 09:25 AM
Hey!
Ive been taking a break from flash but im back on to it at the moment and a tad annoyed at a problem ive got.
I made a simple class which derived from movieclip, lets say called simpleSprite. I then made a method (Function) within the class called loadImageResource(strResource:String).
public function loadImageResource(strResource:String)
{
var ImageLoader = new Loader();
ImageLoader.load(new URLRequest(strResource));
ImageLoader.addEventListener(Event.COMPLETE, this.loadResource);
ImageLoader.addEventListener(IOErrorEvent.IO_ERROR , this.onIOError);
ImageLoader.addEventListener(SecurityErrorEvent.SE CURITY_ERROR, this.onSecurityError);
ImageLoader.addEventListener(HTTPStatusEvent.HTTP_ STATUS, this.onHTTPError);
//this.addChild(ImageLoader);
}
public function loadResource(e:Event){...}
public function onIOError(e:Event){...}
public function onSecurityError(e:Event){...}
public function onHTTPError(e:Event){...}
Now imagine that the bottom functions have lots of fancy stuff in them, as ive just put ... in there to signify there is some error assertion or something.
Anyway the above theoretically should go into the function get the file, then given the asynchronous nature will get back to me in a second calling loadResource() telling me that its complete. However what acctually happens is that it runs through the functions but none of the onXXXXXX or loadResource functions are called, its like the event goes to limbo land.
Originally the functions were private which i changed thinking it may need them public for callback purposes, however that didnt work, i then tried attaching the eventlistener to the class (this.XXXX) rather than the imageLoader...
I can get this to work if i put it outside of the class in some "global" area, however surely there must be a way to let classes load their own content and keep it encapsulated, i.e a player class that has its custom player details and its sprite information.
Has anyone done image loading within a class before as i cant get it to run for the life of me...
webdevotion
October 16th, 2007, 09:30 AM
Funny, just placed the exact same code in a post minutes before this one :)
Use the contentLoaderInfo property to add the listeners to.
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE,onComplete, false, 0, true );
Grofit
October 16th, 2007, 09:37 AM
cheers mate will go give it a go!
Grofit
October 16th, 2007, 10:00 AM
cheers mate will go give it a go!
It seems to load but i think im maybe being a bit of a dunce when im trying to attach it to the scene...
how do i go about getting the loaded simpleSprite displayed on the screen...
var oSprite:simpleSprite = new simpleSprite();
oSprite.loadImageResource("random_image.gif");
//stage.addChild(oSprite);
I tried root/Scene/stage and none of them work, they either give compiler errors or just dont display...
Im also presuming that as the loading is done internally to the class even if i add the child before its loaded it will update the scene with its actual display data when it is loaded, or is this not the case?
webdevotion
October 16th, 2007, 10:18 AM
well, this code should work ( from my head, not tested ):
Also, use lowercase for instance and uppercase for classnames.
var imageLoader : Loader = new Loader();
addChild( imageLoader ); // this line adds your loader instance to the stage
Grofit
October 16th, 2007, 10:28 AM
well, this code should work ( from my head, not tested ):
Also, use lowercase for instance and uppercase for classnames.
var imageLoader : Loader = new Loader();
addChild( imageLoader ); // this line adds your loader instance to the stage
as my simpleSprite class is derived (extended) from MovieClip i add the child inside the simpleSprite function, then i was expecting to add the simpleSprite instance to the actual scene/stage. I could be going about this all wrong as im expecting to be able to attach the object holding the image to the scene, would i have to get the internal image data from that object and assign that to the scene?
So to try and simplify it more, the flow of code i would imagine logically:
1) Create instance of simpleSprite
2) Load image in simpleSprite via loadImageResource
3) Add the loader as a child to the simpleSprite instance
4) Add the simpleSprite instance as a child to the scene
Well thats the way i imagine it all running, but in all honesty im fairly inexperienced with actionscript and how it links to the scene/stage...
webdevotion
October 16th, 2007, 10:36 AM
Why do you extend movieclip ?
Just extend Sprite if you need a SimpleSprite class ;)
MovieClip takes more of the memory footprint in the player.
A Sprite is basically the same, minus the timeline of a MovieClip.
Did you add your SimpleSprite instance to the stage ?
What is the output of trace( this ); in the SimpleSprite constructor?
var s : SimpleSprite = new SimpleSprite ();
addChild( s );
Grofit
October 16th, 2007, 10:46 AM
Why do you extend movieclip ?
Just extend Sprite if you need a SimpleSprite class ;)
MovieClip takes more of the memory footprint in the player.
A Sprite is basically the same, minus the timeline of a MovieClip.
Did you add your SimpleSprite instance to the stage ?
What is the output of trace( this ); in the SimpleSprite constructor?
var s : SimpleSprite = new SimpleSprite ();
addChild( s );
To be honest i just made up the simpleSprite class name, im ideally going to make a movable character on screen who can move around, so i thought i would use a movieclip as that way i can add certain frames for certain animations to make it look like its walking rather than a static image that moves. Not sure if you can achieve that effect using Sprite or Bitmap classes.
Back to the subject at hand yes, i did add the tile as a child expecting it to work, however it didnt :(, do i have to setup anything special in the constructor when deriving from MovieClip as ive just got a blank constructor at the moment?
Thinking about it, it may be more logical to just have the class contain an instance of MovieClip and then expose that via the class and add that as a child to the scene. Although i dont entirely get what control you have over the object once you add it to the scene, if i made a player then added its movieclip to the scene then moved the players x/y by 5 pixels would it update the scene or would i need to add it again?
Thanks for your help by the way!
webdevotion
October 16th, 2007, 10:48 AM
You can place super(); in your constructor to call the constructor of the superclass.
Btw: is this a Flash CS3 project or an Actionscript project in Flex Builder?
Grofit
October 16th, 2007, 11:02 AM
You can place super(); in your constructor to call the constructor of the superclass.
Btw: is this a Flash CS3 project or an Actionscript project in Flex Builder?
im using Flash CS3 + Actionscript version 3, ah so its like Java!
webdevotion
October 16th, 2007, 11:10 AM
Yes, it's Java-like.
Did you get an output from trace(this) I mentioned earlier?
Grofit
October 16th, 2007, 11:15 AM
Yes, it's Java-like.
Did you get an output from trace(this) I mentioned earlier?
Yeah it just came up saying Object, Anyway ive got it working and answered some of my own questions...
If i expose a movie clip internally from the class, and add it as a child it displays.
I *AM* able to change the actual raw movie clip and it updates the child, so it must just pass a pointer over or something.
Thanks for your help chief im sure i will have more questions in the future!
webdevotion
October 16th, 2007, 11:32 AM
Aha, well, it would be nice if you could post your solution for future reference for the other users :book:
See you next time :)
Grofit
October 17th, 2007, 03:44 AM
Aha, well, it would be nice if you could post your solution for future reference for the other users :book:
See you next time :)
package whatever
{
import flash.display.Loader;
import flash.net.URLRequest;
import flash.display.MovieClip;
import flash.events.*;
public class simpleImageLoader
{
private var movImage:MovieClip;
public function tile()
{
movImage = new MovieClip();
}
public function getImage():MovieClip {return movImage;}
public function loadImageResource(strResource:String)
{
var ImageLoader = new Loader();
ImageLoader.load(new URLRequest(strResource));
ImageLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, this.loadResource);
ImageLoader.contentLoaderInfo.addEventListener(IOE rrorEvent.IO_ERROR, this.onIOError);
ImageLoader.contentLoaderInfo.addEventListener(Sec urityErrorEvent.SECURITY_ERROR, this.onSecurityError);
ImageLoader.contentLoaderInfo.addEventListener(HTT PStatusEvent.HTTP_STATUS, this.onHTTPError);
movImage.addChild(ImageLoader);
}
public function loadResource(e:Event)
{
trace("Loaded");
}
public function onIOError(e:IOErrorEvent)
{
trace("Your IO ERROR");
}
public function onSecurityError(e:SecurityErrorEvent)
{
trace("Your Security ERROR");
}
public function onHTTPError(e:HTTPStatusEvent)
{
trace("Your HTTP STATUS");
}
}
}
var myImage:simpleImageLoader = new simpleImageLoader();
addChild(myImage.getImage());
myImage.loadImageResource("whatever.jpg");
myImage.getImage().x = 200;
myImage.getImage().y = 200;
That pretty much sums up how im doing it, may be better ways though.
Grofit
October 17th, 2007, 10:02 AM
Sorry to ressurect this topic, but how come the contentloaderinfo works with assigned listeners but the standard object does.
Also how would you go about sending a bog standard event that triggers a method within a function...?
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.