Archive for the ‘Actionscript’ Category

Receiving data with no commas or special characters

This promblem occurs in AS3 when you’re sending and loading data through a URLrequest like this

var myData:URLRequest = new URLRequest(“http://www.mydomain.com/somescript.php”)
myData.method = URLRequestMethod.POST
var variables:URLVariables = new URLVariables()
variables.password = “somepassword”;
myData.data = variables
var loader:URLLoader = new URLLoader()
loader.dataFormat = URLLoaderDataFormat.VARIABLES
loader.addEventListener(Event.COMPLETE, dataOnLoad)
loader.load(myData)
function dataOnLoad(evt:Event){

trace (loader.data.someVar)

}

The variable if it is a string say comma delimited, will have no commas. It is because you didn’t send any variables ie you didn’t include the line in red.

Calling Button Functions from Elsewhere AS3

Usually when you set up a button listener in actionscript 3 it listens for a mouse event and then fires a function. That functions needs to have the mouse event set as a parameter like this book now buttom function:

bookNow.addEventListener(MouseEvent.MOUSE_DOWN,setupBooking);

function setupBooking(event:MouseEvent){

//this is where the functions methods are written

}

The problem is that if I want to call that function from some other place in my code, like from within another function, ie.

setupBooking();

the error fires saying “expecting 1 argument but it received 0.”

The way around this is to declare the argument as null in the function definition, like this:

function setupBooking(event:MouseEvent=null){

}

then we can call it with or without an argument.

AS3 Functions in switch statements and error #2007

For any of you actionscript coders out there that may be searching for this. I discovered through trial and error that functions cannot be defined inside switches ie the following will not work

The code assumes there is a button on the stage called myButton

var myVar:String = “test”

switch(myVar){

case “test”:

myButton.addEventListener(MouseEvent.MOUSE_DOWN,doMyFunction); //this listener will be reported as null error #2007

function doMyFunction(event:MouseEvent){

trace (“function is working”);

}

}

You have to move the definintion of the function outside the switch like this:

var myVar:String = “test”

switch(myVar){

case “test”:

myButton.addEventListener(MouseEvent.MOUSE_DOWN,doMyFunction); //this listener is OK

}

function doMyFunction(event:MouseEvent){

trace (“function is working”);

}

This is not the same as the if-then statement function and so can be a trap for those who begin using switch statements to be concise. Hope it helps.

Return top