Class works well, but does not appear to me to be the main application file (Application MXML).
Gauge.as
Code:
package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.text.Font;
import flash.text.FontStyle;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.utils.getTimer;
public class Gauge extends Sprite {
private var gauge:Sprite;
private var target:Number;
private var wait:int;
private var time:int;
private var tf:TextField;
private var url:String = "Image_0007.png";
private var loaderGauge:Loader = new Loader();
private var tFormat:TextFormat;
private var timerAtStart:int = 0;
private var t:Timer;
public var isRunning:Boolean = false;
function Gauge():void {
t = new Timer(2000,30);
tFormat = new TextFormat();
tFormat.font = 'Digital-7';
tFormat.size = 16;
tf = new TextField();
tf.x = 200;
tf.text = 'P';
tf.textColor = 0x000000;
tf.setTextFormat(tFormat);
tf.appendText('');
addChild(tf);
loaderGauge.x = stage.stageWidth * 0.5;
loaderGauge.y = stage.stageWidth * 0.3;
loaderGauge.contentLoaderInfo.addEventListener(Event.COMPLETE, startListener);
var request:URLRequest = new URLRequest(url);
loaderGauge.load(request);
addChild(loaderGauge);
target = Math.random() * 180;
wait = int(Math.random() * 62 + 24);
time = 0;
addEventListener(Event.ENTER_FRAME, go);
addEventListener(Event.ENTER_FRAME, update);
addEventListener(TimerEvent.TIMER_COMPLETE, completeHandler);
}
public function go(e:Event):void {
if (time < wait) {
loaderGauge.rotation += (target - loaderGauge.rotation) * 0.05;
time++;
} else {
time = 0;
target = Math.random() * 180;
wait = int(Math.random() * 62 + 24);
}
}
public function update(e:Event):void {
tf.text = (loaderGauge.rotation / 180 * 120).toFixed(2);
}
public function startListener (e:Event):void{
trace("Loading Completed");
var content = e.currentTarget.content;
content.y = -content.height * 0.5;
content.x = -content.width;
}
public function startClock():void {
tickHandler(null);
isRunning = true;
t.reset();
t.start();
}
public function stopClock():void {
t.stop();
isRunning = false;
}
private function tickHandler(e:TimerEvent):void {
var t:Date = new Date();
var h:Number = t.getHours();
var m:Number = t.getMinutes() + h * 60;
var s:Number = t.getSeconds() + m * 60;
var ms:Number = t.getMilliseconds();
}
private function startTimer():void{
timerAtStart = getTimer();
t.start();
}
private function stopTimer():void{
t.stop();
}
private function completeHandler(e:TimerEvent):void {
isRunning = false;
}
}
}
MXML
Code:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import Gauge;
private function Gauge2():void{
var gauge:Gauge = new Gauge();
addChild(gauge);
}
]]>
</fx:Script>
</s:Application>
But flash.display.Sprite cannot be added as a child of a Flex application.
Otherwise:
Code:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" initialize="init();">
<fx:Script>
<![CDATA[
import Gauge;
import flash.display.Sprite;
import spark.core.SpriteVisualElement;
private function init():void{
var gauge:Gauge = new Gauge();
var container:SpriteVisualElement = new SpriteVisualElement();
addElement(container);
container.addChild(gauge);
}
]]>
</fx:Script>
<s:SpriteVisualElement id="container" />
</s:Application>
Where is mistake? Any idea?