Hidden features/tricks of Flash development, Flash

2019-01-21 02:42发布

Guys, I am thoroughly surprised that there is no Flash Hidden Features post yet in the Hidden Features series that I've been tracking for a while now.

There is a recent AS3/Flex one but it's not very active and I don't exactly mean just AS3 when I say Flash here.

The Hidden Features series is great for people who are new to a certain language. It shows the ropes and certain valuable tricks, all in one place. I think it's a brilliant idea. Even experts sometimes find tricks they'd never heard about.

When I started with Flash, I was taken aback by the Flash IDE and odd concepts of Flash, compared to other programming languages.

So, here goes: what are some hidden features of Flash as a language (AS2/3) and the Flash IDE?

Let the fun begin.

24条回答
走好不送
2楼-- · 2019-01-21 03:17

[AS3]

I wouldn't necessarily call this a 'feature', but you can actually access variables before they are defined (even with strict compilation) due to the way the compiler works:

trace(hb); // null
var hb : HBox = new HBox;
trace(hb); // [object]

It can actually lead to frustration when refactoring code (since it compiles).

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-21 03:18

when using flashvars write a utility method getFlashVars().

function getFlashVars():Object {
return Object(LoaderInfo(this.loaderInfo).parameters);

}

then when I'm coding I always add an extra || so that I won't have to flashvars while debugging.

localVar = getFlashVars().sampleVar || "default.xml";
查看更多
The star\"
4楼-- · 2019-01-21 03:20

Here's another gotcha:

By default Flash tools strips any metadata tags that you have. Many frameworks depends on these metadata tags. What you need to be careful is that this doesn't only apply to the compiler, but also to the optimizer. Be sure to run both your compiler and optimizer with -keep-as3-metadata-tag option to keep your metadata tag in there.

查看更多
戒情不戒烟
5楼-- · 2019-01-21 03:21

[AS3]

When looking for a the value of a variable, the player will run up the scope chain until it finds what it's looking for. So using nested (anonymous) functions you do things like make asynchronous calls and handle it using variables that were defined on the scope of the calling method. e.g.

function loadData(myAwesomeObject : AwesomeObject) : void
{

   var urlLoader : URLLoader = new URLLoader();
   urlLoader.addEventListener(Event.COMPLETE, function(event : Event) : void
   {
      myAwesomeObject.someReallyAwesomeMethod(event.target);
   });
   urlLoader.load(new URLRequest("http://someService"));

}

Loads of other uses for this and it's really very useful

查看更多
迷人小祖宗
6楼-- · 2019-01-21 03:22

[as3]

prototype based inheritance :

import flash.display.MovieClip;

var myRectangle = function(target,x,y){
    var internalTarget = target;
    var myMovieClip = new MovieClip();
    var myGraphic = myMovieClip.graphics;
    myGraphic.beginFill(0xAAAAAA);
    myGraphic.drawRect(0,0,100,100);
    myGraphic.endFill();
    trace(typeof internalTarget);
    Sprite(internalTarget).addChild(myMovieClip);
    myMovieClip.x = x ;
    myMovieClip.y = y ;
}
var aRectangle = new myRectangle(this,10,10);
查看更多
smile是对你的礼貌
7楼-- · 2019-01-21 03:23

[AS3] Tips for working with arrays or Vectors

Fastest way through an array, always from the back

var i:int = array.length;
var item:Object;
while(i--)
{
   item = array[i];
}

Clearing an array,

//faster than array = []
array.length = 0;

//garbage friendly
while(array.length)
{
    array.pop();
}

Pushing and splicing

//faster than array.push();
array[array.length] = "pushed value";

//faster than splice(index, 1)
var index:int = array.indexOf(splicee);
array[index] = null;
array.splice(array.length, 1);

Cloning

//fastest way to clone
var newArray:Array = array.concat();

//fastest manipulation
var mapFunction:Function = function(item:Object, index:int, source:Array):Object
{
    return //your cloning or morphing here
}
var newArray:Array = array.map(mapFunction);
查看更多
登录 后发表回答