Wednesday, March 3, 2010

AS2 to AS3 conversion

Ok so your good at as2 and ya wanna go to as3 huh? Well I'll teach ya what ya need to know here.

So first thing first. stop(); and play(); still work the same as they do.


And timeline control is a also exacly the same so all that gotoAnd and stuff work, same with next/prevFrame();


All variables have to be defined.


Now for the all so difficult frame events. So first thing you need to know is that all code goes on the timeline, so no code on MCs. Also all event's have to be in functions.


Let's just make a simple enterframe event that increases a variable


//Define the variable
var myNum:Number = 0


You may or may not be familier with listeners from as2 so I'll explain. When you make a listener you create a small section of script that waits for an event to happen like clicking or a press of a key. These listeners don't have to go in an on() or onClipEvent() block.

So first you wanna make the function to run ever frame first


var myNum:Number = 0
//ignore the evt parameter for now. It will be further expained later
//Fuctions are the same as as2.
function myFunction(evt:Event):void {
//If blocks are the same as as2
if (myNum<10) {
myNum++;
}
trace(myNum);
}

So there's a simple function to increase a variable up to 1o. But it is not being ran at the momment, to run it you have to add an event listener like this:


//Event.ENTER_FRAME is a method that make the function run all the time. Type 'Event.' to bring a list of possible events. The ones in capital letters are the most commonly used.

addEventListener(Event.ENTER_FRAME,myFunction);

function myFunction(evt:Event):void {
if (myNum<10) {
myNum++;
}
trace(myNum);
}

That is a completion of the code we were attepting to make.

Now let's make a button event. First let's make an MC or button on the stage. And name it say... myButton_btn


//So first lets make a event listener on the button.
//MouseEvent.CLICK is pretty self expanety.
myButton_btn.addEventListener(MouseEvent.CLICK,buttonFunction);

//Then the function. Again ignore the parameter.
function buttonFunction (evt:MouseEvent){
trace("clicked")
}


So pretty much the same.

So I guess I'll give some tips.
1. The esc hints don't work any more. 3: So no more Esc+i+f to make your if blocks.
2. By the way the parameters will take to long to expain I'll do it later but you can have any variable there I just chose to use "evt" then the class or the event your checking.
3. If ya need any more help then PM/e-mail me or, Well post it here.

--Sasuke2910

No comments:

Post a Comment