Actionscript 3 Sandbox

From Wikicliki
Jump to: navigation, search

AS3 brings the core language aspects of AS2 into complianace with ECMAScript standard (the international standardized programming language for scripting).

dragging

// MANU SCRIPT DRAG

tab2.manu_1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
tab2.manu_1.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
tab2.manu_2.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
tab2.manu_2.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
tab2.manu_3.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
tab2.manu_3.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
tab2.manu_4.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
tab2.manu_4.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
tab2.manu_5.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
tab2.manu_5.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

//rectX, rectY, rectWidth, rectHeight;

function mouseDownHandler(evt:MouseEvent):void
{
	pingSoundPlay();
	var object = evt.target;
	object.startDrag(false, new Rectangle(90,-460, 560, 540));
}


function mouseUpHandler(evt:MouseEvent):void
{
	var object = evt.target;
	object.stopDrag();
}

problem solution

problem: mc.visible = 1 // WRONG, will generate error saying it is int where should be boolean

solution: mc.visible = true // CORRECT

Basics of AS3

OPTION F9 == Actions Panel

  • The difference between AS2 and AS3:
AS2:
my_MC.createTextField("instance_name", 10,0,0 Stage.width, 22);
my_MC.attachMovie("MC_name", "moviename_mc, this.getNextHighestDepth());
10,0,0,Stage.width == dimensions
22 == depth

Versus AS3:
var myText:TextField = new TextField();
var myMC:MovieClip = new MovieClip();
now in AS3 you dont have to createTextField or attachMovie, and you can define all the variables later. 
  • select the property (highlighted in blue in the Actions panel), and press F1 to see a help menu telling you about what values the property can have.
  • strings must be in quotation marks (var userName:<DataType> = "Data";)
  • you assign data type to the variable so you dont lose track of what it is.
  • trace(variableName);
  • trace("long text comment");
  • function thing():void - the void means no return argument
function moveObj(thing:MovieClip):void 
{ 
thing.y -= 50;           			// mc moves 50px up
thing.rotation += 45;		// mc rotates 45 degrees right
}
moveObj(thing1_mc);
function moveObj(thing:MovieClip, movement:Number, rotationAmount:Number):void 
{
thing.y -= movement;           			// mc moves 50px up
thing.rotation += rotationAmount;		// mc rotates 45 degrees right
}
moveObj(thing1_mc, 50, 45);
function moveObj():Number		// the number here defines what kind of thing is returned
{ 
thing1_mc.y -= 50;
thing1_mc.scaleY = 2;
return thing1_mc.y;
}
thing2_mc.y = moveObj();
  • events must have listeners
myMC.addEventListener(type:String,function)
myMC.addEventLIstener(MouseEvent.CLICK, onClick);
  • we are looking for a MouseEvent.SpecificallytheClick and once we hear it a function "onClick" will run
function onClick(event:MouseEvent):void {
trace("its been clicked!!!");
}
  • myMC.buttonMode == use handcursor when its looking for event (eg: mouseevent)
function onClick(event:MouseEvent):void {
event.target.rotation += 45;
}     // this means that the thing which generates the event rotates!