Posts Tagged ‘ as3

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.

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

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
  • Drop Down Menu with AS3

    Here is a code snippet that I have been using to create dropdown menus from movieclips in actionscript 3. I probably should try to wrap this up in a class as I bust out this snippet whenever I need this functionality. Usually when I have a ‘button’ that I need to have open up a larger movieclip with selectable/dynamic information in it. Would be nice to set a moviclip to be the ‘menu trigger’ (in the example: _sMouseOver) and another to be ‘the target’ (in the example: _sMenu) in a class… Maybe I will work on a class implementation later.

      AS3 Mouse Over Menu (15.4 KiB, 731 hits)

    Code Snippet after the jump: Read more

    AS3 – Snow Effect

    The best snow script that I have seen thus far. You get great performance even with 1500 different snowflakes in the movie and practically everything is customizable. I will definitely be using this in projects that require a snow affect.

    What you can customize:

  • side the snow blows in from
  • number of flakes
  • size of flakes
  • distance from camera (range)
  • snow alpha
  • wind force
  • time between wind force changes
  • gravity (b/c who doesnt want 0 gravity snow)
  • Check it out: Tim Soret – Snow

    HTTPService and ArrayCollection in Flash CS4

    Recently I had a need to use the HTTPService & ArrayCollection Flex classes from inside Flash CS4. In order to do this you need to point to the Flex classes in your Flash preferences as outlined in this post. Code after the jump. Read more

    Setup WAMP and the Zend Framework for use with Flash and Flex

    There are a few tutorials out there on the net about using Zend with Flash/Flex but most seem to be overly complicated and many don’t work even after following the instructions. (I am hoping that mine will. :) ) The next few articles cover a VERY simple implementation that you can build on for use with future projects and I hope it can save people time when it comes to beginning to explore this new method. This post will deal with setting up your environment to be able to use Zend with Flash/Flex.

    Code and instructions after the jump. Read more

    AS3 – Load XML Document Class Example

    One thing that I do quite often in Flash is load XML. In order to save time, I separated a lot of that code out into external class files. I also created a snippet of code that calls those files in order to save time when I want to use them. (You will need Lee Brimlow’s snippets panel for this to work.)

    The LoadXML and LoadURL classes are probably very similar to those found at Learning Actionscript 3 as that is where I learned a lot about this topic previously. Hope you find this helpful!

      Load XML files (774.5 KiB, 471 hits)

    Code snippet (for the panel) after the break…

    Read more

     

    Switch to our mobile site