views:

50

answers:

1

Flash receives the XML, but the values are wrong. How do I fix this?

Problem
I can see the XML loaded with no errors, but my output is way off. It's as though it's not receiving any values. Numbers in the output window and animation move rapidly. The Flash file runs as if it's variables where set to zero. I changed the order of my code, but that didn't help with this. Please explain how I can correct this.

SWF

//load xml
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("xml.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
//parse XML
function processXML(e:Event):void {
myXML = new XML(e.target.data);
trace(myXML);
//receive values from XML
delay = parseInt(myXML.DELAY.text());
trace(delay);   
repeat = parseInt(myXML.REPEAT.text());
trace(repeat);  
}
//variables
var delay:uint = 0;
var repeat:uint = 0;
//timer and event
var timer:Timer = new Timer(uint(delay),uint(repeat));
timer.addEventListener(TimerEvent.TIMER, countdown);
//counter
function countdown(event:TimerEvent) {
myText.text = String(0 + timer.currentCount);
trace(0 + timer.currentCount);
}
timer.start();

XML

<?xml version="1.0" encoding="utf-8"?>
<SESSION>
<DELAY TITLE="starting position">1000</DELAY>
<REPEAT TITLE="starting position">60</REPEAT>
</SESSION>

my output

1
<SESSION>
  <DELAY TITLE="starting position">1000</DELAY>
  <REPEAT TITLE="starting position">60</REPEAT>
</SESSION>
1000
60
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

your problem is that you're calling myLoader.load() and adding an event listener to wait for the xml to finish loading, but you're then immediately setting var delay:uint = 0; and var repeat:uint = 0; and starting the timer. you can see this in your results by the 1, XML, 2, 3, 4 output. the load call is asynchronous so it returns immediately. you need to wait until your processXML function is called before proceeding to the next step.

+2  A: 

you need to re-order your code a touch. you're not waiting for the xml to finish loading before you try and start the timer. the easiest way to do this is to put it in the processXML method, like so.

//load xml
var myXML:XML;
var myLoader:URLLoader = new URLLoader();

myLoader.load(new URLRequest("xml.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

var timer:Timer;

//parse XML
function processXML(e:Event):void {
  myXML = new XML(e.target.data);
  trace(myXML);
  //receive values from XML
  var delay:uint = parseInt(myXML.DELAY.text());
  trace(delay);   
  var repeat:uint = parseInt(myXML.REPEAT.text());
  trace(repeat);  

  //timer and event
  timer = new Timer(delay,repeat);
  timer.addEventListener(TimerEvent.TIMER, countdown);
  timer.start();
}

function countdown(event:TimerEvent) {
  myText.text = String(0 + timer.currentCount);
  trace(0 + timer.currentCount);
}
oedo
@oedo 1120: Access of undefined property timer.
VideoDnd
sorry, my bad, fixed now
oedo
+1 for fast response
jonathanasdf
Thanks a bunch.
VideoDnd
no problem. does my comment above make sense?
oedo
...chicken before the egg thing:) Thank for the description. I need to get better at organizing my code. I appreciate your help.
VideoDnd
no problem. async calls take a little extra thought but once you get used to them, as3 will become your friend :)
oedo
be glad AS3 is singlethreaded and thus there aren't many async functions. Actually, I'm not actually sure whether that's something you should be glad for :)
jonathanasdf