ACTIONSCRIPT STRAIGHT FROM OUR FACTORY FLOOR TO YOUR MONITOR OF CHOICE


SUGAR PILL FACTORY IMAGE

February 26th, 2010-Friday
SUGAR PILL FACTORY IMAGE

Actionscript 3 + PHP Simple Stock Ticker Class


SUGAR PILL FACTORY IMAGE

SUGAR PILL FACTORY IMAGE

Add a stock ticker to your script with this easy to use class.

This is the code in the Ticker.as class

package com.atJascha{
	import flash.text.*;
	import flash.net.*;
	import flash.display.*;
	import flash.utils.*;
	import flash.events.*;

	public class Ticker extends Sprite {

		private var _updateTimer:Timer;
		private var _dataPath:String;
		private var _stockSymbol:String;
		private var _stockInfo:String;
		private var _parseArray:Array;
		private var _dataField:TextField;

		public function Ticker(dataPath:String, stockSymbol:String) {

			_dataPath=dataPath;
			_stockSymbol=stockSymbol;
			_stockInfo="s="+_stockSymbol+"&f=noghl1p2vs"; // to see what these symbols mean click here

			_updateTimer = new Timer(10000);
			_updateTimer.addEventListener(TimerEvent.TIMER, getQuote);
			_updateTimer.start();

			var e:Event;
			getQuote(e);
			buildStock();

		}
		private function getQuote(e:Event):void {
			var variables:URLVariables = new URLVariables();
			variables.info=_stockInfo;

			var urlRequest:URLRequest=new URLRequest(_dataPath);
			urlRequest.method=URLRequestMethod.POST;
			urlRequest.data=variables;

			var stockLoader:URLLoader = new URLLoader();
			stockLoader.load(urlRequest);
			stockLoader.addEventListener(Event.COMPLETE, addStock);
			stockLoader.addEventListener(IOErrorEvent.IO_ERROR, IOErrorHandler);
		}
		private function IOErrorHandler(e:Event):void {
			getQuote(e);
		}
		private function addStock(e:Event):void {
			var array:Array=e.target.data.split("\n");
			array.pop();
			var parseArray:Array = new Array();
			for (var i:int=0; i<array.length; i++) {
				var s:String=array[i].substr(0,array[i].indexOf("\n")-1);
				var nArray:Array=s.split(",");

				for (var con:int = 0; con<nArray.length; con++) {
					parseArray.push(nArray[con]);
				}
			}
			updateQuote(parseArray);
		}
		private function updateQuote(parseArray:Array):void
		{
			var textFormat:TextFormat = new TextFormat;
			textFormat.size = 20;
			textFormat.font = "Tahoma";

			_dataField.text =  parseArray[0] + "\n";
			_dataField.text += "Open: " + parseArray[1] + "\n";
			_dataField.text += "Day's Low: " + parseArray[2] + "\n";
			_dataField.text += "Day's High: " + parseArray[3] + "\n";
			_dataField.text += "Last Trade Prce: " + parseArray[4] + "\n";
			_dataField.text += "Change: " + parseArray[5] + "\n";
			_dataField.text += "Volume: " + parseArray[6] + "\n";
			_dataField.text += "Symbol: " + parseArray[7] + "\n";
			_dataField.setTextFormat(textFormat);

		}

			/*  STOCK ARRAY ORDER ...
			* [0]n = name
			* [1]o = open
			* [2]g = day low
			* [3]h = day high
			* [4]l1 = last trade price
			* [5]p2 = change in percent
			* [6]v = volume
			* [7]s = symbol
			*
			*/

		private function buildStock():void
		{

			var bg:Sprite = new Sprite();
			bg.graphics.beginFill(0xEEEEEE);
			bg.graphics.drawRect(0,0,300,200);
			bg.graphics.endFill();
			addChild(bg);

			_dataField = new TextField();
			_dataField.multiline = true;
			_dataField.autoSize = TextFieldAutoSize.LEFT;
			bg.addChild(_dataField);
		}
	}
}

You invoke it in your document class with this code:

import com.atJascha.*;
var ticker:Ticker = new Ticker("getDow.php", "GE");  // parameters are path to the php file and then your stock symbol.
addChild(ticker);

The php is even simpler:

<?php
$stock = require("http://download.finance.yahoo.com/d/quotes.csv?". $_POST['info']);
echo $stock;
?>

that’s that!
It could use some formatting and love, but ultimately you get your info.
Download the source code here

SUGAR PILL FACTORY IMAGE SUGAR PILL FACTORY IMAGE
Digg! Digg this submit to reddit Stumble it!


SUGAR PILL FACTORY IMAGE

February 24th, 2010-Wednesday
SUGAR PILL FACTORY IMAGE

Full Circle Exchange Launched!


SUGAR PILL FACTORY IMAGE

SUGAR PILL FACTORY IMAGE

After hours of toil we, along with We Are Sew Creative have finally come to launch Full Circle Exchange. Please show some love and support by visiting the site and sending some feedback our way!  Full Circle Exchange

SUGAR PILL FACTORY IMAGE SUGAR PILL FACTORY IMAGE
Digg! Digg this submit to reddit Stumble it!


SUGAR PILL FACTORY IMAGE

February 23rd, 2010-Tuesday
SUGAR PILL FACTORY IMAGE

Totaly Tuesday


SUGAR PILL FACTORY IMAGE

SUGAR PILL FACTORY IMAGE

My list of helpful links of the week.

Favicon generator.

Prevent memory leaks in your AS3 by removing anonymous event listeners.

Get rid of those ugly url’s with mod_rewrite.

Awesome digital art resource site.

Don’t be afraid of CSS3.

Awesome collection of rad letterheads.

Geeky cool programmatic cloth simulation.

Really really geeky list of work stations. I had to look through the whole thing and then dream about submitting mine one day.

SUGAR PILL FACTORY IMAGE SUGAR PILL FACTORY IMAGE
Digg! Digg this submit to reddit Stumble it!


SUGAR PILL FACTORY IMAGE

February 21st, 2010-Sunday
SUGAR PILL FACTORY IMAGE

CSS Style Variable Height Background Image Actionscript 3 Class


SUGAR PILL FACTORY IMAGE

SUGAR PILL FACTORY IMAGE

This a background with a variable height like you see in a lot of css styled page layouts.  It’s an effective way of presenting information in a fun, clean and organized fashion.  It’s a pretty simple class, but still an hour or two of work, hopefully using it will save you some time.

This can be used anytime you wish to add a background element to variable height content.

First the images I used in this example (can be replaced by any).

The top of the content…

The middle (expanding) part…

And the bottom…

First thing I like to do when I write classes in Actionscript is write some static classes that contain a list of general purpose constants and settings.  This helps me organize so that all of my images, colors, and sizes are in one place and easy to get to later if I feel like making changes.

This is my Image.as class.

package com.atJascha{
  public class Images {
   public static const POST_TOP_IMAGE:String="images/paper_top.png";
   public static const POST_MIDDLE_IMAGE:String="images/paper_middle.png";
   public static const POST_BOTTOM_IMAGE:String="images/paper_bottom.png";
}
}

(Keep in mind the paths have to be local to YOUR compiled .swf file.)

So, now I have my image path constants set.  So long as I import com.atJascha.*; any of these variables are available to me simply by writing:

trace(Images.POST_MIDDLE_IMAGE); // prints "images/paper_bottom.png"

Now for the working class.

package com.atJascha{

 /*
 *  Expanding Background Class
 *  by Jascha Rynek
 *  http://blog.sugarpillfactory.com
 */

 import flash.display.*;
 import flash.net.*;
 import flash.events.*;

 public class PostBackground extends Sprite{

 private var _middleHeight:Number; // Remaining Height
 private var _top:Bitmap; // top image reference
 private var _middle:Bitmap; // middle image reference
 private var _bottom:Bitmap; // bottom image reference

 public function PostBackground(height:int){

 _middleHeight = height; // set overall height

 var topLoader:Loader = new Loader();
 topLoader.load(new URLRequest(Images.POST_TOP_IMAGE)); // load top image
 topLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, addTop);
 }
 private function addTop(e:Event):void
 {
 _top = e.target.content; // set top image reference

 var bottomLoader:Loader = new Loader();
 bottomLoader.load(new URLRequest(Images.POST_BOTTOM_IMAGE));  // load bottom image
 bottomLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, addBottom);
 }
 private function addBottom(e:Event):void
 {
 _bottom = e.target.content; // set bottom image reference
 _middleHeight -= _bottom.height + _top.height; // set remaining height

 if(_middleHeight > 0){ // if remaining height is greater than current pieces, add middle piece
 var middleLoader:Loader = new Loader();
 middleLoader.load(new URLRequest(Images.POST_MIDDLE_IMAGE)); // load middle image
 middleLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, addMiddle);
 }else{ // else add top and bottom
 addChild(_top); // add top to stage
 addChild(_bottom);
 _bottom.y = this.height;// add the bottom image and set y to the bottom of the image.
 }
 }
 private function addMiddle(e:Event):void
 {
 addChild(_top); // add top to stage

 _middle = e.target.content;
 addChild(_middle); // add middle to stage
 _middle.y = _top.height; // set middel y position
 _middleHeight -= _middle.height; // set remaining height

 if(_middleHeight>0){
 var times:Number = Math.ceil(_middleHeight/_middle.height);
 /* if there is still more height, figure out how many times the middle piece
 will fit into the remainder*/
 for(var i:int=0;i<times;i++){ // *s the amount of times the middle fits into the remainder
 var bData:BitmapData = _middle.bitmapData;
 var bMap:Bitmap = new Bitmap(bData);
 addChild(bMap); // add the copied middle image
 bMap.y = this.height; // set the copied middle image to the correct y position
 }
 }
 addChild(_bottom);
 _bottom.y = this.height;// add the bottom image and set y to the bottom of the image.

 }
 }
}

Implementing this class is very simple, you only need to pass one “height” paramater.. (assuming you know your content width should match the width of your background images.)

import com.atJascha.*;

var bg:PostBackground = new PostBackground(this.height);
addChild(bg);

Your results should be something like this…

Download working example of Variable Height Actionscript 3 Class.

SUGAR PILL FACTORY IMAGE SUGAR PILL FACTORY IMAGE
Digg! Digg this submit to reddit Stumble it!


SUGAR PILL FACTORY IMAGE

February 16th, 2010-Tuesday
SUGAR PILL FACTORY IMAGE

Tuesday! (the name of today)


SUGAR PILL FACTORY IMAGE

SUGAR PILL FACTORY IMAGE

Another busy week.  Found a couple great code helping sites.

Great tips on improving your designs.

Hilarious list of comments in code.  BTW if you don’t know about Stackoverflow.com you should!

Awesome clothing collection to support iPad enthusiasts.

If you are a web developer, this is one of the coolest tools you can find. It’s called IE tester and it lets you run from IE 5.5-IE 8 in one browser window.

Ever curious why your how to initialize your public static const without instantiating an object in Actionscript 3?  Kaio gives some pointers.

Great article on one man’s opinion on the future of HTML 5, Flash, Javascript and the like.

If you’re looking for a Javascript, Actionscript or PHP code snippet… this is the place.

This guy is one hell of  a web developer.

Blog font generator.

SUGAR PILL FACTORY IMAGE SUGAR PILL FACTORY IMAGE
Digg! Digg this submit to reddit Stumble it!