IDAT102 – Flash Game 0036-0040

In another separate .fla I experimented with XML importing and parsing.

I decided to use this BBC weather feed as a source of information to be brought into my game.

I using the example we were given I eventually created this:

//Plymouth temperature finder
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("http://newsrss.bbc.co.uk/weather/forecast/13/ObservationsRSS.xml"));
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
parseXML(xmlData);
}
function parseXML(xmlInput:XML) :void{
var weather:String = xmlInput.channel.item.description;
temp = weather.substring(13,14);
}

The second to last line places the contents of the description tags within the item tags within the channel tags in the XML document. The last line finds the 14th and 15th character of it, which should be the temperature.

I added a function to calculate the frequency the bonus balls appear:

function bonusRate():void {
if(temp < 4){
intervalBONUS = (Math.random() * 20000) + 5000;
}else{
intervalBONUS = (Math.random() * 30000) + 5000;
}
interval1 = setInterval(ballGenBONUS,intervalBONUS);
}

So if the temperature in Plymouth falls below 4 degrees centigrade then the frequency is more likely to be greater.

This code was later moved to the first frame to allow it to load before the bonusRate() function runs.

When testing the game on my server I encountered another error. Flash does not allow referencing data from different domains then the one the .swf is hosted on.

http://kb2.adobe.com/cps/142/tn_14213.html

A suggested workaround was to use a simple PHP proxy by creating a .php file with:

$dataURL = "http://newsrss.bbc.co.uk/weather/forecast/13/ObservationsRSS.xml";
readfile($dataURL);
?>

and by changing the new URLRequest link to point at the .php file hosted on my server. This solves the problem. For the hand in the game must work independently so I have made the code more resilient by defaulting the temp variable to 10.

Also making use of the temp data I have:

var dotsNumGOOD = 40 + parseInt(temp);
var dotsNumBAD = 8 + Math.ceil(parseInt(temp)/4);

So the hotter the temperature the more balls that can spawn. I took care not to unbalance the game to greatly.

In the first frame I also played around with dates.

var currentDate:Date = new Date();
var Year = currentDate.getFullYear();
if(Year > 2012){
messageTxt.text = "Looks like we made it"
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.