TwelvestoneFlash

movieClipLoader in Class


Sign in

  • Waiting for Godot ( 730 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 5.1 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.9 k posts )
    general front end design an...
  • Back End ( 9.7 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
JERKSTORE
 
2009-01-19

Hey gang, I'm starting to dabble a bit more in OOP, and as such I've run into the following problem.

I have a class that handles a photo area of a site, that will cycle through photos as the user moves to different sections of the site. It works, and the images load, but what I'm not able to do are trace any values in my onLoadStart or onLoadInit functions of the loader.

import mx.transitions.Tween; import mx.transitions.easing.*;

class com.site.BGPhoto extends MovieClip {

private var photoCount:Number = 0;
private var mcBG:MovieClip;
private var mcDropShadow:MovieClip;
private var mcPhotoShell:MovieClip;
private var photoListener:Object = new Object();
private var photoLoader:MovieClipLoader;
private var loading:Boolean = false;

public function BGPhoto(){};

private function onLoad():Void{
    // create photo listener
    photoLoader = new MovieClipLoader();
    photoListener.onLoadStart = function(_mc:MovieClip){
        trace ("load start");
        loading = true;
        _mc.alpha = 0;
    };
    photoListener.onLoadInit = function(_mc:MovieClip){
        trace ("load init");
        loading = false;
        _mc._alpha = 0;
        var photoFadeIn = new Tween(_mc, "_alpha", Strong.easeOut, _mc._alpha, 25, 2, true);
        photoFadeIn.onMotionFinished = function(){
            if (photoCount > 0){
                this["pshell"+(photoCount-1)].removeMovieClip();
            }
            photoCount++;
        };
    };

    loadPhoto("home");
};

public function loadPhoto(section:String):Void{
    var photo = mcPhotoShell.createEmptyMovieClip("pshell"+photoCount, mcPhotoShell.nextHighestDepth());
    photoLoader.loadClip(_root.baseDir+"imgs/photos/"+_root.issue+"/"+section+".jpg", photo);
};

public function stageResize():Void{
    mcBG._width = Stage.width;
    mcDropShadow._width = Stage.width;
};

};

So the loader seems to be sort of working, because the image shows up. But I don't get either trace from inside the loader functions. Am I doing something wrong, or should I create the loader outside of the class, and just reference it from the Class?

JERKSTORE
 
2009-01-19

Also, I'm sure this is probably a laughable excuse for a "Class", but a guy's gotta start somewhere k

lithium
 
2009-01-20

You'll have to forgive me, as my AS2 is rusty, but from memory this is where the Delegate class comes in handy.

/* import the delegate */ import mx.utils.Delegate;

/* then when assigning your callbacks, use this instead */ photoListener.onLoadStart = Delegate.create ( this, onPhotoLoadStart ); photoListener.onLoadInit = Delegate.create ( this, onPhotoLoadInit );

/* then just replace your anonymous functions with class methods */

function onPhotoLoadStart (_mc:MovieClip) { trace ("load start"); loading = true; _mc.alpha = 0; };

function onPhotoLoadInit(mc:MovieClip) { trace ("load init"); loading = false; _mc._alpha = 0; var photoFadeIn = new Tween(_mc, "_alpha", Strong.easeOut, _mc._alpha, 25, 2, true); photoFadeIn.onMotionFinished = function() { if (photoCount > 0) { this["pshell"+(photoCount-1)].removeMovieClip(); } photoCount++; }; };

Using a delegate makes the callback function execute in the scope of the class (or whatever scope you pass as the first argument in Delegate.create()), rather than in the scope of an anonymous function, so you'll have access to all class level members.

You may need to use the same method with the onMotionFinished handler on the tween in order to get the same results, but it may not me necessary.

Hope it helps,

/lith

JLM
 
2009-01-20

Delegate is nicer but prob not essential. Without it I usually set up a local var that ref 'this' since there are scope issues, seem to rem something like..

var here = this; photoListener.onLoadStart = function(_mc:MovieClip){ trace ("load start"); this.loading = true; here._mc.alpha = 0; };

but Delegate looks cleaner.

There are two areas that I think you maybe missing. I would advise setting the listener object at run time, its a bit dodgy doing it when declaring the class vars, normally better to only do initialise really simple types like Number and String. Secondly and most importantly you are not setting up the listener.. there is no relationship in your code between photoListener and photoLoader so nothing will happen, it is possible to make the whole class the photoListener instead but for simplicity digging out some as2 code something like ...

//CORRECT APPROACH _introLoader = new MovieClipLoader(); _introListener = new Object(); _introListener.onLoadInit = Delegate.create( this, loadedAd ) _introLoader.addListener( _introListener ); _introLoader.load(...

and see Lithium's example.

But if it makes you feel any better I had to check through old code and took me awhile to get my head round, AS3 is actually much simpler.

As a hack I think you might be able to put the listeners directly on the loader but you may need to hide it from the compiler ( unless you hack some of the xml def files in your installation but that is dodgy for future coders using your code) have include this for completeness rather than good practice...

//HACK 1 _introLoader = new MovieClipLoader(); _introLoader[('onLoadInit')] = Delegate.create( this, loadedAd ) _introLoader.load(...

may not need the ()

also really really naughty but also kind of nice, no listener object and delegate bullshit just simple forget the java nazi's AS1 style hacking.

//HACK 2 _introLoader = new MovieClipLoader(); var here = this; _introLoader[('onLoadInit')] = function( _mc: MovieClip ){here.loadedAd(_mc)}; _introLoader.load(...

When doing as2 I found DelegateWithArgs useful might be worth a google.

lithium
 
2009-01-20

Originally posted by: JLM Delegate is nicer but prob not essential. Without it I usually set up a local var that ref 'this' since there are scope issues, seem to rem something like..

var here = this; photoListener.onLoadStart = function(_mc:MovieClip){ trace ("load start"); this.loading = true; here._mc.alpha = 0; };

the reason this is a problem in this instance is that "this" as in "this.loading = true" does not refer to the variable declared at the top of the class, it only exists in the scope of the anonymous event handler, hence it doesn't do anything to the main class.

JLM
 
2009-01-20

sorry did not read code properly, maybe something like..

// ( may not work with MTASC compiler ) var here = this; photoListener.onLoadStart = function(_mc:MovieClip){ trace ("load start"); here.loading = true; here._mc.alpha = 0; };

Quote: > But I don't get either trace from inside the loader functions.

Lithium the main prob is down to the addListener missing, Delegate is icing.

JERKSTORE
 
2009-01-20

Hahaha, I am totally missing the addListener! Doh! That's embarrassing k

Thanks for the great delegate info guys!

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFlash

movieClipLoader in Class