Archive for the ‘ ACTIONSCRIPT 3.0 ’ Category

AS3 Typewriter Text Effect

I had a need to use a typewriter text effect to populate text in a textfield. This was easily solved with two lines of code using the Greensock SplitTextfield class.

import com.greensock.TweenMax;
import com.greensock.text.SplitTextField;
import com.greensock.easing.*;

var stf:SplitTextField = new SplitTextField(text_tf);
TweenMax.allFrom(stf.textFields, .01, {alpha:0}, .1);

You can still see the text fading in somewhat but I feel that it works well enough for my use.

Conditional Switch Statements in AS3

This is something I wasn’t aware that you could do in a switch/case statement until recently.  A lot of times developers will handle this with nested if/else statements.

For Example:

var myNum:Number = 7;
 
switch (true)
{
  case (myNum < 5):
  trace ("< 5")
break;
 
case (myNum > 5):
  trace ("> 5")
  break;
}

Using switch (true)/switch (false) is pretty cool. You can use it for any sort of true/false comparisons, including comparing strings. The first one to evaluate true will trip the statement.

Use WordPress as a CMS for Flash

Press2Flash (not to be confused with FlashPress) seems to be a very full featured way of using WordPress as a CMS for your Flash site. You can get all your posts/comments etc and present them however you would like. There is ASDoc documentation for it and the example he provides in the download is very simple to use. If you are looking for a CMS for your Flash site, give Press2Flash a look.

Embed XML in AS3

There are two methods that I have found for for doing this. (Essentially the same thing, just depends on your coding style and how you want to keep things organized.)

Method 1:

  Embed XML (8.9 KiB, 135 hits)

package  {
 
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.utils.ByteArray;
 
	public class EmbedXML extends MovieClip {
 
		[Embed(source="my.xml", mimeType="application/octet-stream")] 
		protected const EmbeddedXML:Class;
		private var _xml:XML;
 
		public function EmbedXML():void  {
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}
 
		private function init(e:Event):void {
			//_xml = XML(new EmbeddedXML());
 
			var contentfile:ByteArray = new EmbeddedXML();
			var contentstr:String = contentfile.readUTFBytes( contentfile.length );
			_xml =  new XML( contentstr );
			trace(_xml);
		}
	}
}

Method 2:

  Embed XML Class (8.9 KiB, 103 hits)

//.as file that you embed the xml in
package {
	import flash.utils.ByteArray;
 
	[Embed(source="my.xml",mimeType="application/octet-stream")]
 
	public class MyXML extends ByteArray {
 
		public function MyXML():void {	}
	}
}
//usage:
package  {
 
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.utils.ByteArray;
 
	public class EmbedXMLTwo extends MovieClip {
 
		private var _xml:XML;
 
		public function EmbedXMLTwo():void  {
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}
 
		private function init(e:Event):void {
			//var myxml:MyXML = new MyXML();
			//_xml = XML (myxml);
 
			var contentfile:ByteArray = new MyXML();
			var contentstr:String = contentfile.readUTFBytes( contentfile.length );
			_xml =  new XML( contentstr );
			trace(_xml);
		}	
	}
}

Load XML with LoaderMax

I did have my own set of classes that I use for loading files(images/xml etc) but since I use Greensock for tweening I figured I should use it for loading my assets as well.

//import the classes, u dont need easing for this but you should be using it
import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.*;
 
//above your class constructor function
private var _loaderMax:LoaderMax;
private var _xml:XML;	
 
//in your code where you want to use it
private function loadXML():void {
_loaderMax = new LoaderMax({name:"mainQueue", onComplete:completeHandler, onError:errorHandler});
_loaderMax.append( new XMLLoader("yourFile.xml", {name:"xmlDoc"}) );
_loaderMax.load();
}
 
private function completeHandler(event:LoaderEvent):void {
_xml = LoaderMax.getContent("xmlDoc");
}
 
private function errorHandler(event:LoaderEvent):void {
	trace("error occured with " + event.target + ": " + event.text);
}

Create a png with php and as3

For a project I was on awhile ago, I needed to decorate a character with different items of clothing. When the user was done ‘dressing’ their character, they would press a button and the whole character would shrink down so it would fit as an ornament on a tree. In order to not confuse the issue, I am going to only show the code that enables you to take a movieclip and create a png from it. In order to do this, you will need to include the Flex SDK as a part of your flash build (for the Base64Encoder class) and you need to download as3corelib. To include the Flex SDK, in your Flash preferences add this line: (Mac OSX)

//VERSION will prob be 3.5, 4 etc...
/Applications/Adobe Flash Builder 4/sdks/flex_sdk_VERSION/frameworks/projects/framework/src

Code after the jump…

  CreatePNG (44.4 KiB, 173 hits)

Read more

Cast XMLNode to Boolean – AS3

I was having a bit of difficulty making an xml node that had a value of true/false be cast properly as a Boolean. Turns out a good method is to when you are setting it, test it against a string value of true. The boolean will only be set to true if the string in your XML file is “true”.

1
2
//Example
var myBoolean:Boolean = XML.isTrueFalse == "true";

Avoid Twitter API-IP Call Limit

Twitter limits the amount of calls you can make to its API and from one IP to a few hundred an hour. So that means if you have something on the page that is showing your latest tweets, it will only work for the first few hundred people. We have a really high traffic site and needed a more robust solution.

What we did is create a php file that loads the XML from the twitter user timeline (example) and saves it on our server. We setup a chron job to call it every minute. The Flash movie loads the XML file that our php file creates for us ensuring that we will never go over the limit but always have the most up to date content from Twitter displayed on our website.

If you have any questions or want an example of the source, post in the comments. Running Example.

Resources Used:

  • The LoadXML and LoadURL files from here.
  • The AxisScroller from here.
  • Tweenlite
  • Convert links in text to clickable links in Flash AS3

    I found this on StackOverflow but it took quite a bit of searching to find. Here you go:

    1
    
    title_str = title_str.replace(/((https?|ftp|telnet|file):((\/\/)|(\\\\))+[\w\d:#@%\/;$()~_?\+-=\\\.&]*)/g, "<u><a href='$1'>$1</a></u>");

    Dynamically Add UIScrollbar to Textfield AS3

    This is building off my previous post and adding a scrollbar to the textfield if there are too may tweets to fit in it. You don’t need to load data from a HTTPService but this is helpful for loading data when you don’t know its length ahead of time.

    Read more

     

    Switch to our mobile site