Manipulating Objects in Flash AS3

This is a basic tutorial that aims at teaching those who are new to Flash, ways to move objects (MovieClips,Buttons etc.) on the Stage and throws light on the various properties of movieclips and buttons and the ways to manipulate them...

Let's start off by creating a new Flash file (Ctrl+N). Now add/create a movie clip onto the stage of the main movie. I have imported this image (Ctrl+R) onto the stage and converted it into a movieclip (F8).
This should be the Object that we will be manipulating with the help of actionscript. Now we need to give this movieclip an instance name so that we can refer to it while writing our code. To do so, fire up the properties window/pane (Ctrl+F3) and set the instance name to "myObj_mc".


Now we will add buttons onto the stage which will control the movement and properties like transparency (alpha). Add 4 buttons to move the Object in the four directions respectively to act as controls to move the movieclip around and add another button to control transparency as show below.

 Now change the instance names of the button with the left arrow to "left_btn", right arrow button to "right_btn", up arrow button to "up_btn", down arrow button to "down_btn" and the button that says transparent to "alpha_btn".
Once we are done with this, we can proceed to the actionscript part. 


Code ::

up_btn.addEventListener(MouseEvent.MOUSE_DOWN,up_fn);
down_btn.addEventListener(MouseEvent.MOUSE_DOWN,down_fn);
left_btn.addEventListener(MouseEvent.MOUSE_DOWN,left_fn);
right_btn.addEventListener(MouseEvent.MOUSE_DOWN,right_fn);

alpha_btn.addEventListener(MouseEvent.MOUSE_DOWN,alpha_fn);

function up_fn(e:MouseEvent)
{    myObj_mc.y-=10;
   
// this  is equivalent to: myObj_mc.y=myObj_mc.y-10; }
   
function down_fn(e:MouseEvent)
{    myObj_mc.y+=10;
    }
   
function left_fn(e:MouseEvent)
{    myObj_mc.x-=10;
    }
   
function right_fn(e:MouseEvent)
{    myObj_mc.x+=10;
    }
   
function alpha_fn(e:MouseEvent)
{    myObj_mc.alpha=0.5;
    }


Code description ::

up_btn.addEventListener(MouseEvent.MOUSE_DOWN,up_fn);
down_btn.addEventListener(MouseEvent.MOUSE_DOWN,down_fn);
left_btn.addEventListener(MouseEvent.MOUSE_DOWN,left_fn);
right_btn.addEventListener(MouseEvent.MOUSE_DOWN,right_fn);


alpha_btn.addEventListener(MouseEvent.MOUSE_DOWN,alpha_fn);


In these first few lines, we are creating eventListeners to detect events from the event stream and when a "MOUSE_DOWN" event is detected, it calls the corresponding functions wherein we define how it should react to such an event.
MOUSE_DOWN is a subset of class MouseEvent and when the event is recorded, it calls the corresponding function with arguments as a MouseEvent...


function up_fn(e:MouseEvent)
{    myObj_mc.y-=10;
   // this  is equivalent to: myObj_mc.y=myObj_mc.y-10; }
   
function down_fn(e:MouseEvent)
{    myObj_mc.y+=10;
    }
   
function left_fn(e:MouseEvent)
{    myObj_mc.x-=10;
   

}
   
function right_fn(e:MouseEvent)
{    myObj_mc.x+=10;
    }
   
function alpha_fn(e:MouseEvent)
{    myObj_mc.alpha=0.5; //sets transparency to half
    }



In the above code, "x" "y" and "alpha" marked in green, are properties of movielips that define the placement along X-axis , along Y-axis and the transparency respectively.
When we say myObj_mc.y-=10; This means that we are moving the movieclip by 10pixels, upwards. Similarly, the movements along other directions are written in the other functions and the transparency is set to half when the corresponding button is pressed (refer alpha_fn function).

Note:: The origin is considered to be at the top-left corner and as we move right, x-coordinate increases and as we move down, y-coordinate increases.
And alpha (transparency) takes values from 0-1, 0 corresponding to full transparency and 1 corresponding to no transparency.


  The functions defined are that the event listeners would call on recording a mouse event. We are defining functions with a parameters "e" which is a MouseEvent Object, this is because when an eventListener calls the functions, it sends the MouseEvent as an argument and if we don't define parameter "e", flash would detect an Argument Mismatch Error.


To download the source files, clik here (movement.fla).
 This tutorial can be extended to buttons and graphics too as they carry almost the same properties.
In case of doubts or any problems faced while trying out this tutorial, feel free to leave a comment below.

Introduction to Buttons & Events in Flash (AS3)

Buttons are objects categorized as Symbols and are used to enhance user interaction. Their timeline consists of four frames or states namely Up, Over, Down, Hit which are respectively the frame to display when the button is inactive, when the user brings his mouse over it, when the user clicks it and the area in which the button should accept mouse events...

Creating a Button ::
To create a new button, select "New Symbol" from the insert menu or use the keyboard shortcut, Ctrl+F8 and choose Button as the type for the new symbol. 
Select the Up frame and draw on the stage, in a way you would like the button when idle. 
Now right click the Over frame and select "Insert KeyFrame" this should create a duplicate of the Up frame which you can edit or you could select "Insert Blank KeyFrame" if you wish to edit from scratch either way, make the Over frame reflect how you would like the button to look when the mouse moves over. 
Similarly, edit the Down frame and the Hit frame.
Note that the Hit frame is not essential for the button to function but then it's advised you use it to define the button area. This can be done by drawing a shape in the hit frame to reflect the buttons area of effect. 

Now that we are done with creating our button, we need to write some actionscript to tell the button how it need to react to Mouse Events (when a mouse moves over, mouse button is pressed, etc.).

Before we do that,we need to move back to the main stage by clicking on the Scene1 link toward the top right corner of the window as show...

Now open the library (Ctrl+L) where the button we just created is listed and drag it and drop it on the stage of the main movie. This should create an instance of the button. 
Open the properties window (Ctrl+F3) and select the button on the stage. In the properties pane, change the instance name to "my_btn".

Now Select the first frame from the timeline, right click and select "Actions" this should open the actions pane where you will need to put the following code.


Code ::

my_btn.addEventListener(MouseEvent.CLICK,handleClick);

function handleClick(e:MouseEvent)
{
trace("my_btn was clicked !!!");
// code to be executed on click here
}


 Code Description ::
my_btn.addEventListener(MouseEvent.CLICK,handleClick);

In this line, we are assigning an Event Listener to the button (instance name : "my_btn") to listen to the Events Stream and call the function "handleClick" when a MouseEvent, "CLICK" is registered.
In simple words, the function is called when the user clicks the button we put on the stage.
Syntax for initializing an eventHandler :: Object.addEventListener(Event,function_name);

function handleClick(e:MouseEvent)
{
trace("my_btn was clicked !!!");
// code to be executed on click here
}

Here, we are defining a function by name handleClick which will be called when a mouse click event is registered.
Here a parameter variable, "e" of the type MouseEvent is set because the eventHandler function passes an event variable as arguments and the function needs to accept it though we dont use it in the code.


Now that you have a basic idea about using buttons and listening to events, you can extend your knowledge to different events and also edit the basic handleClick function in this tutorial and extend it for more complex operations.

Related Posts with Thumbnails