I am pretty novice when it comes to scripting and I am struggling to achieve something pretty simple. I want to have an external text file that will allow me to control the timeout number that I reference in my code below. I need to be able to change this variable by editing a simple text file.
var myTimer:Timer = new Timer(60000,1);
I want to change the 60000 and the 1.
I have followed what looks like a nice clean approach from this tutorial: http://www.designscripting.com/2012/01/as3-flash-loading-variables-fro m-a-text-file/
However, where I am stuck is how to reference my imported variable in the code. I have tried "name1" but this doesn't work. I get the '1067: Implicit coercion of a value of type Number to an unrelated type String.' error'
I am getting an output:
HTTP Status = 0
name1 :: undefined
name2 :: 1}
I am using CS5 and AS3.
Sorry - my bad - here is the code:
var urlLoader:URLLoader = new URLLoader( );
urlLoader.addEventListener( IOErrorEvent.IO_ERROR, handleIOError );
urlLoader.addEventListener( HTTPStatusEvent.HTTP_STATUS, handleHttpStatus );
urlLoader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, handleSecurityError );
urlLoader.addEventListener( Event.COMPLETE, handleComplete );
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.load( new URLRequest( "dataFile.txt" ) );
function handleIOError( event:IOErrorEvent ):void
trace( "IO error: " + event.text );
function handleHttpStatus( event:HTTPStatusEvent ):void
trace( "HTTP Status = " + event.status );
function handleSecurityError( event:SecurityErrorEvent ):void
trace( "Security Error: " + event.text );
function handleComplete( event:Event ):void
trace( "Data loaded" );
trace( "name1 :: " + urlLoader.data.name1 );
trace( "name2 :: " + urlLoader.data.name2 );
and the contents of the data file (just a .txt file):
I don't see anything wrong with what you show, and when I test it I get the following output...
HTTP Status = 0
name1 :: 6000
name2 :: 1
The only thing I can suspect at the moment is the data file itself. I can reproduce the problem if I insert a blank space before that string, so make sure the text string is not preceded by anything.
Thanks - you are right. The text file was corrupted. However, I am still getting the error
'1067: Implicit coercion of a value of type Number to an unrelated type String.'
Any ideas how I can make the Timer accept my numbers from the text file?
When you want to use the strings as numeric values, caste them as Number objects...
var myTimer:Timer = new Timer(Number(urlLoader.data.name1) ,Number(urlLoader.data.name2) );