02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Flash</strong> <strong>MX</strong> <strong>2004</strong> <strong>Games</strong><br />

%><br />

var b = Number(Request("B"));<br />

var op = Request("op");<br />

var output = "answer=error";<br />

Response.Expires = -1;<br />

if (op == "add") {<br />

output = "answer=" + (a + b);<br />

}else if (op == "subtract"){<br />

output = "answer=" + (a - b);<br />

}<br />

Response.Write(output);<br />

The first line tells ASP that the scripting language is JavaScript. Then the actual script is contained<br />

between the tags ‘’. Because the language is JavaScript it is very similar <strong>to</strong> <strong>ActionScript</strong>.<br />

First we declare a few variables. ‘Number(Request(“A”))’ extracts the variable A in the query<br />

string and turns it in<strong>to</strong> a number rather than the default of a string. If the file is saved as ‘calc.asp’<br />

in folder ‘scripts’ of server ‘myserver’ then we can call the page using<br />

http://myserver/scripts/calc.asp?A=13&B=8&op=add<br />

In this case the value for the variable a will be set <strong>to</strong> 13, b <strong>to</strong> 8 and op <strong>to</strong> ‘add’. Using<br />

‘Response.Expires= −1’ tells the browser not <strong>to</strong> cache the page. Then we test for the appropriate<br />

op; if either ‘add’ or ‘subtract’ is found then we build a string, ‘output’. Finally this is sent <strong>to</strong><br />

the browser using ‘Response.Write(output)’. If we called this from <strong>Flash</strong> then the variable ‘answer’<br />

would be set <strong>to</strong> one of ‘a + b’, ‘a − b’, or ‘error’. The output can contain multiple values if each<br />

variable, value pair is connected <strong>to</strong> the rest using the ampersand character (&).<br />

Using the LoadVars object<br />

A new feature in <strong>Flash</strong> <strong>MX</strong> was the LoadVars object. This useful object is designed <strong>to</strong> make<br />

communicating with external data easier for the developer. The object uses callback functions. A<br />

callback function is simply a function that is called when a particular event occurs. You tell the<br />

object where <strong>to</strong> find the function and then this function will be called whenever a certain event<br />

takes place.<br />

Suppose we have the following code on frame 1 of our main timeline:<br />

178<br />

lv = new LoadVars();<br />

lv.onLoad = loaded;<br />

lv.load("myvariables.txt");<br />

s<strong>to</strong>p();

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!