Question

We have created a program in Flash that uses an external actionscript file.

I'm trying to load/import this swf in another flash file but I'm getting a 'Error #1009: Cannot access a property or method of a null object reference'

I know this is to a reference in my external .as file but I'm not sure how to fix this

This is my code to load (and scale) the swf

var ld:Loader = new Loader();
addChild(ld);
ld.load(new URLRequest("tatton.swf"));
ld.contentLoaderInfo.addEventListener(Event.COMPLETE,function(evt) {
     var mc = evt.target.content;
     mc.scaleX=0.7031;
     mc.scaleY=0.7031;
});

Anyone have any ideas?

Thanks

Edit: here is the full error code: TypeError: Error #1009: Cannot access a property or method of a null object reference. at Tatton()

And below is my .as file code:

package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.setTimeout;
    import flash.utils.clearTimeout;
    import flash.utils.getDefinitionByName;

    public class Tatton extends MovieClip
    {
        public var active_clip:MovieClip;
        public var to:Number;

        public function Tatton()
        {
            // Create empty vector of movieclips;
            stage.addEventListener(MouseEvent.CLICK, reset_timeout);
            init();
        }

        public function reset_timeout(e:Event = null)
        {
            clearTimeout(to);
            // 3 mins
            to = setTimeout(fire_timeout, 150 * 1000);
        }

        public function fire_timeout():void
        {   
            // Reset the system (and run attractor) if we're not on the attractor already.
            if ( !(active_clip is Attractor))
            {
                init();
            }
        }

        public function init()
        {
            // Reset globals
            Globals.skip_menu_anim = false;
            Globals.skip_staff_menu = false;

            load_movie("Attractor");
            reset_timeout();
        }

        public function load_movie(name:String):void
        {
            if (active_clip is MovieClip)
            {
                kill_active_movie();
            }

            active_clip = createInstance(name);
            addChild(active_clip);
            active_clip.startup();
        }

        public function kill_active_movie():void
        {
            active_clip.shutdown();
            removeChild(active_clip);
            active_clip = null;

        }
        public function createInstance(className:String):Object
        {
            var myClass:Class = getDefinitionByName(className) as Class;
            var instance:Object = new myClass();
            return instance;
        }
    }

}
Was it helpful?

Solution

try this

public function Tatton() {

    addEventListener(Event.ADDED_TO_STAGE, stageAvailable);
}

private function stageAvailable(e:Event = null) {

    removeEventListener(Event.ADDED_TO_STAGE, stageAvailable);

    // Create empty vector of movieclips;
    stage.addEventListener(MouseEvent.CLICK, reset_timeout);
    init();
}

refer to this article to understand why

OTHER TIPS

You need to add the event listener after you call Loader.load because contentLoaderInfo is null until load is called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top