Latest tutorial: Premium Flash Files | Ask Tutorial5! | Subscribe to RSS Register Login Find Hobbies
Advertisement

Get tutorials on EMail




Do you need more help? You can now Ask Tutorial5! and get free support - Ask a question now!

Control the playhead of a MovieClip using ActionScript

(13 votes)
Written by AnaS   

1. Create a new Flash document 400x300 dimensions

2. Create two new layers

Rename the layers actions, movie, button starting from the top (like in the picture below).

playhead1.gif

3. Draw a circle

In the first frame of the movie layer use the Oval Tool (O) from the Tools Panel to draw a circle about 40pxx40px (hold Shift key pressed to draw a perfect circle). Select none for Stroke Color and what do you wish for Fill Color.

4. Convert the circle to a MovieClip

Select the circle and choose Modify >> Convert to Symbol (F8). In the Convert to Symbol window that appear choose MovieClip as Type, give it a name of ballMC and choose also center for Registration point like in the following image:

playhead2.gif

5. Go to the Poperties panel and give it an instance name of ball_mc

 

6. Edit in Place

Now right-click on the ball_mc and choose Edit in Place or double-click it. You are in the Edit Mode for ballMC now.

playhead3.gif

7. Convert the ball to a MovieClip

Select the ball and choose again Modify >> Convert to Symbol (F8). In the Convert to Symbol window choose same options like above and give it a name of ball2MC.

8. Now we'll do some animation with the ball

Go to in the first frame of the Layer1 and in the Properties panel choose Tween:Motion. Go to the frame 30 and right-click >> Insert Keyframe (F6). Go to the frame 60 too and repeat the step.

9. Change the position of the ball

Go back to frame 30 and change the position on the ball whetever you wish, but having at least 100 pixels from the original position.

10. Insert a button

Now click Scene 1. Go to the first frame of the button layer. Go to Window >> Common Libraries >> Buttons. Choose from here a what button you desire and drag it on the Stage. In the Properties panel give it an instance name of button_btn.

playhead4.gif

11. The ActionScript code

Now go to the first frame of the actios layer. Open the Actions panel (F9) and put this ActionScript code in:


var contor:Number=0;

 

// we use the contor variable to hold the state of the ball: 1 if it is stopped, 0 if the ball is playing

button_btn.onRelease=function(){
if (contor==1){
ball_mc.play(); contor=0;

// if the ball MovieClip is stopped we use the play() method to make it move

}
else if (contor==0){
ball_mc.stop();
contor=1;
}
}


// if the ball MovieClip is playing we use the stop() method to stop it

 

12. Test the movie

Now test the movie (Ctrl+Enter) and see the result. Hope it was helpful!

You need to Register in order to download the FLA file.



Subscribe now via RSS feed and get all the new tutorials

written by tony , November 22, 2007

hi .

am using actionscript for first time and trying to make a penalty shootout game!

at the moment , i have managed to make the keeper move randomly, the ball chasing the mouseclick and used hit Test() to detect either the save of the keeper or the goal.

however, how:

(i) is it possible to place only 1 shot , and then reset the process of shooting , so the ball is set back to the kick spot?

(ii)can I end the game after 10 ball shootings?

(iii) make the game over after three non goals

i would appreciate your help!!!!

cheers

Do you need more help? Ask now!
 

busy
Last Updated ( Friday, 28 March 2008 )