Posts Tagged ‘ snippet

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.

Printing multi line XML while maintaining the formatting in PHP

A co-worker of mine introduced me to this tip for maintaining the formatting of multi line information in PHP; in this case it is XML. It uses heredoc. The syntax is basically three <<< and then the name of the type of variable...

Here is a code snippet of it in use:

<?php
 
echo <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<someNode>
	{$massiveAmountOfXML}
</someNode>
XML;
 
?>

HTTP Post in PHP without a Form

For one reason or another I had trouble getting this to work so I thought I would post my code snippet that I used here.

<?php
<?php
ini_set('display_errors', E_ALL);
 
class PostWithOutAForm {
 
	var $_returned_message;
	var $data = array();
 
	public function __construct() {	}
 
	public function callPong($msg) {
		$data['message'] = $msg;
 
		$post_str = '';
		foreach($data as $key=>$val) {
			$post_str .= $key.'='.urlencode($val).'&';
		}
		$post_str = substr($post_str, 0, -1);
 
		// Initialize cURL
		$ch = curl_init('http://localhost:8888/TargetOfPost.php');
		curl_setopt($ch, CURLOPT_POST,				count($data));
		curl_setopt($ch, CURLOPT_POSTFIELDS, 		$post_str);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 	TRUE);
 
		// Execute...
		$output = curl_exec($ch);
 
		 if (!$output) {
			$output = curl_error($ch);
		}
		curl_close($ch);
 
		// Display the result
		return $output;
	}
}
?>

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);
}

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";

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>");

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 – 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