Posted tagged ‘variables’

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!

STEP 1:

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!

STEP 2:

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!

STEP 3:

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.

STEP 4:

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.

FUN: Playing around with variables

August 4, 2008

Now, I’ll show you some cool things you can do with variables. To start up, create a new DYNAMIC text box and give it the variable main. This will be the variable that will bounce around.

STEP 1:

So, let’s start this. read the explanations in the codes, so you’ll understand them! Yeah, by the way, make a new MC and put it off stage! Well, now to the first code part, a whole line, yay!

main=1;//The main variable will be a 1

So, test your movie (CTRL+ENTER) and you’ll see the number one.

STEP 2:

It’s easy to make the variable bounce, and it’s easy to modify the code. But no copy/paste! Put the code on the MC which is off stage.

onClipEvent(enterFrame){//When you enter the frame, the code will run itself over and over again
if(_root.main<10){//If the variable main is less than 10
_root.main+=1;//Variable will go higher
}else{//If it's more than that
_root.main=1;//Main will be reset.
}
}

So, if you want it to count to 100 before it will restart, change “10” to “100” and so on. If you want it to start from something else, change the ones to where you want it to start from. You can also put it to count backwards, I’m sure you’ll figure out how 😉

So, now you do probably know variables a bit better, and that’s good, because variables is very important when you go more advanced! It’s almost all about variables and math, lol…. It might sound complicated but it isn’t, it’s kinda fun too 🙂

Picking up health, making enemies and moving a character.

August 3, 2008

Welcome to my first tutorial. This tutorial is a basic tutorial which shows you how to make a health pack, create some enemies and create a simple walking script for a simple flash game.

STEP 1:

Make two different MC’s. Give one of them the instance name hero. The hero MC will be your character that you control in your game. The other one (the health pack) doesn’t need any instance name.

STEP 2:

These MC’s do need some graphical work inside them, so draw a health pack inside the health pack MC and draw a player inside the hero MC.

STEP 3:

Now, let’s begin with making the player walking with the arrow keys. This is very simple and I’m now gonna explain for you how to do it.

Please, take some time to actually understand the code and don’t just copy/paste. If you do that, you will never learn!

onClipEvent (load) { //When the MC have loaded
power = 3; //The speed of the player
}
onClipEvent (enterFrame) { //When you enter the frame, this code will run itself on and on again
if (Key.isDown(Key.LEFT)) { //When you hold down the left key
_x -= power; //x position will go lower according to the "power" variable
this._rotation = 270; //This will rotate the MC 270 degrees.
play(); //Play the walking animation, if you have any
}
if (Key.isDown(Key.RIGHT)) { //When you hold down the right key
_x += power; //x position will go higher
according to the "power" variable
this._rotation = 90; //This will rotate the MC 90 degrees.
play(); //Play walking animation
}
if (Key.isDown(Key.UP)) { //When you hold down the up key
_y -= power;
//y position will go lower according to the "power" variable
this._rotation = 0; //No rotation needed, will reset rotation if the MC was already rotated
play(); //Play the walking animation
}
if (Key.isDown(Key.DOWN)) { //If you hold down the down key
_y += power;
//y position will go higher according to the "power" variable
this._rotation = 180; //Rotate the MC 180 degrees.
play(); //Play the walking animation
}
if (Key.isDown(Key.RIGHT) && Key.isDown(Key.UP)) { //If you hold down both the right and up key
_rotation = 45; //Rotate the MC 45 degrees.
}
if (Key.isDown(Key.LEFT) && Key.isDown(Key.UP)) { //If you hold down both the left and up key
_rotation = 315; //Rotate the MC 315 degrees.
}
if (Key.isDown(Key.RIGHT) && Key.isDown(Key.DOWN)) { //If you hold down both the right and down key
_rotation = 135; //Rotate the MC 135 degrees.
}
if (Key.isDown(Key.LEFT) && Key.isDown(Key.DOWN)) { //If you hold down both the left and the down key
_rotation = 225; //Rotate the MC 225 degrees
} //END!

STEP 4:

Now, let’s click on the first frame on the main timeline. We will now add a health variable. It’s only one line code.

health=100; //This means you have 100 HP's

STEP 5:

Now, let’s add a dynamic text box. Insert a text field, and change it to dynamic in the drop down menu. Then after that, you will see a text field called “Var:” under the properties. Change var to health. If you now test your movie (CTRL+ENTER), you can see that there is the number 100 in the dynamic text field.

STEP 6:

We was talking about making an enemy. This is easy, as long as we don’t want any AI for it. We mustn’t use anything more complicated than hitTest for this. So, create a third MC. This will be the enemy. You must also have some code on it, of course.

The code isn’t many lines, and it’s easy to learn and understand, study, and don’t just copy/paste!

onClipEvent(enterFrame){ //When you enter the frame, code will run over and over again
if(_root.hero.hitTest(_x, _y, true)){ //Checks if the hero MC hits this.
_root.health-=10; //You'll lose 10 health in case you touch this enemy.
}
}

Now, that wasn’t hard, right?

STEP 7:

We’re almost finished, but we must add the health pack first. To begin, double click the health pack MC. Then press the first frame and open the actions panel. Then just write stop(); which will stop the MC from playing.

Now, add a blank keyframe inside the health pack. Then, just return to the main timeline and add the following code (but no copy/paste, I think you’re starting to get it!) to the health pack MC.

onClipEvent (enterFrame) { //When you enter the frame, code will run itself over and over again.
if (_root.hero.hitTest(_x, _y, true)) { //Checks if the hero MC hits the healthpack.
this.gotoAndStop(2);//If it's true, then this healthpack will go to the empty frame, so it can't be picked up again.
if (_root.health<70) { //If your health is less than 70...
_root.health += 30; //Then, you'll get 30 HP's
} else { //But. what if the health is over 70/=70?
_root.health = 100; //Then it will be 100. Full health.

}
}
}

Great, now everything should work! Look out for more tutorials by me!