Posted tagged ‘button’

Making a simple Motion Tween using script

August 8, 2008

Okey, this isn’t really a real motion tween, but it will basically do the same thing. It’s simple, as simple as it can get, almost….

So, I’m gonna teach you how to make a motion tween using script, and I will also learn you how to do a play and pause button for it. So, go on to step one, now!


Well, let’s begin by looking at the first frame in the movie. It’s empty, for now. But soon, it won’t be. Begin with creating two buttons. They do not need any instance names. The first button should be a play button, the second one should be a pause button. Another thing you could do is to add a MC, no instance name this time either. Now, a last thing to do at this step, add the following code to the first frame!

playpause=2; //The tween is paused, change to 1 to have it play as default


Now, you got two buttons. You need to add some code on them, of course. If there’s some new commands in the code, read it until you do understand the code, and don’t just copy/paste.

This one is for the play button:

on(release){ //When you release the button
_root.playpause=1; //Set the variable to 1 (play)

And here’s a code for the pause button:
on(release){ //When you release the button
_root.playpause=1; //Set the variable to 2 (pause)

Good, but there’s still the most important part missing. Take a look at step 3!


Let’s finish it! You need to add some code to the MC, or it won’t work. The code you need is basically simple, and if you know how to use variables, you should have no problem to modify it. And anyway, there’s descriptions for every line with any commands.

onClipEvent(load){ //When MC have been loaded
speed=5; //Set the speed of the motion tween
startx=100; //Set the X starting position
starty=100; //Set the Y starting position
endx=220; //Set the X final position
endy=300; //Set the Y final position
onClipEvent(enterFrame){//When you enter the frame, this code will run itself over and over again
if(_root.playpause===1){//1 is play, pause is 2. It must be on play.
if(this._x< to >)
this._x+=speed;//The MC X axis will go higher
if(this._y< to >)
this._y+=speed;//The MC Y axis will go higher


Only 17 lines! And it’s still working! In total, you used 24 lines(!), and the result is a tween using script, with a play and pause button too!

Oh yeah, maybe you can use this for something, I have no idea if you can find something to do with it, but if you use it, feel free to post links!


More AS2 Basics: Programming a button and variables introduction

August 4, 2008

I want this blog to have tutorials in many dificulties, but I’m still trying to make it useful for those who are new to AS2. So I’ll learn you something as simple as programming a button. Enjoy!


Create a new flash document. When you’ve done that, you should, as usual, have ONE keyframe, which is empty because you haven’t added anything there yet. Let’s begin with adding some script on that frame, click it and press F9 to open the actions panel. The code you should write is the easiest code there is in AS2:

stop(); //This will stop the movie from playing any further

That was step one!


So, now you should add a blank keyframe after the current frame. Right click on the right side of the current frame and press “Insert Blank Keyframe”. Good, that’s it!


Go back to the first button and create a new button. Draw something, select your drawing, and press F8. Set symbol instance to button. Then press OK.


Now comes some script, read it, and put it on the button.

on(release){//When you release the button....
gotoAndStop(2);//Will go to the specified frame (2) and stop playing. Stop can be changed to Play in case you want it to play

That’s the first example, but we’re going to show you some more. Basically, you change the gotoAndStop(2); part to what command you want. But we’ll begin with changing line 1:

//This code shouldn't be applied to the movie, it's instructions.
//Edit the code inside the stars! on(*release*){
press //When you click the button
releaseOutside //When you release the button, cursor may not be above the button
rollOver //When you move your mouse over the button
rollOut //When you roll your mouse away from the button
dragOver //Holding down the mouse button and dragging over the button
dragOut //Holding down the mouse button and dragging away from the button
keyPress"" //When you press the specified key. There's also better codes for this.

Now, you can also modify line number two, here’s some simple things you can change it to:

//This code shouldn't be applied to the movie, it's instructions.
gotoAndPlay(2); //Same as gotoAndStop(); but this plays the movie. 2 is the frame it will go to.
nextFrame(); //Goes to the next frame in the movie, this stops
prevFrame(); //Goes to the previous frame in the movie, just like nextFrame();, this stops
number +=1; //Will make the number variable go up by one.
number -=1; //Will make the number variable go down by one
number =1; //Will set the number variable to one

That was it, and yeah, if you want that number variable to work, add the following code to the first frame in your movie:

number=1; //number is the name of the variable, 1 is what it will be by default.

And a very last thing, how to show the variable. Add a new text field, change the type to dynamic by clicking the little drop down menu and then in the var: text field, type the name of the variable. In this case, “number”.

So, now it’s over, for real.