Search This Blog

Friday, April 22, 2011

Learning AS3

Load Movie() in AS3

var swfToLoad: String = "mySwf.swf";
var loader: Loader = new Loader();
loader.load(new URLRequest(swfToLoad));
addChild(loader);

var myLoader:Loader = new Loader();
myLoader.load(new URLRequest("load.swf"));

------------------------------------------------------------------------------------------------
Load Movie using BUTTONS and Progress BAR

import fl.controls.ProgressBar;
import fl.controls.Button;

var my_loader:Loader = new Loader();


var my_btn:Button = new Button();
my_btn.label = "Load Image";

my_btn.x = 100;
my_btn.y = 200;
addChild (my_btn);


var my_pb:ProgressBar = new ProgressBar();
my_pb.source = my_loader.contentLoaderInfo;

my_pb.x = 100;
my_pb.y = 200;


my_btn.addEventListener(MouseEvent.CLICK, startLoading);
function startLoading(e:MouseEvent):void{

my_loader.load(new URLRequest("myPhoto.jpg"));
addChild(my_pb);
removeChild(my_btn);
my_btn=null;
}


my_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishLoading);
function finishLoading(e:Event):void{

addChild(my_loader);
removeChild(my_pb);
my_pb = null;
}


------------------------------------------------------------------------------------------------

Add mouse click event to SimpleButton

package {
  import flash.display.*;
  import flash.events.*;


  public class SimpleButtonDemo extends Sprite {
    public function SimpleButtonDemo(  ) {
      var button:SimpleButton = new SimpleButton(  );
      button.x = 20;
      button.y = 20;
      
      button.upState = createCircle( 0x00FF00, 15 );
      button.overState = createCircle( 0xFFFFFF, 16 );
      button.downState = createCircle( 0xCCCCCC, 15 );


      button.hitTestState = createCircle( 0x000000, 50 );
      
      button.addEventListener( MouseEvent.CLICK, handleClick );
      
      addChild( button );  
    }
    
    private function createCircle( color:uint, radius:Number ):Shape {
      var circle:Shape = new Shape(  );
      circle.graphics.lineStyle( 1, 0x000000 );
      circle.graphics.beginFill( color );
      circle.graphics.drawCircle( 0, 0, radius );
      circle.graphics.endFill(  );
      return circle;
    }
    
    private function handleClick( event:MouseEvent ):void {
      trace( "Mouse clicked on the button" );  
    }
  }
}


--------------------------------------------------------------------
 Load and Save in AS3
 var buttonShape:Shape = new Shape();
buttonShape.graphics.beginFill(0x336699);
buttonShape.graphics.drawCircle(50, 50, 25);
var button = new SimpleButton(buttonShape,
 buttonShape, buttonShape, buttonShape);
addChild(button);


var fileRef:FileReference= new FileReference();
button.addEventListener(MouseEvent.CLICK, onButtonClick);


function onButtonClick(e:MouseEvent):void {
fileRef.browse([new FileFilter("Text", "*.jpg;*.gif;*.png")]);
fileRef.addEventListener(Event.SELECT, onFileSelected);
}


function onFileSelected(e:Event):void {
fileRef.addEventListener(Event.COMPLETE, onFileLoaded);
fileRef.load();
}


function onFileLoaded(e:Event):void {
var loader:Loader = new Loader();
loader.loadBytes(e.target.data);
addChild(loader);
}


var MyTextField:TextField = new TextField();
var MyButtonField:TextField = new TextField();
var MyFile:FileReference = new FileReference();


MyTextField.border = true;
MyTextField.type = TextFieldType.INPUT;
MyTextField.y=100


MyButtonField.background = true;
MyButtonField.backgroundColor = 0x339933;
MyButtonField.x = 150;
MyButtonField.height = 20;
MyButtonField.text = "Click here to save";


addChild(MyTextField);
addChild(MyButtonField);
MyButtonField.addEventListener(MouseEvent.CLICK, clickhandler);


function clickhandler(e:MouseEvent): void {
    MyFile.save(MyTextField.text);
}
 ------------------------------------------------------------------

Switch Case for Buttons


but1.addEventListener(MouseEvent.CLICK, clickHandler);
but2.addEventListener(MouseEvent.CLICK, clickHandler);
but3.addEventListener(MouseEvent.CLICK, clickHandler);


function clickHandler(event:MouseEvent):void
{


switch (event.currentTarget.name)
{
case "but1" :
trace("you chose a red Papa Smurf");
break;
case "but2" :
trace("you chose a green Papa Smurf");
break;
case "but3" :
trace("you chose a blue Papa Smurf");
break;


}
}

No comments:

Post a Comment