One of the biggest challenges for me while working on this game has been developing a working database that is able to communicate with the game and vice versa.
The database we are using is mySQL driven and as such cannot interact with flash directly, however it has been possible to transfer information between the database and the game via php scripts.
The first thing we do in this process is to send the information, we want the database to recieve, from flash to the php script. In this example I will use the coin system, so flash will be sending the users unique id and the amount of money earned to the php.
The code we use is as follows:
_root.addCoin = function() {
coinbag = new LoadVars();
coinbag.user_id = userID;
coinbag.amount = coinIncrease;
replyCoinbag = new LoadVars();
replyCoinbag.onLoad = openCoinbag;
coinbag.sendAndLoad("http://www.academy.artdesignhull.ac.uk/coinIncrease.php",replyCoinbag,"post");
};
function openCoinbag(success) {
if (success) {
_root.currencyFigure.text = "$"+this.currencyFigure;
}
}
This script creates the function "addCoin" which can be called at any point in time, this exact script will also work for the shop to reduce the coin as a negative number can be used with no errors.
Now the two figures sent to the php come into play:
coinbag.user_id = userID;
coinbag.amount = coinIncrease;
:
The PHP script that controls how these figures are used is:
< ? php
include('db-connect.php');
$result = mysql_query("UPDATE `users` SET `currency`=`currency`+".$_POST['amount']." WHERE `user_id`=".$_POST['user_id'], $conn);
echo mysql_error($conn);
if ($result) {
$a = mysql_query("SELECT * FROM `users` WHERE `user_id`=".$_POST['user_id'], $conn);
if ($a) {
$details = mysql_fetch_array($a, MYSQL_ASSOC);
echo("result=");
$id = $details['user_id'];
$userLoggedIn = $details['displayname'];
$currencyFigure = number_format($details['currency'], 0, '.', ',');
echo("ok&user_id=$id&user_name=$userLoggedIn¤cyFigure=$currencyFigure");
} else {
echo mysql_error($conn);
}
} else {
mysql_error($conn);
}
?>"
This script adds both figures to the database and then checks the users info in the database before sending back all the figures to flash. Before sending the currency figure back it converts it from a regular number such as 28574 to 28,574.
Once it has been sent the final lines of the flash function come into play:
function openCoinbag(success) {
if (success) {
_root.currencyFigure.text = "$"+this.currencyFigure;
}
}
This takes the new currency figure and adding a $ to the start of it, applies it to the currency text box.
This is one of the more simple functions of the database and while there is a leveling / stat system almost complete there are still some bugs with it.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment