This is my first step into Actionscript 3.0. I thought I would share it with everyone and at the same time, try to do a very simple thing so that I won’t be daunted by the new language.
What I am going to do:
Create a button that – after being clicked on, will give a dynamic text field a new value.
Firstly, create a new Actionscript 3.0 document and then just draw any object, and convert it into a Button(F8). Give this button an instance name of “button_btn”.
Next, create a new text field, set the type to “Dynamic Text” in the properties panel, and give it an instance name of “output_txt”.
Now before I go on to show you the Actionscript 3.0 code, I’ll show you what the code should be like, should it be in Actionscript 2.0. This code will not work in this newly created AS3.0 document.
Actionscript 2.0 Code:
This is what it will look like it is still AS2.0:
button_btn.onRelease = function(){
output_txt.text = “Button was clicked!”;
}
Now the AS3 Code:
Actionscript 3.0 Code:
function eventResponse(evt:MouseEvent):void {
output_txt.text = “Button was clicked!”;
}
button_btn.addEventListener(MouseEvent.MOUSE_UP,eventResponse);
As you can see, there’s an extra line of code, and a whole bunch of even nonsense. Worry not, the Flash help documentation provided a pretty detailed explaination to get you started to events. I’m going to quote from the documentation since it’s quite well written:
Basic event handling
The technique for specifying certain actions that should be performed in response to particular events is known as event handling. When you are writing ActionScript code to perform event handling, there are three important elements you’ll want to identify:The event source: Which object is the one the event is going to happen to? For instance, which button will be clicked, or which Loader object is loading the image? The event source is also known as the event target, because it’s the object where the event is targeted by Flash Player (where the event actually happens).
The event: What is the thing that is going to happen, the thing that you want to respond to? This is important to identify, because many objects trigger several events.The response: What step(s) do you want performed when the event happens?
Any time you write ActionScript code to handle events, it will include these three elements, and the code will follow this basic structure (elements in bold are placeholders you’d fill in for your specific case):function eventResponse(eventObject:EventType):void
{
// Actions performed in response to the event go here.
}eventSource.addEventListener(EventType.EVENT_NAME, eventResponse);
To summarise, eventSource, is the target MovieClip or Button that you want to add the event to. In our example, it was a button with the instance name button_btn.
eventType is what the user will do to the button. In our case, it was after we clicked the button, which in flash terms is a MOUSE_UP MouseEvent.
eventResponse is the function that will be executed after the event is being done, which in relation to our example means after the button is clicked on, the function eventResponse is called. And in this function, output_txt is assigned a new value.
This is a really basic of basic introduction to Flash Actionscript 3.0 and I hope it will help anyone who’s trying to start learning it a kickstart. Keep sharing!
Here’s the source file to the above example:
Source File: Requires Flash CS3 of course!
what a clear, concise wonderful explanation. thank you so very much!
i m currently working with flashcs3, thanks a lot for startup