TwelvestoneFlash

loader as3 thoughts and approaches


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 ...
JLM
 
2008-05-28

loading in flash, changes with every iteration but it always seems to demand investigation and time and should/could always be simpler than it is.

The suggested as3 approach is the loader class, but for me using it for more than one item is messy and awkward.

I have seen bulkloader etc.. but these type of solutions seem overly frameworked/complex and only worth using in larger applications, where you can afford the time to learn the ins and outs and where extra overhead is insignificant and where huge flexibility is usefull.

My main issue is using the loader class directly in loading similar items; This is because I would like to use a single method/call back event as a loaded setup function for an array of images/swfs.

Getting a ref to the image/swf seems to be messy, so my approach is to wrap the as3 loader in a SimpleLoader which can be more easily be used in a class that deals with an array of images in a similar manor. Since I need to use the loaded in with papervision some complications have crept into my basic SimpleLoader class and it is far from fully conceptualised but I felt if I posted it others ideas my come forth.

The code consists of a wrapper and associated events class

package net.justinfront.customevents {

// see moock 226 for example

import flash.events.*;

public class SimpleLoaderEvent extends Event
{

    public static const COMPLETE:       String  = 'complete';
    public static const PROGRESS:       String  = 'progress';
    public static const VISUALLOADED:   String  = 'visualloaded'; 
    public static const PRESSED:        String  = 'pressed';


    public var ref:                     *;
    public var data:                    *;

    public function SimpleLoaderEvent(  type:       String, 
                                        bubbles:    Boolean,
                                        cancelable: Boolean,
                                        ref_:       *, 
                                        data_:      *
                            )
    {

        super( type, bubbles, cancelable );
        ref     = ref_;
        data    = data_;

    }


    public override function clone():Event
    {
        return new SimpleLoaderEvent(   type, 
                                        bubbles, 
                                        cancelable, 
                                        ref,
                                        data
                            );
    } 


    public override function toString():String
    {

        return formatToString(  'SimpleLoaderEvent', 
                                'type',
                                'bubbles',
                                'cancelable',
                                'eventPhase',
                                'ref',
                                'data'
                            )

    } 


}

}

package net.justinfront.utils {

import flash.display.*;
import flash.net.*;
import flash.events.*;
import net.justinfront.customevents.*;    
import flash.system.*;
import flash.geom.*;


public class SimpleLoader extends EventDispatcher
{


    private var _loader:            Loader;
    private var _loaderContext:     LoaderContext
    private var _location:          String;
    private var _url:               URLRequest;
    private var _ref:               *;
    private var _total:             Number;
    private var _container:         MovieClip;


    public function get totalBytes(): Number
    {

        return Math.round( _total );

    }

    public function SimpleLoader( ref_, location_: String)
    {

        _loader                 = new Loader();
        _ref                        = ref_;
        _location               = location_;
        _loaderContext          = new LoaderContext();

    }


    public function get context(): LoaderContext
    {
        return _loaderContext;
    }


    public function load():void
    {

        _loader.contentLoaderInfo.addEventListener( Event.INIT,                 completed               );
        _loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS,     downloadProgressFirst   );
        _loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR,      loadError               );
        _loader.load(new URLRequest( _location ), _loaderContext );            

    }


    private function completed( e: Event ):void
    {

        var mc: MovieClip       = new MovieClip();
        _container              = new MovieClip();

        _container.addChild( e.target.content );

        var w: Number           = _container.width;
        var h: Number           = _container.height;
        var rollover: Bitmap    = new Bitmap( new BitmapData( w, h, true, 0x00000000 ));
        rollover.smoothing      = true;

        _container.addChild( rollover );
        _container.addEventListener( MouseEvent.MOUSE_DOWN, pressed );
        mc.addChild( _container );

        //mc.x = -1000;
        //mc.y = -1000;

        dispatchEvent( new SimpleLoaderEvent( SimpleLoaderEvent.VISUALLOADED, 
                                        true,
                                        false,
                                        _ref,
                                        mc
                                    ));

    }


    private function downloadProgressFirst( e: ProgressEvent ):void
    {

        _total  = e.bytesTotal;
        _loader.contentLoaderInfo.removeEventListener(  ProgressEvent.PROGRESS,     downloadProgressFirst   );
        downloadProgress( e );
        _loader.contentLoaderInfo.addEventListener(     ProgressEvent.PROGRESS,     downloadProgress   );

    }


    private function downloadProgress( e: ProgressEvent ):void
    {

        dispatchEvent( new SimpleLoaderEvent( SimpleLoaderEvent.PROGRESS, 
                                        true,
                                        false,
                                        _ref,
                                        Math.floor( e.bytesLoaded*100/e.bytesTotal ) + '%'
                                    ));

    }


    // so can be overridden
    public function loadError( e: IOErrorEvent = null ):void{  dispatchEvent( e )  }

    private function pressed( e )
    {

        dispatchEvent( new SimpleLoaderEvent( SimpleLoaderEvent.PRESSED, 
                                        true,
                                        false,
                                        _ref,
                                        _container
                                    ));

    }

}

}

pyrogen
 
2008-05-28

consider adding LoaderContext to enable cross domain image loading...

var context:LoaderContext = new LoaderContext(); context.checkPolicyFile = true;

var request:URLRequest = new URLRequest( assetURL); loader.load(request, context);

edit.... never mind you have it there

JLM
 
2008-05-28

LoaderContext seems to differs for images and swf and I felt it was better to have a load stage and give it a setter, rather than add a LoaderContext to initialisation.

pyrogen
 
2008-05-28

def

i scanned you class in a lazy manner and posted recklessly k

Looks great man.

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

loader as3 thoughts and approaches