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.