<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreation();">
    <mx:Panel width="336" layout="absolute" title="Script" left="10" bottom="10" top="10">
        <mx:TextArea id="txtCmdXML" top="40" bottom="136" left="10" right="10"/>
        <mx:Button x="10" label="Run once" click="runOnce(event);" y="10"/>
        <mx:Button id="btnRunTimer" label="Run with timer" right="10" click="runWithTimer();" width="115" y="10"/>
        <mx:List id="listExamples" height="92" left="10" right="10" bottom="10" dataProvider="{xmlExamples.scen}" labelField="@name"  change="selectExample();"></mx:List>
        <mx:Label x="10" text="Examples:" bottom="110"/>
    </mx:Panel>
    <mx:Canvas backgroundColor="#ffffff" borderColor="#008040" borderStyle="inset" id="mainCanvas" width="514" height="422" horizontalCenter="172" verticalCenter="0">
        <mx:Image x="182" y="146" source="assets/personDown.gif" id="mainPerson"/>
    </mx:Canvas>
    <mx:XML id="xmlExamples" source="examples.xml" /> 
    <mx:Script> 
        <![CDATA[
            import script.XMLScriptEngine;
            import mx.collections.ArrayCollection;
            import flash.events.TimerEvent;
            import flash.utils.Timer;
        
            public var environment: Object = {};
            
            public var running: Boolean = false;
            
            public var alarmTimer:Timer = new Timer(100);

            public function runWithTimer():void {
                if(running) {
                    btnRunTimer.label = "Run with timer";
                    running = false;
                    alarmTimer.stop();
                    alarmTimer.removeEventListener(TimerEvent.TIMER,runOnce);
                } else {
                    btnRunTimer.label = "Stop";
                    running = true;
                    alarmTimer.addEventListener(TimerEvent.TIMER,runOnce);
                    alarmTimer.start();
                }
            }
             
            public function selectExample():void {
                txtCmdXML.text=listExamples.selectedItem.commands;
            }
            
            public function onCreation():void {
                environment.world = mainCanvas;
            }

            // scripting part
            
            public var xmlScript: XMLScriptEngine = XMLScriptEngine.getInstance();
            
            public function runOnce(event:Event):void {
                var cmdXML:XML = XML(txtCmdXML.text);
                xmlScript.runCommands(mainPerson,environment,cmdXML);
            }
        ]]>
    </mx:Script>    
</mx:Application>