Wednesday, 9 July 2008

Clicking game

This Flash tutorial will teach you how to create a simple clicking game. The game consists of random objects which appear repeatedly on the screen. The user has to click on the objects that appear. There are a total of twenty turns available and the number of hits by the user is also counted.

Clicking game part 1

Step 1

Open a new flash document. Set the width to 500px and height to 400px, and then choose whatever background colour you wish.

Import your image you wish to use as your clicking object by selecting File > Import > Import in stage then select your object then click ok. I used a football as my clicking object, but you use whatever you wish.

If you don’t want to import an image you could alternatively create your clicking object using the oval tool at this stage.


Step 2

Convert your clicking object into a button by selecting F8. Give your object an appropriate name, check button and click ok. And give your button the instance name “btn”.



Now convert your button instance into a movie clip by selecting F8. Give your object an appropriate name, check movie clip and click ok. And give your movie clip the instance name “ball”.



So, you should now have button instance inside a movie clip instance.


Step 3

On the top right hand corner of the stage create the following:



The “Hits” and “Total” are static text. And the boxes adjacent the static texts are dynamic text with the border around text checked.



Give each of the corresponding dynamic text the variable name “hits” and “total”.


Step 4

On the timeline insert a new layer called “actions” right click on the 1st frame and select actions from the drop down menu.

Add the following code:

ball._visible = false;
total = 0;
hits = 0;
end = 20;
ball.btn.onPress = function(){
_root.hits++;
};

seeBall = function(){
if (total < end){
ball._x = Math.random()*400;
ball._y = Math.random()*400;
ball._visible = true;
total++;
}else{
ball._visible = false;
}
};
setInterval(seeBall,800);


**This piece of code makes your clicking object move to a random position on the stage. The “hits” increment by a factor of 1 when the object is clicked. The object moves randomly 20 times, but this can be easily changed. When the total reaches 20 the game ends. You can alternatively use a countdown timer.

The setinterval function moves the object repeatedly every 800 milliseconds. Decreasing the time will make the object move faster.


Step 5

Test your movie clip Ctrl + enter.



You should now have a clicking game. Checkout Clicking game part 2.

8 comments:

gangadhar 4 August 2008 at 02:46  

hi., i think there are bugs in the code provided

iliketo 4 August 2008 at 03:26  

@Gangadhar - The bugs are now fixed.

elcid812 26 August 2008 at 21:17  

is there a way to have the "ball" appear all over the screen. Not just up and down?

jebee 23 October 2008 at 05:09  

I want a text to appear when the game is "done" - after the 20 showups. Like "Well done - you scored 18!" or "Try again"

How to do this?

iliketo 14 December 2008 at 10:13  

@Jebee checkout Clicking game part 2

iliketo 9 January 2009 at 11:07  

@Elcid812

The ball should appear ramdomly on the screen.

hpsauxe 31 January 2009 at 12:04  

Is there anyway that you can have 3 different items coming up?

iliketo 31 January 2009 at 15:36  

@Hpsauxe

You could simply repeat the steps, but give the new items different instance names.

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP