Archive

Posts Tagged ‘movieclips’

Flex: working with MovieClips

June 16th, 2008 6 comments

You do not do everything with Flex components, especially if you develop games or cool interfaces. You often need to load SWF files and use the MovieClips inside them. A previous article talks about Instantiating and displaying MovieClips from loaded SWF.

Here are a few considerations when loading MovieClips into Flex:

  • If you embed a symbol, you will lose all timeline actions. Embed the whole SWF instead.
  •  

  • If you have a stop on the first frame of a multi-frame MovieClip, it might never execute. Add the action on the second frame.
  •  

  • When you say gotoAndStop (or gotoAndPlay), you cannot immediately reference the children on that frame. You have 3 choices:
        

    • Use stage.invalidate() and listen to RENDER events
    •  

    • Listen to ADDED events and check if the target of the event is the object that you need.
    •  

    • Forget about frames and do like Flex intended: one symbol per MovieClip state (button states for example)
    •  

  •  

  • If your MovieClip is 1-frame long, it will still loop indefinitely, consuming precious resources. Use stop() on the first frame.
  •  

  • When you use the addChild method with the MovieClip, the clip will not be copied. It will be removed from its previous parent (firing the REMOVED_FROM_STAGE event) and then added to the new parent.
  •  

  • You cannot duplicate a MovieClip. If you want to be able to create multiple instances, link these MovieClips in you SWFlibrary and instantiate as many as you want.
  •  

  • A MovieClip cannot be added directly to Flex containers. Add it as a child of a SWFLoader or an Image and add that control.
  •  

  • The Loader class is a DisplayObject but has trouble resizing itself. You can always grab the loader.content as a MovieClip and add it where you need it.
  •  

Tags: ,

Instantiating and displaying MovieClips from loaded SWF

March 6th, 2008 No comments

So you want to dynamically load a .swf, instantiate it and add to to a SWFLoader control.

First, loading the .swf:

var loader:Loader = new Loader();
loader.load(url);

Now, to get its classes, we need to listen for the loading completion:

loader.addEventListener(Event.INIT, callbackFunction);

Once done, the callbackFunction will get and instantiate a class:

var mcClass:Class =
loader.content.loaderInfo.applicationDomain.getDefinition("instanceName") as Class;
var mc:MovieClip = MovieClip(new mcClass());

Hurray! Now we can add it to some appropriate container:

someSwfLoader.addChild(mc);

Now we want to scale it to fit the available space:

mc.scaleX = mc.scaleY = someSwfLoader.width / mc.width;

(a more complex algorithm may be used)

A problem may arise if your clip content is not at 0,0 coordinates:

var bounds:Rectangle = mc.getBounds(Application.application.stage);
mc.x -= bounds.left * mc.scaleX;
mc.y -= bounds.top * mc.scaleX;

(if you have an ActionScript project instead of Flex, you’ll need to access the stage by other means)

Tags: ,