100 ROUNDS, a Flash game with Php/MySql hiscore table and explained source code
I want you all to play 100 ROUNDS, a game created for didactic pourpose.
The game is simple: just enter your name, press "Play" and start moving your mouse clockwise trying to make 100 rounds as fast as you can to enter the highscore table.
I took some ideas from a script found on actionscript.org... but I cannot find it.
For this game I provide the entire source code.
Let's start with the .fla file.
The movie has two frames, the first one with the main title where you can enter your name and press "Play" to... play, and the second frame where you can find the game itself.
Frame 1
There isn't so much to say about frame 1, the actionscript is:
-
playbutton.onRelease = function() {
-
gotoAndPlay(2);
-
};
-
stop();
Lines 1-3 check if the playbutton (the istance name of the Flash default button component) has been released (that means it has been pressed).
On the textinput component the actionscript is:
-
on(change){
-
_root.playername=this.text;
-
}
We just want the playername variable to be updated with the textinput content every time it changes.
Frame 2
Frame 2 is a bit more complicated, and the entire script is located in a movieclip called script located in the center of the movie. Important! At the center of the movie (I will explain later).
As you may know if you played the game, it does not start until you press the mouse button.
So the first part is
-
onClipEvent (mouseDown) {
-
if (!playing_game) {
-
playing_game = 1;
-
rounds = 0;
-
quadrant = 3;
-
_root.rounds = 0;
-
start_time = getTimer();
-
}
-
}
I created a playing_game status, to detect if I am playing or not.
Line 2: if I am not playing...
Line 3: Set the playing_game variable to 1 (now I am playing)
Line 4: Set the completed rounds to 0.
Line 5: This is the "trick" of the game. How can I know when a player makes a complete clockwise round? I simply divided the area into 4 quadrants... the top-left one, the top-right, the bottom-right and the bottom-left. When the mouse is on a quadrant, I will set the quadrant to be touched by the mouse as the next one.
This means you can complete a fake clockwise round even if you make a counter-clockwise round, but it will take more time because you will have to perform three counter-clockwise rounds.
Line 6: Set the completed rounds to 0
Line 7: Saves the time. We need to save it now to calculate how many time you need to complete 100 rounds
Now, we need some script to call when the mouse is moving
-
onClipEvent (mouseMove) {
-
if (playing_game) {
-
_root.help = "Complete 100 clockwise rounds";
-
x = this._xmouse;
-
y = this._ymouse*-1;
-
angle = Math.atan(y/x)/(Math.PI/180);
-
if (x<0) {
-
angle += 180;
-
}
-
if (x>=0 && y<0) {
-
angle += 360;
-
}
-
_root.arrow._rotation = angle*-1;
-
if (quadrant == int(angle/90)) {
-
quadrant--;
-
if (quadrant == -1) {
-
quadrant = 3;
-
rounds++;
-
_root.rounds = rounds;
-
if (rounds == 100) {
-
playing_game = 0;
-
_root.rounds = ":)";
-
running_time = _root.timer_txt;
-
player_name = _root.playername;
-
_root.help = "Press mouse button to restart";
-
getURL("http://www.triquitips.com/100rounds/round_score.php", "round_score", "POST");
-
}
-
}
-
}
-
updateAfterEvent();
-
}
-
}
As you can see in line 2, the whole code is executed only if I am playing the game (if I pressed mouse button, as explained before)
Line 3: Change the help content to display new text on the top of the movie
Line 4: Define x as the actual x mouse position
Line 5: Define y as the actual y mouse position and invert it from positive to negative or from negative to positive. You must do it because flash movie coordinates aren't equal to cartesian coordinates (the ones you need).
Note: x and y mouse position aren't relative to the flash movie. We are detecting x and y mouse position relative to the script object located in the center of the movie (so we are detecting x and y mouse position relative to the center of the movie - that's why we must place the object at the center of the movie)
Line 6: Detect the angle of the mouse... a simple formula you can find on every old school book.
Lines 7-12: Adjust angle value to have the right angle value
Line 13: Set the arrow rotation to angle value (it will rotate the arrow according to mouse movement)
Line 14: Remember the quadrant thing? Well, we now have to check if the mouse is in the quadrant it has to be. In that case...
Line 15: Decrease quadrant value. Now you have to move the mouse onto the next quadrant
Line 16: If you just moved mouse onto quadrant 0, you completed a round. So...
Line 17: Restore quadrant to 3
Line 18: Increase rounds value
Line 19: Display rounds performed
Line 20: If you made 100 rounds...
Line 21: Set playing_game to 0. Game over dude...
Line 22: Displays a smiley
Line 23: Save on running_time the amount of time you needed to complete 100 rounds (it will be explained later how to calculate the time)
Line 24: Save in player_name the name you entered into the text input in frame 1
Line 25: Change the help content to inform you have to press mouse button to restart
Line 26: POST movie variables to a php page in round_score iframe (to be explained later)
Line 30: Update the movie
The last thing we need in the flash movie is a timer
-
onClipEvent (enterFrame) {
-
if (playing_game) {
-
elapsedTime = getTimer()-start_time;
-
elapsedHours = Math.floor(elapsedTime/3600000);
-
remaining = elapsedTime-(elapsedHours*3600000);
-
elapsedM = Math.floor(remaining/60000);
-
remaining = remaining-(elapsedM*60000);
-
elapsedS = Math.floor(remaining/1000);
-
remaining = remaining-(elapsedS*1000);
-
elapsedH = Math.floor(remaining/10);
-
if (elapsedHours<10) {
-
hours = "0"+elapsedHours.toString();
-
} else {
-
hours = elapsedHours.toString();
-
}
-
if (elapsedM<10) {
-
minutes = "0"+elapsedM.toString();
-
} else {
-
minutes = elapsedM.toString();
-
}
-
if (elapsedS<10) {
-
seconds = "0"+elapsedS.toString();
-
} else {
-
seconds = elapsedS.toString();
-
}
-
if (elapsedH<10) {
-
hundredths = "0"+elapsedH.toString();
-
} else {
-
hundredths = elapsedH.toString();
-
}
-
_root.timer_txt = hours+":"+minutes+":"+seconds+":"+hundredths;
-
}
-
}
Very simple and basic, it starts calculating the time only if you are playing the game. Remember the start_time variable? Now we have a elapsedTime value that we calculate as the difference from actual time and start_time. The remaining lines are only math and string operations to give the time a chronometer feel.
And the flash is done!!!
Now, let's spend some time on
getURL("http://www.triquitips.com/100rounds/round_score.php", "round_score", "POST");
As said, we passed some information on a page called round_score.php. Now will need some Php/MySql lines to have the hall of fame.
First, we need a MySql table to store the best times
-
CREATE TABLE `high_round` (
-
`id` INT(11) NOT NULL AUTO_INCREMENT,
-
`name` TEXT NOT NULL,
-
`htime` TEXT NOT NULL,
-
`DATE` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
-
PRIMARY KEY (`id`)
-
)
Field explications:
id: the index
name: player name
htime: time to complete 100 rounds
date: actual timestamp
Now, the Php to populate the table and get the best times.
-
<?
-
-
-
if(($_POST['player_name'])and($_POST['running_time'])){
-
$sql = "insert into high_round(name,htime) values ('".$_POST['player_name']."','".$_POST['running_time']."')";
-
}
-
$sql = "select * from high_round order by htime asc";
-
for($x=1;$x<=$number;$x++){
-
$html .="<tr bgcolor = \"#005eff\"><td>$x</td><td>$row[name]</td><td>$row[htime]</td></tr>";
-
}
-
-
?>
-
<html>
-
-
<head>
-
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
-
<title>100 ROUNDS:: dicactic flash game by Emanuele Feronato</title>
-
<style>
-
body{margin:0px;background-color:#0001ff}
-
td{font-size:11px;font-family:verdana;color:white;line-height:20px}
-
</style>
-
</head>
-
-
<body>
-
<table border = "0" cellspacing = "1" cellpadding = "3" bgcolor = "white"><tr bgcolor = "#971500"><td align = "center" colspan = "3"><strong>HALL OF FAME</strong></td></tr><tr bgcolor = "#0067ff"><td width = "30"><strong>#</strong></td><td width = "150"><strong>Name</strong></td><td><strong>Time</strong></td></tr><?php echo $html; ?></table>
-
-
</body>
-
-
</html>
Lines 3-4: Connection to database
Line 6: if we receive a POST from the movie
Line 7-8: Execute the MySql query yo insert player name and time into the table. We do not need to insert id and date because have automatic values
Line 10-11: Execute the MySql query to select the table ordered by htime (the time you needed to complete 100 rounds) ordered from the smaller to the bigger. Being a time, the smaller, the better.
Line 13-16: Fetch the result and output as table rows.
Remaining lines are just plain html.
Well, that's all folks!!
Take the Flash source code and the Photoshop source of the title graphics.
Give me feeback!! Comment! Comment!
Other Resources: Stock Control Software - Wasp Barcode ofers stock control software packages to provide small businesses the efficiency of large companies at prices that are affordable.
Tell me what do you think about this post. I'll write better and better entries.
They can be easily customized to meet the unique requirements of your project.
41 Responses to “100 ROUNDS, a Flash game with Php/MySql hiscore table and explained source code”
Leave a Reply
Trackbacks
-
Movie Database on
December 15th, 2006 4:23 am
Markus
It was quite useful reading, found some interesting details about this topic. Thanks.
-
iPod Downloads on
December 16th, 2006 7:40 am
movie
Nice comments. Well, time to take a break and go download some nice


Hi Emanuele!
…you’ve done a great job on this. First I wanna thank you for commenting your code so precicely and as second Iwanna mention that my hand always go weird after 50 seconds or so… really fun!
thanks and best wishes
Michael
Hello Emmanuele !
Well, well, well. What do we have here ? A wonderfully commented actionscript to help beginners with such concepts as variables incremntating.
Thank you so much for your neat and minute explaination.
Keep going.
monsieur f
You only told us the Actionscript not everything,
my game did not work because you did not explain everything correctly!
Well, tell me why it’s not working
Good thanks alot for the great in depth explanation on it. This is great for all begginers
man,this sucks!!!!!!tell me how to make a game!!
How can i open the “script” file? i cannot seem to open it, so i can edit the content of it?! im rather new to Flash, but i have tried for 2 hours now :) Can you hint me? do i need a actionscript editor?
I have Flash Professionel 8.0 and i opened your fla file, and i just cannot edit the Movie Clip “script”
The only problem that I have is the sql data query. It keeps giving me an error. ALL I AM TRYING TO DO IS SET UP A HIGHSCORE TABLE!!!! GRRRRR. IT IS SO FRUSTRATING!!!
BTW try unlocking the layer that has it on it. The layer or clip is possibly locked.
Where is the source download?
nvm I just found it.
Well thanks for expalining the script but i still dont understand alot im super new to flash. so do we have to remember the action script, or are we supposed to just find it?
the source file dosent work!!!!!!!
I can’t get mine to work. Can you do a turorial with pictures to help explain otherwise it is to hard.
Wah thanks berat ya om, dah ada pandangan baru untuk bikin web game yg keren.
sekali lagi thanks
First i realy like your artical
what the probalam in this line :
getURL(”http://olgames.byethost9.com/testgames/score.php”, “score”, “POST”);
score is the name of the frame
and score php is the name of the score page
its dont work and i have an eroor page
your game is fucked the highscorelist is replaced by google.nl wasn’t me :P
you c u didn’t replace any source code thats why people can do it in 00:00:00 because they use a small script in their name to set clock back to 0 before posting to php
Am I allowed to freely use this game?
What I wish to do is to add a preloader and make the game submit its scores to the SMF Arcade modification.
It would work as the scores variable SMF Arcade expects would be set with this:
score = 0-elapsedTime;
Sure you can.
A link would be appreciated but you are free to use the game anyway.
yourgame sucks balls
Hey I’m new to php - I understand the user name and password part of the script, but what is my host? If I’m using 100mb.com is that my host? or is the name of my database the host? Please help…
I can’t seem to find the actionscript to the game itself in the .fla file, and none of the layers are locked, is it hidden in some funky way that I can’t figure out? :)
Thanks,
Im familiar with PHP. But have never been looking into Flash, its a good tutorial to get me started.
BTW,
A cure to the problems with the highscore being set to a page is to secure the username variable before its entered into the database. I belive a user has entered javascript in their username.
And one thing, im using PHP 5, and get method works but not POST method. Are you fmailiar with this problem?
thanx alot, just been looking everywhere for a script like this, then remembered about this site . . . much appreciated
:D
For my first ever attempt at PHP and MySQL, this was surprisingly cooperative. I didn’t have any trouble getting any of this to work. It’s not exactly the goal I have to use a database in this particular way, but it was a nice affirmation to actually have the scripts all work together without complaints from any of the client programs or the server.
If you have any tutorials on sending variables to a MySQL database to be retrieved back into Flash, please let me know. This was great. Kudos.
can u please make a tutorial with files that u can copy and paste
and show what to change depending on the game
so i can put that into my game and at will work
, cause i dont get anything of this tutorial
Interesting.
As a beginner I find this very helpful. Glad to have come here at this site.
Hi, first of all thanks Emanuele for all your tuts. They are really wonderful. I´m just starting in flash, and you helped me really so much..
Have to say i have been for four days looking for a highscore tut. I found two very interesting ones, but could not get them working, i think it was an issue of my server. I´m sure you will know them, Jeroen´s one, EgoAnt´s one and finally i found one only php needing, Glen Rhodes. This last one maybe it is a little obsolete, and maybe not secure, but i made it work, on freehostia.com. (on another server it didnt work fine).
It seems to me there are too many variables that can make an php script not working correctly.
So have been trying yours on this page, but it seems it does not work, neither in explorer not firefox. Is this because there are any broken links? i get some strange errors.
So, many thank to you Emanuele.
Hello Emanuele.
Where can I find the game so I can upload it to my site?
The only thing I find is the flash file.
Nils
Download the flash file then publish it and upload that.
I get this in my frame when I test the html
____________________________________________________
“”"Warning: mysql_connect(): Unknown MySQL server host ‘mysql.dayotr.info’ (1) in /home/content/s/i/l/silverneo188/html/100rounds/round_score.php on line 3
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/content/s/i/l/silverneo188/html/100rounds/round_score.php on line 4
Warning: mysql_query(): Can’t connect to local MySQL server through socket ‘/usr/local/mysql-5.0/data/mysql.sock’ (2) in /home/content/s/i/l/silverneo188/html/100rounds/round_score.php on line 11
Warning: mysql_query(): A link to the server could not be established in /home/content/s/i/l/silverneo188/html/100rounds/round_score.php on line 11″”"
_________________________________________________________
whats wrong? what do I put in the “your_host_name” in the php file?
can I hide the php page? coz I don’t wanna have a new page open… how do I fix this? :(
Hi,
I can’t get the game to work. Is there someone who can help me?
Hi, I still can;t find the movie clip called script. Can someone point that out for me?
sorry, but this does tell you how to make a game. I am afraid that YOU are the person who doesn’t know what to do. sharpen up, my friend!
Thanks for the great tutorial. I do a little bit of flash for fun. i’m pretty decent with creating graphics and animations, but this is the first time i’m making an actual game. Thanks alot!
“Yeah, little bobby tables we call him”
Is there a good reason why you don’t check the data that’s coming in for SQL Injection hacks?