Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- import fl.controls.TextArea;
- import flash.display.*;
- import flash.text.*;
- import flash.xml.*;
- import flash.events.*;
- import flash.net.URLLoader;
- import flash.net.URLRequest;
- import flash.utils.*;
- public class WeatherService extends MovieClip {
- public var xml_Weather:XML = null;
- public var str_Location:String = null;
- public var str_Temparature:String = null;
- public var num_Temparature:Number = 0;
- public var str_ImageURL:String = null;
- public var str_ImageName:String = null;
- public var ta_TextArea:TextArea = new TextArea();
- public var ldr_Loader:URLLoader = new URLLoader();
- public var req_URLRequest:URLRequest = new URLRequest();
- public function WeatherService ():void {
- init();
- var myTimer:Timer = new Timer (600000);
- myTimer.addEventListener(TimerEvent.TIMER, callTime);
- myTimer.start();
- }
- public function init():void {
- trace ("init got called");
- req_URLRequest = new URLRequest("http://www.weather.gov/xml/current_obs/KAUW.xml");
- ldr_Loader.addEventListener(Event.COMPLETE, f_XMLParser);
- ldr_Loader.load(req_URLRequest);
- }
- public function callTime (event:TimerEvent):void {
- init();
- }
- public function f_XMLParser(event:Event):void {
- if (ldr_Loader != null)
- {
- xml_Weather = new XML(event.target.data);
- str_Location = xml_Weather.location;
- str_Temparature = xml_Weather.temperature_string;
- var firstChar_str:String = str_Temparature.charAt(0);
- var secondChar_str:String = str_Temparature.charAt(1);
- trace(firstChar_str);
- trace(secondChar_str);
- var tempString:String = (firstChar_str + secondChar_str);
- trace ("this is it " + tempString);
- var myNumber:Number = Number(tempString);
- trace (myNumber);
- moveRed(myNumber);
- trace ("this is " + str_Temparature);
- str_ImageURL = xml_Weather.icon_url_base;
- str_ImageName = xml_Weather.icon_url_name;
- trace (str_ImageURL);
- trace (str_ImageName);
- mcHolder.therm_mc.temp_txt.text = str_Temparature.toString();
- mcHolder.therm_mc.location_txt.text = str_Location.toString();
- var i =new Loader();
- i.load(new URLRequest(str_ImageURL.concat(str_ImageName)));
- i.x = 77;
- i.y = 82;
- mcHolder.addChild(i)
- if (myNumber < 80) {
- trace ("it is cold");
- }
- if (myNumber >= 50 && myNumber <= 70) //if true, calls the function to pass data to Flex
- {
- tempAlert();
- }
- }
- else {
- trace("Text not parseable into XML");
- }
- }
- public function moveRed(tempNum:Number):void{
- if (tempNum < 0){
- mcHolder.therm_mc.red_mc.y = 420;
- }
- else if(tempNum >=0 && tempNum<10){
- mcHolder.therm_mc.red_mc.y = 407
- }
- else if(tempNum >=10 && tempNum<20){
- mcHolder.therm_mc.red_mc.y = 367
- }
- else if(tempNum >=20 && tempNum<30){
- mcHolder.therm_mc.red_mc.y = 327
- }
- else if(tempNum >=30 && tempNum<40){
- mcHolder.therm_mc.red_mc.y = 287
- }
- else if(tempNum >=40 && tempNum<50){
- mcHolder.therm_mc.red_mc.y = 247
- }
- else if(tempNum >=50 && tempNum<60){
- mcHolder.therm_mc.red_mc.y = 207
- }
- else if(tempNum >=60 && tempNum<70){
- mcHolder.therm_mc.red_mc.y = 167
- }
- else if(tempNum >=70 && tempNum<80){
- mcHolder.therm_mc.red_mc.y = 127
- }
- else if(tempNum >=80 && tempNum<90){
- mcHolder.therm_mc.red_mc.y = 87
- }
- else if(tempNum >=90 && tempNum<100){
- mcHolder.therm_mc.red_mc.y = 47
- }
- else if(tempNum > 100){
- mcHolder.therm_mc.red_mc.y = 27
- }
- else{
- trace("tempNum is not a valid temperature");
- }
- }
- public function tempAlert():void //dispatches a new Event named tempAlert that the Flex app looks for
- {
- dispatchEvent(new Event("tempAlert", true));
- trace("data passed")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement