Home / Tutorials / Games / Making a Game /

Flash Tutorials

Making a Game - Page 2

Posted by : fflash on Jun 29, 2008

 

5.0/5

PART TWO[/st]In this, final, part of the tutorial, we will make this:

Here is an example of the game.

As you should be able to see, the number rises and the button stops it. But added on we have made a random target number, a restart button, and detected whether or not the target number has been reached. To do this, you will learn about:

  • The Math.random function [list][li]The Math.round function [/li][/list][list][li]The goto function [/li][/list]So, let us review our code so far: onClipEvent (load) { var number = 0; var notpress = true; } on (press) { notpress = false; } onClipEvent (enterFrame) { if (notpress) { _root.numbertext.text = number; number += 1; } else { _root.numbertext.text = number; } }

Believe it or not, we will be adding even more code to that. So what has to be done first? Well, we have to set a target number. We will set that in a variable. Here is where you will learn about both the Math.random function and the Math.round function. We will start with the Math.round function, as that is the simplest one. The structure of the Math.round function is this: Math.round(*formula*) It basically rounds whatever is in its brackets to the nearest integer. Relatively simple. But now we have to learn the Math.random function. This is also relatively simple, but seems very complicated when put into practice. It basically produces a random number between 0 and 1. You can produce a random number between any two number easily by typing something like this: (Math.random()*((Maximum number - Minimum Number)) + Minimum number To help you understand, I will give an example. Say I wanted a random number between 50 and 100. Well I would type: (Math.random()*50) + 50 So, we have the random number between 0 and 1, and then we are multiplying it by 50, meaning that the random number will have a range of 0 and 50, but I want the minimum number to be 50, not 0, so I add 50 to the number produced, making the minimum number 50, but also increasing the maximum number to 100, which is why you multiply by the maximum number minus the minimum number; because the minimum number will be added again. Get it? Well, hopefully you understand, and if you don't, just bear with me.

So let's use those functions.

Under: onClipEvent (load) { var number = 0; var notpress = true;

Type: var target = Math.round((Math.random()*80)+20) What we have done, is declare an new variable, target, which will hold our target number, and said that it will be a random number between 20 and 100, and then made sure that they were integers by adding the Math.round function. Now we have our target number, so we have to assign it to a dynamic textbox. So, create a dynamic textbox on the stage and give it an instance name of "targettext", and then, below the line of code you just wrote, type: _root.targettext.text = target By the way, I don't think I explained last time, but, I am referring to it as "_root.targettext" because it is on the root timeline. So your code so far should be: onClipEvent (load) { var number = 0; var notpress = true; var target = Math.round((Math.random()*80)+20) _root.targettext.text = target } on (press) { notpress = false; } onClipEvent (enterFrame) { if (notpress) { _root.numbertext.text = number; number += 1; } else { _root.numbertext.text = number; } }

If you test your movie, a random target number should appear in your target textbox. The next step is another "if.......else" function, when we will be adding the win/lose text. So, add another dynamic textbox and give an instance name of "wintext" and add to your ActionScript so that it says this: onClipEvent (load) { var number = 0; var notpress = true; var target = Math.round((Math.random()*80)+20) _root.targettext.text = target } on (press) { notpress = false; } onClipEvent (enterFrame) { if (notpress) { _root.numbertext.text = number; number += 1; } else { _root.numbertext.text = number; if (number == target){ _root.wintext.text = "win" }else{ _root.wintext.text = "lose" } } }

You will notice another "if...else" statement inside the original one we had. So, what we are saying is, if it has been pressed, check if the number variable is equal to the target variable, and if it is, make the wintext textbox = "win", but if it isn't, make it equal lose. And that is all the code in that button. All we have to do now is simply, add the restart button. This is easy. Make a new button symbol called restart, and open up its action panel. Inside, type this: on(release){ gotoAndPlay(2) }

This is where we learn about the "goto" function. It basically takes you to another frame and/or scene. It can be either "gotoAndPlay" or "gotoAndStop". It is constructed like this: gotoAndPlay(frame number); or this: gotoAndStop(frame number); Pretty easy. So we have told Flash that when the button is released, it should go to frame 2. But wait a minute, we don't have a frame two! So let's make one. Go to frame two and press F7 to insert a new blank keyframe. i.e. there is nothing in it. Then click on the frame and press F9 to open up its actions panel. In it, type: gotoAndStop(1); So, what the restart button does is basically goes to frame 2, where you are redirected back to frame 1, where the game restarts. And that's it! So there we have it, our own number matching game! Thank you for reading this, and I hope you have learned something.

no comment

Add comment

Please login to post comments.