Tag Archive for as3

AS3 Load External Text File

package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class Main extends MovieClip
	{
		private var _txtFileToLoad:String = "myTextFile.txt";
		private var _txtLoader:URLLoader;
		private var _loadedTxt:String;
		

		public function Main():void
		{
			loadExternalTxtFile();
		}
		
		private function loadExternalTxtFile():void
		{
			_txtLoader = new URLLoader();
			_txtLoader.addEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.addEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			_txtLoader.load(new URLRequest(_txtFileToLoad));
		}
		
		private function onTxtLoader_COMPLETE(event:Event):void {
			_txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			_loadedTxt = String(event.target.data);
			trace(_loadedTxt);
		}
		
		private function onTxtLoader_IO_ERROR(event:Event):void {
			_txtLoader.removeEventListener(Event.COMPLETE, onTxtLoader_COMPLETE);
			_txtLoader.removeEventListener(IOErrorEvent.IO_ERROR, onTxtLoader_IO_ERROR);
			trace(event.target.text);
		}

	}
}

source

Base 58 encode (used for flic.kr short urls)

/*
 Function used to return flickr short url from a photo id.
 example of usage
 var shortFlickrURL:String = 'www.flic.kr/p/' + Base58Encoder.encode(Number('4725679319') );
/// returns <a href="http://www.flic.kr/p/8cAkPD">www.flic.kr/p/8cAkPD</a>
/// short url for this full url <a href="http://www.flickr.com/photos/ninjaparade/4725679319/" >http://www.flickr.com/photos/ninjaparade/4725679319/</a>


*/

package {

	/**
	 * @author ninjaparade
	 */
	public class Base58Encoder {

		
		public static function encode( num : Number ) : String
		{
		var alphabet : String = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' ;
			var base_count : int = alphabet.length;
			var encode : String = "";
			while(num >= base_count) {
				var div : int = num / base_count;
				var mod : int = (num - base_count * Math.round(div) );
				encode = alphabet.charAt(mod) + encode;
				num = Math.round(div);
			}

			if(num) 
			{
				encode = alphabet.charAt(num) + encode;
			}
			return encode;
		}
	}
}

source

Randomly Colored Elastic Stripes

//Get Traer.Physics for AS3 at <a href="http://blog.sqrtof5.com/?p=53" >http://blog.sqrtof5.com/?p=53</a>
import traer.physics.Attraction;
import traer.physics.Particle;
import traer.physics.ParticleSystem;
import traer.physics.Spring;

//get k-lib Kuler API Library at <a href="http://code.google.com/p/k-lib/" >http://code.google.com/p/k-lib/</a>
import com.adobe.kuler.events.GetResultEvent;
import com.adobe.kuler.params.SearchParams;
import com.adobe.kuler.swatches.swatch.color.Color;
import com.adobe.kuler.KLibService;

import flash.geom.Vector3D;

//
//
//
//
// Get a Kuler API key at <a href="http://kuler.adobe.com/api/" >http://kuler.adobe.com/api/</a>
//
// BE SURE TO ENTER YOUR API KEY HERE FOR THIS TO WORK
//		||
//		||
//		||
//		/

var YOUR_KULER_API_KEY = "";



var s:ParticleSystem = new ParticleSystem(new Vector3D(0, 0, 0), .2);
var waterMolecules = new Array();
var waterParticles = new Array();
var sky = new Array();
var earth = new Array();
var magnet = s.makeParticle(0.12, new Vector3D(0, 0, 0));
var hover=new Molecule();
var points = new Array();
var water = new Sprite();
var lastColor = "000000";
var container = new Sprite();
var stripes = new Array();
var colors = new Array();
var totalWidth = 0;
var swatch;


var klib:KLibService = new KLibService(YOUR_KULER_API_KEY);
klib.addEventListener(GetResultEvent.GET_RESULTS, onResults);
klib.getRecent();


function onResults(e){
	swatch = e.results.swatches[0];
	trace(swatch.colors[ int(Math.random() * swatch.colors.length -1) ]);
	init();
}



function init(){
	magnet.makeFixed();
	magnet.position.y=200;
	var i=0;
	while(totalWidth<stage.stageWidth){

		colors[i] = lastColor;
		while(colors[i]==lastColor){
			colors[i]= swatch.colors[ int(Math.random() * swatch.colors.length -1) ].swatchHexColor;
			trace(colors[i]);
		}
		sky[i]=s.makeParticle(10, new Vector3D(totalWidth, 100, 0)); sky[i].makeFixed();
		waterParticles[i]=s.makeParticle(0.5, new Vector3D(totalWidth, 200, 0));
		earth[i]=s.makeParticle(10, new Vector3D(totalWidth, 300, 0)); earth[i].makeFixed();
		var thisWidth = Math.random()*20 + 5;
		totalWidth += thisWidth;
		stripes[i]=new Sprite();
		addChild(stripes[i]);

		if(i>0){
			s.makeSpring(waterParticles[i-1], waterParticles[i], 0.1 , .05, 0);
			s.makeSpring(sky[i], waterParticles[i], 0.05 , .05, 0);
			s.makeSpring(earth[i], waterParticles[i], 0.05 , .05, 0);
			
			s.makeAttraction(magnet, waterParticles[i], -15000 , 10);
			
		}
		lastColor = colors[i];
		i++
	}
	
	waterParticles[0].makeFixed();
	waterParticles[waterParticles.length-1].makeFixed();
	waterParticles[waterParticles.length-1].position.x=stage.stageWidth;

	stage.addEventListener(Event.ENTER_FRAME,loop);
}

function loop(e){
	s.tick(1);
	magnet.position.x = hover.x = mouseX;

	for(var i in waterParticles){
		stripes[i].graphics.clear();
		stripes[i].graphics.beginFill("0x"+colors[i]);
		if(i<waterParticles.length){
			stripes[i].graphics.moveTo(waterParticles[i].position.x,0);
			stripes[i].graphics.lineTo(waterParticles[i].position.x,stage.stageHeight);
		}
		if(i>0){
			stripes[i].graphics.lineTo(waterParticles[i-1].position.x,stage.stageHeight);
			stripes[i].graphics.lineTo(waterParticles[i-1].position.x,0);			
		}
		stripes[i].graphics.endFill();

	}

}

source

Lissage video FLVPlayback

myFLVPlayer.getVideoPlayer(myFLVPlayer.activeVideoPlayerIndex).smoothing = true;

source

SnapClip, Grab BitmapData Regardless of x, y

private function snapClip (clip:DisplayObject):BitmapData
{
	var bounds:Rectangle = clip.getBounds (clip);
	var bmd:BitmapData = new BitmapData (int (bounds.width + 0.5), int (bounds.height + 0.5), true, 0);
	bmd.draw (clip, new Matrix (1, 0, 0, 1, -bounds.x, -bounds.y));
	return bmd;
}

source

Halftone Effect in Actionscript 3 / Flash

package
{
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.net.*;
	


	public class Halftone extends Sprite
	{

		private var image:Class;
		
		private var loader:URLLoader;
		private var shader:Shader;
		private var filter:ShaderFilter;
		private var im:Bitmap;
		
		public function Halftone():void
		{	
			var myImage = new MyImage(500,333);
			im = new Bitmap(myImage);
			addChild(im);
			loader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.load(new URLRequest("Halftone.pbj")); //Download: <a href="http://www.adamcoulombe.info/lab/as3/halftone/Halftone.pbj" >http://www.adamcoulombe.info/lab/as3/halftone/Halftone.pbj</a>
		}
		
		private function onComplete(e:Event):void
		{
			stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved)

		}
		private function mouseMoved(e):void
		{
			shader = new Shader(loader.data);
			//shader.data.contrast.value = [ 1 ] ;
			shader.data.brightness.value = [ stage.mouseX /stage.stageWidth + 1   ];
			//shader.data.amount.value = [100];
			filter = new ShaderFilter(shader);
			im.filters = [filter];		
		}
	}
}

source

AS3 Stop All Sounds From Playing

import flash.media.SoundMixer;

SoundMixer.stopAll();

source

AS3 Change / set volume on NetStream object (FLV playback)

var nc = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
// change "1" to whatever you need the volume to be changed to
ns.soundTransform = new SoundTransform(1);

source

AS3 Loading and Using an External CSS File

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class CSSFormattingExample extends Sprite {
		var loader:URLLoader;
		var field:TextField;
		var exampleText:String = "<h1>This is a headline</h1>" +
		"<p>This is a line of text. <span class='bluetext'>" +
		"This line of text is colored blue.</span></p>";

		public function CSSFormattingExample():void {
			field = new TextField();
			field.width=300;
			field.autoSize=TextFieldAutoSize.LEFT;
			field.wordWrap=true;
			addChild(field);

			var req:URLRequest=new URLRequest("example.css");

			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onCSSFileLoaded);
			loader.load(req);
		}

		public function onCSSFileLoaded(event:Event):void {
			var sheet:StyleSheet = new StyleSheet();
			sheet.parseCSS(loader.data);
			field.styleSheet=sheet;
			field.htmlText=exampleText;
		}
	}
}


// THE 'example.css' FILE SHOULD LOOK LIKE THIS ...
//p {
//	font-family: Times New Roman, Times, _serif;
//	font-size: 14;
//}
//
//h1 {
//	font-family: Arial, Helvetica, _sans;
//	font-size: 20;
//	font-weight: bold;
//}
//
//.bluetext {
//	color: #0000CC;
//}

source