1

I need to load in an external swf and be able to use it as a Movieclip in FlashDevelop i.e I need to be able to go to specific keyframes, start and stop it playing etc. Some simple working sample code would be hugely appreciated as I cannot find any satisfactory tutorials through Google.

EDIT I now have this code

package 
{
import flash.net.*;
import flash.display.*;
import flash.events.*;
import flash.utils.getQualifiedClassName;

public class Main extends MovieClip 
{

    var animatedBox:MovieClip = new MovieClip();
    var ldr:Loader = new Loader();
    var frames:int = 0;

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onload);
        ldr.load(new URLRequest("../lib/test.swf"));
    }

    function onload(e:Event)
    {
        if ( !e.target )
        return;

        if( e.target.content is MovieClip )
        {
            animatedBox = e.target.content as MovieClip;

            animatedBox.gotoAndPlay("Start");
        }
        else
        {
            trace( getQualifiedClassName( e.target.content ) );
        }
    }

}

}

After I try to run it I get [Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference. Any ideas?

user1938856
  • 39
  • 2
  • 6

1 Answers1

2
import flash.utils.getQualifiedClassName;

var mc: MovieClip;

var ldr: Loader = new Loader();
ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoad );
ldr.load( new URLRequest("your.swf") );


function onLoad( e:Event ):void 
{
    if( !e.target )
        return;

    trace( getQualifiedClassName( e.target.content ) );
    /* if you get: flash.display::AVM1Movie
       it means you are trying to load an AS1 or AS2 SWF
       into AS3 SWF. They both need to be AS3 */

    mc = e.target.content as MovieClip;
    mc.gotoAndPlay( 2 );    
    // or  mc.gotoAndPlay( 'yourLabel' ); 

}
sanchez
  • 4,519
  • 3
  • 23
  • 53
  • I changed the code but got an error. Please take a look at the edited post – user1938856 Jun 02 '13 at 11:12
  • When I am typing animatedBox = event.target.content the content doesn't come up in the auto suggestion box like the .target and .anything else would. I'm thinking this is the problem as a failure here would cause the null animatedBox variable – user1938856 Jun 02 '13 at 11:22
  • Can you run your swf in debug mode, and tell me which line gives you that error? – sanchez Jun 02 '13 at 11:23
  • I get it at line 30 which is animatedBox.gotoAndPlay(1); – user1938856 Jun 02 '13 at 11:29
  • It traces "flash.display::LoaderInfo" – user1938856 Jun 02 '13 at 11:40
  • Are you trying to load AS1 or AS2 SWF into AS3 SWF ? They both need to be AS3 – sanchez Jun 02 '13 at 11:55
  • Ok, that must be it. Any idea what free programs I could make an as3 swf with? I tried Vectorian Giotto and SoThink swf Quicker and they both seem to create these AVM1Movie's rather than AVM2Movie's. I just want to create an animated object and then use it in my project – user1938856 Jun 02 '13 at 12:00
  • Ok done. Do you have any idea about the program I need though? – user1938856 Jun 02 '13 at 12:03