ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Create a Small Weather App (Actionscript 3,XML)

Updated on January 17, 2014

Screen Shot

Screen Shot
Screen Shot

Flash AS3 Yahoo RSS Feed

Hello All,

It has been sometime since I posted a blog concerning any topic, but I have been busy connecting the dots, so to speak.

What we will be creating is a weather Bug like AS3 App, that drives itself from Yahoo Weather Feeds (XML).

Not difficult, but at the same time I welcome any comments and updates to make this as elegant a solution as possible.

HERE IS THE COMPLETE CODE (pertaining to the tutorial):

//submit button
Submit.addEventListener(MouseEvent.CLICK,getWeather);
function getWeather(e:Event){
//trace(zipCode.text);
var zipText:String = zipCode.text;
XmlClass(zipText);
}
// xml retrieval
function XmlClass(zip:String):void {
//setup listener event
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
//send to the rss feed
xmlLoader.load(new URLRequest("http://weather.yahooapis.com/forecastrss?p=" + zip));
}
//run this function after mouseclick
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
//send to the parse weather function
ParseWeather(xmlData);
}
var mcTimer:Timer = new Timer(0,1)
var i:Boolean = false
//
var timer:Timer = new Timer(0012)
var r:uint = 0
var s:String
//
function ParseWeather(weatherInfo:XML):void {
//setup string vars for text input
var city:String = weatherInfo.channel.title;
var update:String = weatherInfo.channel.item.children()[5].@date;
var temp:String = weatherInfo.channel.item.children()[5].@temp;
var cond:String = weatherInfo.channel.item.children()[5].@text;
var hum:String = weatherInfo.channel.children()[9].@humidity;
var sun:String = weatherInfo.channel.children()[10].@sunrise;
var Sset:String = weatherInfo.channel.children()[10].@sunset;
//extended forcast vars
var dayOne:String = weatherInfo.channel.item.children()[7].@day;
var dayTwo:String = weatherInfo.channel.item.children()[8].@day;
var hiOne:String = weatherInfo.channel.item.children()[7].@high;
var hiTwo:String = weatherInfo.channel.item.children()[8].@high;
var loOne:String = weatherInfo.channel.item.children()[7].@low;
var loTwo:String = weatherInfo.channel.item.children()[8].@low;
//assign to location text box(es)
var locStrip:RegExp = /Yahoo! Weather - /;
locationText.text = city.replace(locStrip, "");
//update box
upDatedOnText.text = "Last Updated On:" + " " + update;
//temparature text
tempText.filters = [glowText]
//tempText.text = temp;
//condtion text
condText.text = cond;
condText.filters = [glowText]
//humidity
humText.text = "Humidity: " + hum + "%";
humText.filters = [glowText]
//sunrise and sunset
SunText.text = "Sunrise: " + sun + "\nSunset: " + Sset;
SunText.filters = [glowText]
//extended forcast
extendForcast.text = "extended\n\n" + dayOne + " " + "hi " + hiOne + " lo " + loOne + "\n" + dayTwo + " " + "hi " + hiTwo + " lo " + loTwo;
extendForcast.filters = [glowText]
//trace(temp);
//setup a function for the thermometer
var newTemp:uint = Number(temp)
var thermSec:uint = newTemp / 30 * 1000
//run a delay on the timer
mcTimer.delay = thermSec
mcTimer.start()
mcTimer.addEventListener(TimerEvent.TIMER, turnOff)
//
timer.addEventListener(TimerEvent.TIMER, testEvt)
timer.start()
function testEvt(e:TimerEvent):void{
s = String(r)
if(r == newTemp){
r = 0
trace("stop")
timer.stop()
timer.removeEventListener(TimerEvent.TIMER, testEvt)
}else{
r++
//trace(s)
tempText.text = s
}
//testo.text = i
}
//
if(i == false){
th.play()
i = true
//trace("zero")
}else if (i == true){
th.gotoAndPlay(2)
//trace("more")
}
}

function turnOff(e:TimerEvent){
th.stop()
mcTimer.stop()
mcTimer.removeEventListener(TimerEvent.TIMER, turnOff)
//trace(mcTimer.running)
}

Don't worry we will break it down function block by block.

1) Submit.addEventListener(MouseEvent.CLICK,getWeather);

This sets up a listener for the click on the Submit Button, on our movie canvas we dropped a button on it and gave it a name "Submit", basically when we click this button it will run the function named getWeather

2)

function getWeather(e:Event){
//trace(zipCode.text);
var zipText:String = zipCode.text;
XmlClass(zipText);
}

Look at the variable we created with the name of zipText it is attached to a text box (input) named "zipText", afterwards we send this value to a function named XmlClass(), that function will load the XML we need to get the values for our display.

3)

// xml retrieval
function XmlClass(zip:String):void {
//setup listener event
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
//send to the rss feed
xmlLoader.load(new URLRequest("http://weather.yahooapis.com/forecastrss?p=" + zip));
}

Look at the parameter for the function, we used the input from the zip text input text box, after that we attached the var to a querystring: http://weather.yahooapis.com/forecastrss?p=" + zip, this will send and load a xml request back into the movie using this function)

LoadXML see the 2nd line calling a listener to load the function (just simply grabbing it and loading it)

4)

function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
//send to the parse weather function
ParseWeather(xmlData);
}

Once loaded we can do our damage, look at the function ParseWeather, we will use this to take apart the XML file to grab the needed info from it.

Inside the ParseWeather function there is all kinds of stuff going on but these lines will give you a glimpse of the grabbing within the XML file.

//setup string vars for text input
var city:String = weatherInfo.channel.title;
var update:String = weatherInfo.channel.item.children()[5].@date;
var temp:String = weatherInfo.channel.item.children()[5].@temp;
var cond:String = weatherInfo.channel.item.children()[5].@text;
...... more code

All we are doing here is taking the node and putting the value into a text box, dynamic, I cannot go into detail about XML but lets take this line:

var city:String = weatherInfo.channel.title;

If you were to look at the raw Yahoo feed, you would notice top level node is called "channel" we assigned this to an XML object we created name weatherInfo, so it reads weatherInfo,channel. If we crawl thru this object the next node is name "title", weatherInfo,channel.title.

This can be confusing, but if you consult the Yahoo, RSS feed page they provide a small primer on XML feeds.

5) Therometer animation

//setup a function for the thermometer
var newTemp:uint = Number(temp)
var thermSec:uint = newTemp / 30 * 1000
//trace(newTemp)
mcTimer.delay = thermSec
mcTimer.start()
mcTimer.addEventListener(TimerEvent.TIMER, turnOff)

There could be a number of ways you could probably do this, for this example I did it this way, pls help other members and contributors on a more elegant solution.

Basically, here is what happened:

Change the newTemp into a number and use that number to run the mercury animation, that number will be used to run a timer, because the animation is of a certain length, the timer will stop at that time and is controlled by another listener.

mcTimer.addEventListener(TimerEvent.TIMER, turnOff)

turnOff

function turnOff(e:TimerEvent){
th.stop()
mcTimer.stop()
mcTimer.removeEventListener(TimerEvent.TIMER, turnOff)
//trace(mcTimer.running)
}

I know that is a lot to go thru just to get the weather, but it gives a lot of insight into what is happening behind the curtain. I will write an article that explains the TIMER class, AS3 variables, text inputs etc, it can be confusing looking at all this.

But any developers that have any experience in those subjects I invite comments and more elegant solutions.

For now I want to show that it can be done, by us regular people, thanks again.


Comments

    0 of 8192 characters used
    Post Comment

    • Alpho011 profile imageAUTHOR

      Alpho011 

      8 years ago from Marietta, Georgia

      Thanks psychicdog.net

    • psychicdog.net profile image

      psychicdog.net 

      8 years ago

      great to see you back Alpho011, your scripts are always great to read. Thanks for the info here. Looks like I've been putting off learning XML for too long.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)