2

I'm just started doing 'hands-on' learning of Actionscript 3 using FlashDevelop.

I've now managed to subclass a sprite, load a graphic from the library, and manipulate it around the stage. Next step is doing the same with a MovieClip, but it doesn't seem to be as simple.

Basically, I want a simple subclass of MovieClip, that uses a SWF that I created & exported in Flash CS5 (anything else that can create them?), and then added it into my library in FlashDevelop.

In my 'Main' class, I want something like this:

package 
{
    import flash.display.MovieClip;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        private var myClip:MovieClip;

        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);

            myClip = new MyMovieClip();
            addChild(myMovieClip);          
        }
    }
}

So, for my subclass, I was looking at something, like this:

package  
{
    import flash.display.MovieClip;

    [Embed(source = '../lib/MyButton.swf', mimeType = 'application/octet-stream')]
    public class MyMovieClip extends MovieClip
    {
        public function MyMovieClip() 
        {
      // Empty
        }
    }
}

But that didn't work. Then I tried:

package  
{
    import flash.display.MovieClip;

    public class MyMovieClip extends MovieClip
    {
        [Embed(source = '../lib/MyButton.swf', mimeType = 'application/octet-stream')]
        private var MyMovieLivClass:Class;
        private var myClip:MovieClip;

        public function MyMovieClip() 
        {
            myClip = new MyMovieLivClass() as MovieClip;    
            addChild(myClip);           
        }
    }
}

But that says the child is null, and I'm not sure I should be creating a 'new' MovieClip inside my sub-classed MovieClip anyways.

What is the step I'm missing here?

Dave
  • 1,696
  • 4
  • 23
  • 47

1 Answers1

2

In your last sample, you used Embed correctly (you can't slap it on class declaration). If myClip is null, this means cast as MovieClip failed and MyButton.swf gives some other type. If MyButton.swf has only one frame, it will be Sprite and not MovieClip.

EDIT: I wish I'd notice this earlier. Your clip is embedded as ByteArrayAsset with mimeType of raw bytes. Remove mimeType from Embed and compiler will make it MovieClipAsset, which is MovieClip.

alxx
  • 9,897
  • 4
  • 26
  • 41
  • The clip is the stock 'Button' from one of the CS5 templates. 3 states (up/over/down) of 15 frames each to hold some animation too. – Dave Apr 06 '11 at 11:10
  • Actually, just realised that the animation was an object embedded into the first frame of the main stage... I wonder if that is in... Going to check. – Dave Apr 06 '11 at 11:12
  • Cast it into DisplayObject to see if that changes anything. Try to see type of `new MyMovieLivClass()` in debugger to check what it is. – alxx Apr 06 '11 at 11:17
  • Hrm, didn't help. Changed the SWF to a new 30 frame simple animation, but no change. Putting a breakpoint on the 'addChild' line shows that myClip is indeed NULL. Change the definition of 'myClip' to DisplayObject, and it is still NULL. I put a try/catch around the 'myClip = new MyMovieLivClass() as DisplayObject;' but nothing was caught there. – Dave Apr 06 '11 at 11:34
  • Your edit is correct. Not sure why FlashDevelop automatically adds that for SWF files, but at least I know now :) Many thanks. – Dave Apr 06 '11 at 12:25
  • FlashDevelop embeds it this way as you're embedding a SWF, not a MovieClip. Check out the Embed syntax (specifically the symbol attribute, which lets you embed a symbol from the swf, not the whole thing) http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf60546-7ffb.html – divillysausages Apr 06 '11 at 12:57
  • Alternatively, export from CS5 as a SWC instead of a SWF (right-click on a symbol in the library and select SWC instead of SWF). Then, you can add the library directly to FlashDevelop (Check out the "Adding the SWC to your FlashDevelop Project" section of http://active.tutsplus.com/tutorials/beginners-guide-to-flashdevelop-intro-basix/). Once you do that, you can just call `var mc:MovieClip = new MyMovieClip;` (Where `MyMovieClip` is a `MovieClip` exported for ActionScript in your SWC) – divillysausages Apr 06 '11 at 13:04