Introduction to AS3: Variable types

The language of actionscript doesn’t use traditional variable types like advanced computer languages (java, C++, c.) Actionscript does things a little different. They like to be a little more generic since creating room in memory is not a heavy issue. Because of this the data types are a fair amount more user friendly.

The variable types flash uses are as follows:

Int” This stands for integer and can be a negative or positive and thus include 0. (-4,-3,-2,-1,0,1,2,3,4,5 are all examples of the int data type.)

Uint” This is very similar to the int data type however it cannot hold negative numbers, or ‘0.’ It can only hold whole numbers.

Number’ A number can be any number. This include negatives, 0, positives, and decimal numbers (floating point) which is the equivalent of declaring a double, float, or single.

String’ This data type will display characters of text. An example of this would be “text,”

Array’ An array data type is more than one item in a variable. This means that could store a list of bowling scores.(1,6,2,4,2).

Boolean’ A Boolean data type traditionally takes up one bit of data because it only stores a 0, or a 1 which is interpreted to true or false.

Custom object’ This can be whatever you want and is declared as a variable type because it is stored in memory much like a variable. The difference is that this can store multiple variable types.

Introduction to AS3: Creating the package.

Setting up a package for your flash traditionally means that you will be going in the direction of object oriented programming. This means that you will be able to create different class files and use them as objects.

The simplest way to look at this is using any general object such as a city. In order for the city to be properly run we will need an object for power grid, roads, and sewer system. These sub objects can be split up into their own classes and used in the main movie clip by setting up a package.

The basic package layout looks something like this:

1
2
3
4
5
6
7
8
9
10
11
12
package {
import flash.display.MovieClip;
 
public class Main extends MovieClip {
 
public function Main(){
trace("If this appears in the output you have done well.");
} // end of main function
 
} // end of main
 
} //end of package

Tip: When creating a swf object you need to make sure that you do not mix together AS1 and AS2 with action script 3. This is unlikely to happen if you are learning from scratch or teaching yourself but if you need to update a previous project then you will need to make sure you are not mixing levels of actionscript. This is caused by the way they are put together in the creation of the swf file.

Inside of this file you can add all of your movie clips and declare them as variables. You can also import all of the needed files along with create the code required to do what you would like to do.

The last thing that is required when creating a package is the link to your movie clip. You can create this link simply by clicking on the stage, making sure it is your default document, then adding the document class name as shown below.

If everything is ok and your flash program runs you will have a line of confirmation in the output.

Introduction to flash AS3

Introduction to flash AS3

This is a quick briefing before going in depth on different bits and pieces of flash AS3. I will be creating a series of brief introductions into certain aspects and components of the language along with a tutorial using said component.

All of the articles will consist of key notes and source code. These tutorials are a project in their own and will take up a fair amount of time. They will also receive their own section from the top menu and a brief updates in the projects section.

Intro to AS3: Using Mouse Events to Control an Object

Going through the code

View the results

For this creation we are first going to need event listeners in the project. An event listener is something that reacts when an event happens. So within the project we have a fade in and fade out button. Both of these buttons have event listeners that are given instructions on what to do when the event occurs. So in this example we are going to use the rotate left event listener which looks as follows.

rotate_left_btn.addEventListener(MouseEvent.MOUSE_DOWN, onRotateLeft);

This states that for the instance rotate_left_btn it will add an event listener and that event listener is a mouse event that will take place when the mouse button has been pressed down on. Then it will link to the onRotateLeft function.

//Functions for Rotation
function onRotateLeft(evt:MouseEvent):void {
box.rotation -= 10;
};

Note: Take note that information after two forward slashes is read as a comment and will not effect your code in any way. //Comments end when the line ends.

The function above declares that it is first a function then names it onRotationLeft. After naming the function the code will declare that this function has a mouse event then is posted as void. On the next line we state that we want there to be a left rotation so we use -=10;  then end the function with a closing bracket and a semicolon.

Later in the code we have some new mouse events and function that are not mouse buttons but drag actions with the mouse. Theses are created the same way that the other mouse buttons where, there is a function and an event listener but note the front of the event listener where we declare box at the beginning so it will only work when we click down and drag the box.

The Stage

What you will need on your stage is first a movie clip with an instance name of box. Now you will need the button images, these don’t need to be pretty they just need to be functional. The layout is up to you but if you need an example click the link before the action script code to view the SWF file.

The button instances are as follows:

move_left_btn
move_right_btn
move_up_btn
Move_down_btn

scale_up_btn
scale_down_btn

rotate_left_btn
rotate_right_btn

fade_in_btn
fade_out_btn

toggle_visible_btn

The Action script side of things

//Assigning movements with Functions
function onMoveLeft(evt:MouseEvent):void {
box.x -= 20;
};
function onMoveRight (evt:MouseEvent):void {
box.x += 20;Mouse Events: Controlling a Box
};
function onMoveUp(evt:MouseEvent):void {
box.y -= 10;
};
function onMoveDown (evt:MouseEvent):void {
box.y += 10;
};
//Move Mouse Events
move_left_btn.addEventListener(MouseEvent.MOUSE_DOWN, onMoveLeft);
move_right_btn.addEventListener(MouseEvent.MOUSE_DOWN, onMoveRight);
move_up_btn.addEventListener(MouseEvent.MOUSE_DOWN, onMoveUp);
move_down_btn.addEventListener(MouseEvent.MOUSE_DOWN, onMoveDown);
scale_up_btn.addEventListener(MouseEvent.MOUSE_DOWN, onScaleUp);
scale_down_btn.addEventListener(MouseEvent.MOUSE_DOWN, onScaleDown);
//Rotation Mouse Events
rotate_left_btn.addEventListener(MouseEvent.MOUSE_DOWN, onRotateLeft);
rotate_right_btn.addEventListener(MouseEvent.MOUSE_DOWN, onRotateRight);
//Fading Mouse Events
fade_in_btn.addEventListener(MouseEvent.MOUSE_DOWN, onFadeIn);
fade_out_btn.addEventListener(MouseEvent.MOUSE_DOWN, onFadeOut);
//Toggles the Visibility
toggle_visible_btn.addEventListener(MouseEvent.MOUSE_UP, onToggleVisible);
//Functions For Scaling
function onScaleUp(evt:MouseEvent):void {
box.scaleX += 0.1;
box.scaleY += 0.1;
};
function onScaleDown(evt:MouseEvent):void {
box.scaleX -= 0.1;
box.scaleY -= 0.1;
};
//Functions for Rotation
function onRotateLeft(evt:MouseEvent):void {
box.rotation -= 10;
};
function onRotateRight(evt:MouseEvent):void {
box.rotation += 10;
};
//Function For Fade in and Fade Out
function onFadeIn(evt:MouseEvent):void {
box.alpha += 0.1;
};
function onFadeOut (evt:MouseEvent):void {
box.alpha -= 0.1;
};
//Toggle Button to change Visibility
function onToggleVisible(evt:MouseEvent):void {
box.visible = !box.visible;
};
box.addEventListener(MouseEvent.MOUSE_DOWN,onStartDrag);
box.addEventListener(MouseEvent.MOUSE_UP,onStopDrag);
function onStartDrag(evt:MouseEvent):void {
evt.target.startDrag();
};
function onStopDrag(evt:MouseEvent):void {
evt.target.stopDrag();

};

Caleb Jonasson