How to Controlling Movie Clip by Buttons

Make a box, the select it and press F8 make it a button and press ok.. Give it a instance
name “mc”.
Create a button and name instance as “btn_right”.
Press on the first frame in the timeline and press F9. In the Action panel copy the following
code.
btn_right.addEventListener(MouseEvent.CLICK, move_right);
function move_right (e:Event){
mc.x += 5;
}
this will make make your Movie Clip move to right.
Remember “+” sign keep adding the value to x.
to understand the x and y, Just remember as a photographic memory x stands for horizontal
and y stands for vertical.
Now to move Movie Clip in all directions UP, DOWN, LEFT and RIGHT. We need to make
more buttons name the instances as “btn_down” ,“btn_up” and “btn_left”.
To move left :
btn_right.addEventListener(MouseEvent.CLICK, move_left);
function move_left (e:Event){
mc.x -= 5;
}
To move up :
btn_right.addEventListener(MouseEvent.CLICK, move_up);
function move_up (e:Event){
mc.y -= 5;
}
To move down :
btn_right.addEventListener(MouseEvent.CLICK, move_down);
function move_down (e:Event){
mc.y += 5;
}

Advertisements

How to Create a Music Player – Basic

 

  1. Your need at least one .wav file. If you dont have it. Select any mp3 file and then convert it to .wav. Once you have a .wav file , import that file in your new flash library .
  2. Now create two buttons as “Play” & “Stop”. Instance name as “play_” & “stop_” respectively.
  3. Open the library and right click on the wav file. Select Linkage. Set class as “my_sound_file”.
  1. Goto frame 1 and copy the code:
     

    // set my_sound_file as : sound.

    var sound:Sound=new my_sound_file();

    var my_channel:SoundChannel;

     

     

    play_.addEventListener(MouseEvent.CLICK, Play_it)

    function Play_it (e:Event){

    // play the sound.

    my_channel = sound.play();

    }

     

    stop_.addEventListener(MouseEvent.CLICK, stop_it)

    function stop_it (e:Event){

    // stop the sound.

    my_channel.stop();

    }

     

    5. Test Movie . If every thing works fine , you shall enjoy further advance tutorial.

Controlling Movie Clip by Buttons AS3

 

Make a box, the select it and press F8 make it a button and press ok.. Give it a instance name “mc”.

 

Create a button and name instance as “btn_right”.


Press on the first frame in the timeline and press F9. In the Action panel copy the following code.

 

btn_right.addEventListener(MouseEvent.CLICK, move_right);

 

function move_right (e:Event){

mc.x += 5;

 

}

 

this will make make your Movie Clip move to right.

Remember “+” sign keep adding the value to x.

 

to understand the x and y, Just remember as a photographic memory x stands for horizontal and y stands for vertical.

 

 

 

 

 

 

Now to move Movie Clip in all directions UP, DOWN, LEFT and RIGHT. We need to make more buttons name the instances as “btn_down” ,“btn_up” and “btn_left”.

 

To move left :

 

btn_right.addEventListener(MouseEvent.CLICK, move_left);

 

function move_left (e:Event){

mc.x -= 5;

 

}

 

To move up :

 

btn_right.addEventListener(MouseEvent.CLICK, move_up);

 

function move_up (e:Event){

mc.y -= 5;

 

}

 

To move down :

 

btn_right.addEventListener(MouseEvent.CLICK, move_down);

 

function move_down (e:Event){

mc.y += 5;

 

}

 

}

 

this will make make your Movie Clip move to right.

Remember “+” sign keep adding the value to x.

 

to understand the x and y, Just remember as a photographic memory x stands for horizontal and y stands for vertical.

 

 

 

 

 

 

Now to move Movie Clip in all directions UP, DOWN, LEFT and RIGHT. We need to make more buttons name the instances as “btn_down” ,“btn_up” and “btn_left”.

 

To move left :

 

btn_right.addEventListener(MouseEvent.CLICK, move_left);

 

function move_left (e:Event){

mc.x -= 5;

 

}

 

To move up :

 

btn_right.addEventListener(MouseEvent.CLICK, move_up);

 

function move_up (e:Event){

mc.y -= 5;

 

}

 

To move down :

 

btn_right.addEventListener(MouseEvent.CLICK, move_down);

 

function move_down (e:Event){

mc.y += 5;

 

}

 

Action to Object in Array AS3 – Tutorial

 

 

make a movie clip,  drag it from libray. We need at least 4  copies of same movie clip.Now rename each as mc_0, mc_1,mc_2, mc_3.

in action script panel past this code.

var mc:Array=new Array (mc_0, mc_1,mc_2, mc_3);
trace (mc.length);
==========================================
//Output
4
==========================================

Now how we have 4 movie clips , how are we going to give the action to all ?We nee to create a loop to give actions to all mc”s in array.
in action script panel past this code.

for (var i:int; i<mc.length; i++){

trace(“ok”);
}
==========================================

output should be as :
ok
ok
ok
ok
==========================================
add this above  trace(“ok”)

mc[i].addEventListener(Event.ENTER_FRAME, move);

Now we have given action to all the objects. Now we need to create and action , what action to be performed by the movie clips ? paste this code after “for”.

function move(e:Event){

mc[i].rotation ++;

}
Your last code should look like this :

==========================================
var mc:Array=new Array (mc_0, mc_1,mc_2, mc_3);
trace (mc.length);

for (var i:int; i<mc.length; i++){
mc[i].addEventListener(Event.ENTER_FRAME, move);
trace(“ok”);
}

function move(e:Event){

mc[i].rotation ++;

}

==========================================

This will make your all objects rotate around itself. If this works.

try following
mc[i].alpha = 0.2;
mc[i].x= 5;
mc[i].y= 5;

==========================================
remember  mc[i].x += 5;   the plus sign keeps om adding the value to object. Means mc will keep moving in x direction till it goes out of the stage.

these are many things we can do, we shall proceed to advance level in next tutotials.
Thank you.

Array in AS3 (Basic)

array is like a file or like a book in which we can store information about names or numbers.

var numbers:Array=new Array (1,2,3,4,5,6,7,8,9,0);
var god_names:Array=new Array (krishna, shiva, buddha, christ);
var countries:Array=new Array (india, nepal, china, thailand);

trace (numbers);
trace (god_names);
trace (countries);

Output//

1,2,3,4,5,6,7,8,9,0.
krishna, shiva, buddha, christ.
india, nepal, china, thailand.
=============================================

trace (countries.length); // this gives the number of the words stored in an Array.
Output//
4

Movie Clip Drag Tutotial AS3

 

Make a movie clip and give in an instance name “mc”.
click on the frame in the timeline and press F9.

and past the following code :

mc.addEventListener (MouseEvent.MOUSE_DOWN, start_drag);
function start_drag (e:Event){

mc.startDrag();

}

 

this will allow to drag the movie clip, now we also need to give it an action as stop drag when mouse click in disable.

mc.addEventListener (MouseEvent.MOUSE_UP, stop_drag);
function stop_drag (e:Event){

mc.stopDrag();

}

 

Hope you got it. 🙂

How to create simple Calculator in Flash ( Actionscript 3 )

Create buttons and text fields as created above. give them instance name and change its type. Write down this code in action panel . And run it ( Crlt  + Enter ).  as mentioned in picture.

Explanation of code:

num1.restrict=”0-9″
num2.restrict=”0-9″
// so that user can only type numbers in text box.

cal.addEventListener(MouseEvent.CLICK, result_);
// Action to button. (cal is our button)
//result_ is our function , in simple words action to button.

function result_(e:Event){
//Introduced new variable ( r ) , num1 + num2 means addition of two numbers,  here first we have converted text into numbers ( uint ),  then converted to text again ( String ).var r:String=String(uint(num1.text)+uint(num2.text));

// Inserted ( r = result), r into textfeild (re ).
re.text=r;

// end of function. } ============================================================================================ This simplest Tutorial, after following this , if you have doubts , can ask me. ============================================================================================ This is calculator create by me. In photoshop and Flash. After working in actionscript you can also create your own graphic. And add it to button layer.

Calculator

Get Biggest Number in Array ( Actionscript 3 )

//get biggest number…………

// define array and number
var avg:Array=new Array(1,5,8,9, 43,56,88,54,67,90,66,45);

// define where big number holder

var bigno:uint=0;

// define result
var res:uint;

// for loop repeat action
for(var i:uint=0; i<avg.length; i++){

// condition if 0 is less than avg[i], then bigno  equal to avg[i]
if(  bigno<avg[i] ){
bigno=avg[i]

}

}
//finally trace results
trace(bigno);
trace(avg.length);
}

Average in Actionscript 3

// define array
var avg:Array=new Array(13,24,35,46,21,34,78,96,56,78,90,65,43);

// define bigno to hold biggest number
var bigno:uint=0;

// define a variable to hold sum of array
var res:uint;

//for loop to repeat actions and get sum of array.
for(var i:uint=0; i<avg.length; i++){

//condition for bigno, if bigno is smaller than number in array.
if(bigno<avg[i] || bigno>0){

// res for result of sum of total numbers in array.
res+=(bigno+avg[i]);

}
}

//trace sum of array
trace(“total”+res);

// to know actual length of  array
trace(“length of arr”+avg.length);

// to know the final result, res= length of array.
trace(“result is  “+res/avg.length);

/////////////////////////////////////////////////////////////

//you should get the following result

total679
legth of arr13
result is  52.23076923076923