Chapter 30 Example 1

by actionscriptbible
♥0 | Line 50 | Modified 2010-02-04 18:06:14 | MIT License
play

ActionScript3 source code

/**
 * Copyright actionscriptbible ( http://wonderfl.net/user/actionscriptbible )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dDpJ
 */

package {
  import com.actionscriptbible.Example;
  
  import flash.events.*;
  import flash.net.*;
  public class ch30ex1 extends Example {
    public function ch30ex1() {
      var fileReference:FileReference = new FileReference();
      fileReference.addEventListener(Event.SELECT, onSelect);
      fileReference.addEventListener(Event.CANCEL, onCancel);
      
      var btn:TestButton = new TestButton(100, 25, "Browse");
      addChild(btn);
      btn.x = btn.y = 20;
      btn.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
        fileReference.browse([
          new FileFilter("All Files", "*"),
          new FileFilter("Images", "*.png;*.jpg")
        ]);
      });
      trace("\n\n\n");
    }
    private function onSelect(event:Event):void {
      var fileReference:FileReference = FileReference(event.target);
      var extension:String = fileReference.name.match(/\.\w+$/)[0];
      trace("So you like " + extension + "s, huh?");
    }
    private function onCancel(event:Event):void {
      trace("What, you don't like files?");
    }
    
  }
}
import flash.display.*;
import flash.text.*;
class TestButton extends Sprite {
  public var label:TextField;
  public function TestButton(w:Number, h:Number, labelText:String) {
    graphics.lineStyle(0.5, 0, 0, true);
    graphics.beginFill(0xa0a0a0);
    graphics.drawRoundRect(0, 0, w, h, 8);
    label = new TextField();
    addChild(label);
    label.defaultTextFormat = new TextFormat("_sans", 11, 0, true, false,
      false, null, null, "center");
    label.width = w;
    label.height = h;
    label.text = labelText;
    label.y = (h - label.textHeight)/2 - 2;
    buttonMode = true;
    mouseChildren = false;
  }
}