Tag Archives: actionscript

Yahoo Weather RSS

Actionscript:
  1. var temp:Number;
  2. var weatherData:XML;
  3. var yweather:Namespace;
  4.  
  5. var zipcode:String="11211";
  6. var units:String = "f";  // "f" or "c" for Fahrenheit or Celsius
  7.  
  8. var yahooURL:String = "http://weather.yahooapis.com/forecastrss?p=" + zipcode + "&u=" + units;
  9.  
  10. var yahooWeather:URLLoader = new URLLoader();
  11.  
  12. yahooWeather.load(new URLRequest(yahooURL));
  13. yahooWeather.addEventListener(Event.COMPLETE, onLoaded);
  14.  
  15. function onLoaded(evt:Event):void {
  16.  
  17.     weatherData=new XML(evt.target.data);
  18.     yweather = weatherData.namespace("yweather");
  19.    
  20.     temp = weatherData.channel.item.yweather::condition.@temp;
  21.      
  22.     trace(temp);
  23. }

This snippet comes from a student question about how to get the temperature from yahoo weather.

You could use this snippet to get lots of additional information based on a zipcode... like latitude, longitude, humidity, wind chill etc... The trickiest part of this code is the Namespace stuff... which I find generally annoying... probably just because I'm not used to it.

You can read some detailed info about the content contained in the RSS file here.

Posted in XML, external data | Also tagged , , , , , | Leave a comment

2D Vector

Actionscript:
  1. var map:Vector.<Vector.<int>> = new Vector.<Vector.<int>>();
  2. map[0] = new Vector.<int>();
  3. map[0].push(1);
  4. map[0].push(0);
  5. map[0].push(0);
  6.  
  7. map[1] = new Vector.<int>();
  8. map[1].push(0);
  9. map[1].push(1);
  10. map[1].push(0);
  11.  
  12. map[2] = new Vector.<int>();
  13. map[2].push(0);
  14. map[2].push(0);
  15. map[2].push(1);
  16.  
  17. /*
  18. map:
  19. 1,0,0
  20. 0,1,0
  21. 0,0,1
  22. */

This creates a two dimensional Vector of ints (flash 10 player only). The first line is the real snippet... took me a few minutes to get the syntax right the first time I needed a 2D Vector.

Posted in Vector, arrays | Also tagged | Leave a comment

Draw Spiral

Actionscript:
  1. stage.frameRate = 30;
  2. var xp:Number = 0;
  3. var yp:Number = 0;
  4. var counter:Number = 0;
  5. graphics.lineStyle(0,0x999999);
  6. graphics.moveTo(200,200);
  7. addEventListener(Event.ENTER_FRAME, onLoop);
  8. function onLoop(evt:Event):void {
  9.     for (var i:int = 0; i <20; i++) {
  10.         counter += .05;
  11.         xp=200 + counter * Math.cos(counter);
  12.         yp=200 + counter * Math.sin(counter);
  13.         graphics.lineTo(xp, yp);
  14.     }
  15.     if (counter> 400) {
  16.         removeEventListener(Event.ENTER_FRAME, onLoop);
  17.     }
  18. }

Draws a spiral over time.

Posted in Graphics, motion | Also tagged , | Leave a comment

Object.prototype Crap

Actionscript:
  1. Object.prototype.myVar = "I am a variable";
  2.  
  3. var s:Sprite = new Sprite();
  4.  
  5. trace(Object(s).myVar);
  6.  
  7. var v:Video = new Video();
  8. trace(Object(v).myVar);

This.... should not be done... and setting myVar like this:

Actionscript:
  1. Object(s).myVar = "hello";

Will cause an error....

I keep a folder on my laptop for this website..... when I have a random snippet idea I put it in this folder.... Every couple of weeks I go through this folder and turn select snippets into posts.... when I rediscovered this snippet today it really made me laugh....

Posted in Object, dynamic | Also tagged , | Leave a comment