I've been thinking about making a bunch of AS3 tutorials for a while, and have now started! After seeing in AS3: Main that paranoia suggested a basics tutorial, I thought 'why not?', and started this. Anywho...
Changes from AS2 and some Need-To-Know stuff
- You cant only put code anywhere other than on frames.
- _root does not exist anymore, the closest thing to it is stage, but it isnt really the same. Best thing to do is this.parent (_parent changed to parent), thats what I do.
- All properties with an underscore infront of them have been un-underscored (for example, _x is now just x and _rotation is rotation)
- You have to import EVERYTHING (well, nearly). Check out this to find out where you need to import thing from.
- Events are key. No more 'onEnterFrame = function()', you need events if you want something triggered.
- Variables need to be declared. Always.
- _xmouse and _ymouse have been changed to mouseX and mouseY
- _xscale and _yscale have been converted to scaleX and scaleY, and have been changed so that instead of it being out of 100, its just out of 1.
- It's become incredibly hard to detect keyboard inputs without the use of dELta's open source class.
Examples
Ok, so want some examples to see how stuff works, so you can use it later. I'll give you some more easy-peasy stuff.
Enter Frame Code
addEventListener(Event.ENTER_FRAME, OEF)
function OEF (event:Event){
//Your enter frame code.
}
Ok, step by step:
addEventListener(Event.ENTER_FRAME, OEF)
This code says that the function 'OEF' (btw, doesnt have to be called that, thats just what I use) should be run whenever the ENTER_FRAME event is triggered.
function OEF (event:Event){
This creates a function called OEF. The thing that screws me up the most is the event:Event bit. The event bit is saying that this function isnt just a regular function, its only triggered by an event. The :Event bit says that that particular type of event was just a regular Event, if it was more special, like a mouse event, it would say :MouseEvent.
//Your enter frame code.
This is where the code you want to run every frame goes.
}
This closes the function.
See, that wasnt scary, was it? Ok, second example...
Button Codes
So heres a code that is kinda like the first, but uses a different event. A whole different type of event, even.
Ok, to start off, you need a button or movie clip on stage, with an instance name of 'MyButton'. Surprizingly, this is going to be your button. Put this code on the frame:
import flash.events.MouseEvent
MyButton.addEventListener(MouseEvent.CLI CK, onClick)
function onClick (event:MouseEvent){
//Code goes here
}
This should be pretty self explainatory, but what the heck!
import flash.events.MouseEvent
This imports the MouseEvent thing, so that you are able to use it. Told you that you had to import everything!
MyButton.addEventListener(MouseEvent.CLI CK, onClick)
This adds and event listener to 'MyButton', for the MouseEvent CLICK, and it means that whenever MyButton is clicked, the function 'onClick' is triggered.
function onClick (event:MouseEvent){
This declares a function called 'onClick'. In the brackets this time is event:MouseEvent, which says that this function is designed for being triggered by events, specificly MouseEvents.
//Code goes here
This is where your code goes. Did you really not work this out?
}
This ends the function.
And thats the end of the basics! Comments, anyone?
I hope the text-formatting worked, I used a lot of it...