More AS2 Basics: Programming a button and variables introduction

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.

Explore posts in the same categories: ActionScript 2.0, Basic

Tags: , , , ,

You can comment below, or link to this permanent URL from your own site.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: