Notes/ActionScript

edit
revisions

what's new
search
help

kiwi


ActionScript Notes

GOTCHA: for and for..in

It seems like sometimes for do not work properly, while for..in will work. A recente example was iterating on the attributes of an XML node :

  for ( var i=0 ; i<node.attributes.length ; i++ )
  { trace("a: " + node.attributes[i]); }

did not produce any result, while

  for ( var a in node.attributes ) { trace("a: " + a); }

did work properly. Moreover, tracing node.attributes gave me undefined. I wish there was a reason for that...

Get flash movie width and height

The Stage represents the root of all movie clips. To access the flash applet width and height, simply do the following :

  var w = Stage.width
  var h = Stage.height

Handling events

Movie clips can be attached event handlers, such as for instance onRollOver/onRollOut and onMouseDown/onMouseUp. However, both have a different behaviour :

  • onRollOver/onRollOut are only triggered when the mouse is really over the movie clip
  • while the others are triggered anyway, so you have to test if the mouse cursor is in your movie clip hit area

Make a MovieClip? subclass

If your are like me, and only develop ActionScript using MTASC (without the Flash IDE), you may have wondered how to create movie clips (so you can draw or do whatever you want on them).

The first solution that one may think of is to try to sublass a MovieClip? and instanciate it. However, the only way to instanciate a MovieClip? is to use the attachMovieClip method (of MovieClip?s such as _root) :

  _root.attachMovieClip( "MyMovieClip?", "movie_001", 1);

The trick is that if you create a MyMovieClip? MovieClip?subclass, this won't work, because the Flash player won't find the class named MyMovieClip?. So what you have to do is ensure that your subclass gets registered into the Flash player namespace. To do so, you simply have to use the following trick, by Peter Joel:

 class MyMovieClip? extends MovieClip?
 {
    public static var symbolName = "__Packages.MyMovieClip?";
    public static var symbolOwner = MyMovieClip?;
    public static var symbolLinked = Object.registerClass(symbolName, symbolOwner); 

    public function MyMovieClip? ()
    {
        ...
    }
 }

Be sure that you do not strip the __Packages. prefix before your class name, because otherwise it won't work. Now, you should be able to instanciate your class using the following function call :

  _root.attachMovieClip( MyMovieClip?.symbolName, "movie_001", 1);

Also, note that the following line

   _root.attachMovieClip( "MyMovieClip?", "movie_001", 1);

will not work, you have to fully prefix your class name. I don't really get the reason for that, but well, that's ActionScript ;)

Drawing text in your movie

Center your text in text fields

Use and include custom fonts in your SWF

last modified on November 26, 2005, at 07:14 PM

© type-z.org and its contributing authors, 2001-2004.
Content is licensed under a Creative Commons License.