PDA

View Full Version : [AS3]Questions



Dazzer
December 12th, 2006, 04:07 AM
I'm creating a class that will load external clips, hold them in a dictionary array by name. These clips are basically sprites, but contain 2-4 different "rotations". This sprite loader will receive requests for sprites, and it will return BitmapData.

Now the questions



package dazzer.engine
{
import flash.utils.Dictionary
import flash.display.Loader
import flash.net.*

public class spriteLoader
{
private var myClips:Dictionary();
public function spriteLoader():void
{
myClips = new Dictionary();
}
function loadPath(name:String, path:String):void
{
//This part is truncated to save time
temp:Loader = new Loader()
temp.load(new URLRequest(path));
myClips[name] = temp.content
}
}
}


1) Does my class need to extend anything? Or can i just create it like that? Since it doesn't have to be in the displaylist.

2) I know the code above won't work. But its just the general idea.
What steps should i take for Garbage Collection? If , say, I wish to clear all the memory on the sprites, should i just put


myClips = new Dictionary();


what if i wanted to remove just 1 sprite from memory? I don't have a reference to the Loader anymore? Would


myClips["whatever"] = null

qualify that Loader it was referencing for collection?

Thank you anyone for their reply

Krilnon
December 12th, 2006, 07:34 AM
1. I might considered making this a subclass of EventDispatcher, since if it is supposed to be managing the loading process, you would hope that external classes wouldn't have to be listening for the events generated by each of your various Loader instances.

2. Why are you using a Dictionary? You aren't using weak referencing or non-String keys, and those two things are the only advantages that I see in using Dictionary over Object. Assuming 'temp' is eventually declared, and you don't come up with any other strange permanent references to your Sprites/Loaders during the listening process, then myClips should still be the only reference that isn't destroyed, which would mean that both of the examples that you posted should work.

Dazzer
December 12th, 2006, 07:43 AM
1. I might considered making this a subclass of EventDispatcher, since if it is supposed to be managing the loading process, you would hope that external classes wouldn't have to be listening for the events generated by each of your various Loader instances.

2. Why are you using a Dictionary? You aren't using weak referencing or non-String keys, and those two things are the only advantages that I see in using Dictionary over Object. Assuming 'temp' is eventually declared, and you don't come up with any other strange permanent references to your Sprites/Loaders during the listening process, then myClips should still be the only reference that isn't destroyed, which would mean that both of the examples that you posted should work.

ah i see... yes I shoudl be using arrays there for string-indexing. Funny how that slipped my mind.

with regards to using eventdispatcher... that has never crossed my mind. I'm not sure how that works... I've only ever extended sprites and movieclips.
But the purpose of this class is not really to loadsprites. That is a function. Its real purpose is the distribution of bitmapData to entities that request it.

When a clip finishes loading, i capture the event, then dispatch another event. Is that an elegant way of doing it?

thanks for the reply

Krilnon
December 12th, 2006, 04:29 PM
It seems elegant enough to me. If the purpose of the class is to distribute, then it should be the only class to be listening to the load events, since the other classes need to go through the distributor.

Dazzer
December 12th, 2006, 10:54 PM
Ok more more more questions

Now that i have this "distributor" how do i allow all the other components to get things from it?
I don't really want to have (parent.spriteLoader) all over the place. Is there a way to have a global function that will apply to all my components?

- frustration -