Archive for the ‘Flex’ Category

  • Object是一切之源,要重视,要重视。

    几个最基本的属性和方法,其实很多时候都派得上用场,但是我们经常忽略它们。

    废话不说,看代码就全明白了。

    var object:Object = { propA: "value A",
                          propB: "value B",
                          propC: "value C" };
    
    var isEnumerable:Boolean;
    isEnumerable = object.propertyIsEnumerable( "propA" );
    // true
    isEnumerable = object.propertyIsEnumerable( "propB" );
    // true
    isEnumerable = object.propertyIsEnumerable( "propC" );
    // true
    
    for (var prop:String in object)
    {
        trace( prop + " = " + object[prop] );
    }
    // propA = value A
    // propB = value B
    // propC = value C
    
    var object:Object = { propA: "value A",
                          propB: "value B",
                          propC: "value C" };
    
    object.setPropertyIsEnumerable( "propA", false );
    
    var isEnumerable:Boolean;
    isEnumerable = object.propertyIsEnumerable( "propA" );
    // false
    isEnumerable = object.propertyIsEnumerable( "propB" );
    // true
    isEnumerable = object.propertyIsEnumerable( "propC" );
    // true
    
    for ( var prop:String in object )
    {
        trace( prop + " = " + object[prop] );
    }
    // propB = value B
    // propC = value C
    
  • 这样就能绑定成功

    private var _currentSet:ArrayCollection;
    
    [Bindable (event="currentSetChange")]
    public function get currentSet():ArrayCollection
    {
    return _currentSet;
    }
    
    public function setPhotos(photos:Array):void
    {
    _currentSet = new ArrayCollection(photos);
    dispatchEvent( new Event(’currentSetChange’))
    }
    

    这样就不能

    [Bindable (event="currentSetChange")]
    public var currentSet:ArrayCollection;
    
    public function setPhotos(photos:Array):void
    {
    currentSet = new ArrayCollection(photos);
    dispatchEvent( new Event(’currentSetChange’))
    }
    

    为什么呢…

  • 这个类似于副标题的status显示在Panel顶部的右边

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="vertical"
    verticalAlign="middle"
    backgroundColor="white" viewSourceURL="srcview/index.html">
    
    <mx:Style>
    Alert {
    statusStyleName: "myStatus";
    roundedBottomCorners: false;
    dropShadowEnabled: false;
    cornerRadius: 10;
    }
    
    .myStatus {
    color: red;
    fontWeight: bold;
    }
    </mx:Style>
    
    <mx:Script>
    <![CDATA[
    import mx.controls.Alert;
    
    private var alert:Alert;
    
    private function init():void {
    var alertText:String = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse rutrum metus nonummy augue. In hac habitasse platea dictumst. Nulla arcu libero, nonummy non, suscipit a, mollis non, augue. Maecenas porttitor urna vel enim. Nam eget tortor. Mauris facilisis suscipit felis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Proin nunc turpis, venenatis non, laoreet at, fringilla nec, purus. Sed sodales. Sed turpis. Vestibulum sagittis justo id metus. Sed placerat, nibh lobortis mattis adipiscing, sapien wisi interdum arcu, nec vehicula sem tortor id nibh.";
    var alertTitle:String = "The quick brown fox jumped over the lazy dog.";
    alert = Alert.show(alertText, alertTitle);
    alert.status = "I'm a status message";
    }
    ]]>
    </mx:Script>
    
    <mx:ApplicationControlBar dock=”true”>
    <mx:Button label=”Show alert” click=”init();” />
    </mx:ApplicationControlBar>
    
    </mx:Application>
    
  • [Embed(source="bitmap.png",scaleGridTop="10",scaleGridBottom="10",scaleGridLeft="10",scaleGridRight="10")]
    private var png:Class;

  • 也就是很熟悉的混合模式,BlendMode类枚举了所有的混合模式,如下

    • BlendMode.ADD ("add"):通常用于创建两个图像之间的动画变亮模糊效果。
    • BlendMode.ALPHA ("alpha"):通常用于在背景上应用前景的透明度。
    • BlendMode.DARKEN ("darken"):通常用于重叠类型。
    • BlendMode.DIFFERENCE ("difference"):通常用于创建更多变动的颜色。
    • BlendMode.ERASE ("erase"):通常用于使用前景 Alpha 剪掉(擦除)背景的一部分。
    • BlendMode.HARDLIGHT ("hardlight"):通常用于创建阴影效果。
    • BlendMode.INVERT ("invert"):用于反转背景。
    • BlendMode.LAYER ("layer"):用于强制为特定显示对象的预构成创建临时缓冲区。
    • BlendMode.LIGHTEN ("lighten"):通常用于重叠类型。
    • BlendMode.MULTIPLY ("multiply"):通常用于创建阴影和深度效果。
    • BlendMode.NORMAL ("normal"):用于指定混合图像的像素值覆盖基本图像的像素值。
    • BlendMode.OVERLAY ("overlay"):通常用于创建阴影效果。
    • BlendMode.SCREEN ("screen"):通常用于创建亮点和镜头眩光。
    • BlendMode.SUBTRACT ("subtract"):通常用于创建两个图像之间的动画变暗模糊效果。
    其中,BlendMode.LAYER只是强制缓冲图像,不进行实际混合,有很多实际用途,例如,使TextField的Alpha起作用…
  • 原来describeType是这么用的…

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
    creationComplete="init()">
    <mx:Script>
    <![CDATA[
    import mx.controls.Alert;
    public function init():void{
    
    var description : XML = describeType( this );
    var accessors : XMLList = description.accessor.( @access == "readwrite" ).@name;
    
    var str:String;
    for ( var i : uint = 0; i < accessors.length(); i++ )
    {
    var name : String = accessors[ i ];
    
    str += (name+”\n”);
    }
    Alert.show(str);
    }
    ]]>
    </mx:Script>
    </mx:Application>
  • 作用:返回name所指向的类的引用

    示例:

    package {
        import flash.display.DisplayObject;
        import flash.display.Sprite;
        import flash.utils.getDefinitionByName;
    
        public class GetDefinitionByNameExample extends Sprite {
            private var bgColor:uint = 0xFFCC00;
            private var size:uint = 80;
    
            public function GetDefinitionByNameExample() {
                var ClassReference:Class = getDefinitionByName("flash.display.Sprite") as Class;
                var instance:Object = new ClassReference();
                instance.graphics.beginFill(bgColor);
                instance.graphics.drawRect(0, 0, size, size);
                instance.graphics.endFill();
                addChild(DisplayObject(instance));
            }
        }
    }

    说白了,就是要实例化Sprite的时候不写new Sprite()的另外一种方法,再延伸一点,就是可以用字符串来决定创建什么类。或者可以用来读取共享库中的类。

  • 作用就是为当前类添加一个事件类型。

    例如:

    package
    {
    import flash.events.EventDispatcher;
    
    import mx.events.FlexEvent;
    [Event(name="add", type="mx.events.FlexEvent")]
    public class customEvent extends EventDispatcher
    {
    public function customEvent()
    {
    this.addEventListener(FlexEvent.ADD,***);
    }
    }
    }

    如果不加上[Event(name="add", type="mx.events.FlexEvent")]的话,addEventListener();中就不会出现FlexEvent.ADD这个代码提示。