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:37

[AS3]

The || (logical or) operator can be used for any truthy/falsey values.

var obj : Object = nullObject || instance; // instance
var val : int = 5 || 10; // 5
var val : int = 0 || 10; // 10

Also, you can use & and | (bitwise and/or) to do a non-short-circuted expression:

function functionA() { return false; }
function functionB() { return true; }

if (functionA() && functionB()) { trace("something"); } // methodB won't be executed
if (functionA() & functionB()) { trace("something"); } // methodB WILL be executed
查看更多
聊天终结者
3楼-- · 2019-01-21 03:37

[AS3]

var list : Vector.<Object> = new Vector.<Object>();

becomes slightly shorter by writing

var list : Vector.<Object> = new <Object>[];

You can even predefine values:

var list : Vector.<Object> = new <Object>["one", "two", "three"];
查看更多
男人必须洒脱
4楼-- · 2019-01-21 03:37

Here's something most people don't know: MouseEvents are tracked outside of the application window if the MOUSE_DOWN event has been fired, but not MOUSE_UP. You can grab mouse positions outside of the application window (and even outside of the browser window) as long as whatever you're doing makes the user hold their mouse down. To test this, try running the following code:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx"
           creationComplete="init()">
<fx:Script>
    <![CDATA[
        protected function init():void {
            addEventListener(Event.ADDED_TO_STAGE, magic)
        }

        protected function magic(e:Event):void {
            stage.addEventListener(MouseEvent.MOUSE_MOVE, moreMagic);
        }

        protected function moreMagic(e:MouseEvent):void {
            magicalButton.label = "Hold me down! " + String(e.stageX) + "x" + String(e.stageY);
        }
    ]]>
</fx:Script>    
<s:Button id="magicalButton" label="Hold me down!"/>

查看更多
疯言疯语
5楼-- · 2019-01-21 03:38

This package will get you all the DisplayObjects in DisplayObjectContainer by setting class types.

Note: It wont count frames in MovieClips.

package {

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.utils.getQualifiedClassName;

/**
 * 
 * @author Cansın Şenalioğly @ cansin.senalioglu@gmail.com
 *   
 */ 

//--------------------------------------
//  Class description
//--------------------------------------
/**
 * <p>Gets all DisplayObject types in DisplayObjectContainer.</p>
 *
 * @langversion 3.0
 * @playerversion Flash 9.0.28.0
 * @includeExample examples/ButtonExample.as
 *  
 *  @playerversion AIR 1.0
 */

public class DisplayObjectFinder
{

    /**
     * 
     * 
     * 
     * <p>Gets all DisplayObject type in DisplayObjectContainer</p>
     * <b>Example:</b><br> 
     * <p>var items:Array = DisplayObjectFinder.getAllDOTypeInDOC(stage,MovieClip,callBack);<br>
     * trace(items.length);<br>
     * function callBack(object:MovieClip):void{ trace(object.name) };</p>
     *
     * @param container Objects parent (DisplayObjectCotainer);
     * @param typeClass Objects Class type;
     * @param forEach For every object call back function;
     * 
     * @langversion 3.0
     * @playerversion Flash 9.0.28.0
     *  
     *  @playerversion AIR 1.0
     */ 
    public static function getAllDOTypeInDOC(container:DisplayObjectContainer,typeClass:Class,forEach:Function=null):Array {
        var a:Array = [];
        var len:int = container.numChildren;
        while(len--){
            var o:DisplayObject = container.getChildAt(len);
            if(flash.utils.getQualifiedClassName(o) == flash.utils.getQualifiedClassName(typeClass)){
                a[a.length] = o;
                if(forEach != null)
                    forEach(o);
            }
            if(o is DisplayObjectContainer){
                var aa:Array = getAllDOTypeInDOC(o as DisplayObjectContainer,typeClass,forEach);
                var lena:int = aa.length;
                while(lena--){
                    a[a.length] = aa[lena];
                }
            }
        }
        return a;
    }
}
}
查看更多
走好不送
6楼-- · 2019-01-21 03:42

MovieClip.addFrameScript() is a undocumented ActionScript 3.0 feature that allows you to specify a function that is called when the playhead of the Movieclip timeline enters a particular frame number.

function someFunction():void {

}

movieclip_mc.addFrameScript(4,someFunction);

The frame number is zero based (1st frame = 0) and needs to be whole numbers only, however if you wanted to use frame labels you could use something like this:

function addFrameLabelScript(frame:String, func:Function):void{
    var labels:Array = currentLabels;
    for(var i:int=0;i<labels.length;i++){
        if(labels[i].name == frame){
            addFrameScript(labels[i].frame-1,func);
        }
    }
} 
查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-21 03:42
Graphics::drawRoundRectComplex (x:Number, y:Number, width:Number, height:Number, topLeftRadius:Number, topRightRadius:Number, bottomLeftRadius:Number, bottomRightRadius:Number) : void;

It's not documented anywhere, but a forum post explains more.

查看更多
登录 后发表回答