A Flash Developer Resource Site














Internet Commerce

Partners & Affiliates














Developer Channel

internet.com


Featured Flash FLA
Gallery Downloads 11337 Flash Movies | 1 New Flash Movies Added
What's New | Top 100

Featured FLA

» Author: Bugra Ozden
» Title: Skatalog v9 - product catalog
» Description: Create your product catalog easly and publish on your website or Create your image gallery, documents list, portfolio. Fully XML Driven
» More by Bugra Ozden


Random FLAs | Add Flash Movie
Featured Flash Site
Gallery Downloads 5828 Flash Sites | 0 New Flash Links
What's New | Top 100 Flash Site

Featured Site

» Posted in the Flash Kit Links section
» Title: Creative DW Image Show PRO
» Description: Creative DW Image Show PRO is a Dreamweaver extension which enables the user to create multimedia presentations. It combines the features of the popular Creative DW Image Show with the ability to add professional text effects to slides (similar to After Effects). The product is very customizable: the user can choose the duration of the transition effects, the slide motion start and end position, zoom and panning type for both images and texts.


Random Links | Add your own Flash Related Links
Flash Tutorials 1280 Tutorials 7 New Tutorials Added!
What's New | Top100

» Make a Flash Slide Show Screen Saver
» Simple flash making tutorial for thanksgiving
» Create flash banner for website
» Create xml slideshow with free template
» How to Insert a Multilingual Subtitle Into Your Flash Video Studio
» How to Create Cool Halloween Slideshow
» Debugging flash using the Firebug console
» Create Flash Slideshow on Blogger
» FLASH TRICKS IN WEB ADVERTISING: FLASH BANNERS
» Unknown Tag: Title10
Random Tutorial | Add Site

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume


Tutorials Home What's New Top Rated Submit myTutes Random!

Search Tutorials


Tutorials Tutorials » Games

Categories How to create scoreboards for your flash games
Author: Nick Kuh | Website: http://www.flasharcadia.com |

 
Page 4
«prev 1 2 3 4 5 6 7 next»

Setting up the first section of the PHP script

Write the php script using your favourite text editor. Make sure you save it as highscores.php

The php script is split into two parts: the first part loads the current set of high scores and the second adds the current player's score to the database and then refreshes the high scores in the flash movie.

Here's the first section:

if ($command=="init"){
$link = @mysql_connect("localhost", "YOURUSERNAME",
"YOURUSERPASSWORD");
if (!$link){
print "&result=Error connecting to database";
exit;
}
if (!@mysql_select_db("YOURDATABASENAME")){
print "&result=Couldn't select database";
exit;
}
$players = "";
$scores = "";
$innerquery = "SELECT * FROM highscores ORDER BY scores DESC";
$innerresult = @mysql_query($innerquery);
$numPlayers=mysql_num_rows($innerresult);
for($loop=0;$loop<$numPlayers; $loop++) {
$newPlayer = mysql_result($innerresult,$loop,"players");
$newScore = mysql_result($innerresult,$loop,"scores");
$players = "$players
$newPlayer"; $scores = "$scores
$newScore"; } printf("&players=%s", $players); printf("&scores=%s", $scores); mysql_close($link); }

Let's breakdown the code

this is the tag used to begin a php script

if ($command=="init"){
The script looks for a variable called command. If it exists and it's value equals init then the script executes the first section. So by setting our flash action script variable command to equal init before we called highscores.php for the first time this section of the php script will be executed.
$link = @mysql_connect("localhost", "YOURUSERNAME",
"YOURUSERPASSWORD");
The new link variable connects to our database using your username and password.
if (!$link){
print "&players=Error connecting to database";
exit;
}
if (!@mysql_select_db("YOURDATABASENAME")){
print "&players =Couldn't select database";
exit;
}
If the php script is unable to connect to the database then it quits the rest of the script and creates sends the sting information back to the flash movie. For anyone who's imported variables into flash from a text file this format will look very familiar. Flash will interpret the information sent as the value of a variable called players.
If all of the connections to our mySQL data are successful then the main body of our code will be executed:
$players = "";
$scores = "";
This ensures that these two variables are reset so that our results are new.
$innerquery = "SELECT * FROM highscores ORDER BY scores DESC";
$innerresult = @mysql_query($innerquery);
The first line creates a php variable which holds a basic SQL command. It selects al l (* - this symbol means all in a SQL command) of the data from our highscores table, it then arranges it in score order (lowest score first) and then reverses the order so that the player with the best score is now in first place.
The second line executes the SQL command.
$numPlayers=mysql_num_rows($innerresult);
This variable calculates the number of rows in our highscores table using a standard mySQL command.
for($loop=0;$loop<$numPlayers; $loop++) {
$newPlayer = mysql_result($innerresult,$loop,"players");
$newScore = mysql_result($innerresult,$loop,"scores");
$players = "$players
$newPlayer"; $scores = "$scores
$newScore"; }
Ok, so here we're creating a loop using almost identical syntax to action scripting. The loop will finish when it has got results from the total number of rows in our mySQL table. $newPlayer and $newScore are temporary variables used to store the player's name and score from the current row being looped. The values of $newPlayer and $newScore get added to $players and $scores respectively each time the script loops. Notice how in php you can place a variable in the middle of a string. If the dollar sign wasn't used then php would not recognise our variables.
printf("&players=%s", $players);
printf("&scores=%s", $scores);
Finally we create two Flash variables called players and scores which will be sent back to our flash game. In the layout above, php will replace the %s with whatever appears after the comma.
mysql_close($link);
}
It's always good manors to close the connection to your database when you've finished receiving data.

«prev 1 2 3 4 5 6 7 next»

» Level Intermediate

Added: : 2002-04-21
Rating: 6.74 Votes: 19
Hits: 4203
» Author
Nick is a Flash Developer for London based web design agency Edition Interactive. His main role is online games design and development for clients such as Channel Four Television.
» Download
Download the files used in this tutorial.
Download (94 kb)
Get conversion and unzipping tools for PC and Mac here!

» Forums
More help? Search our boards for quick answers!

Please rate this tutorial, 10 is the top rating, you can also click the comments link to read/write a review.
10 9 8 7 6 5 4 3 2 1
www.flasharcadia.com. &URL=http://www.flashkit.com/tutorials/Games/How_to_c-Nick_Kuh-771/index.php"> Read or Post Comments
 
   
 

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs