Tag Archive for actionscript

Neonsunburst Content Class

package com.neonsunburst {
	
	import flash.display.MovieClip;
	import flash.events.*;
	import com.gskinner.motion.GTween;
	import com.gskinner.motion.easing.*;
	
	public class Content extends MovieClip{
		
		var model:Model;
		
		public function Content (aModel:Model)
		{
			model = aModel;
			model.addEventListener(Event.CHANGE, update);
		}
		public function update(event:Event) : void {
			trace (this + " model updated");
			new GTween(this.slides.getChildAt(model.getCurrentPage() - 1), .5, {alpha:1}, {ease:Sine.easeIn});
			new GTween(this.slides.getChildAt(model.getPreviousPage() -1), .5, {alpha:0}, {ease:Sine.easeIn});
		}
		public function engage() : void {
			new GTween(this.hifiA, .5, {alpha:0}, {ease:Sine.easeIn});
		}
		public function unEngage() : void {
			new GTween(this.hifiA, .5, {alpha:1}, {ease:Sine.easeIn});
		}
	}
}

source

Lissage video FLVPlayback

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

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

CSS In Flash: How to recognize strong tag

var css:TextField.StyleSheet = new TextField.StyleSheet();
css.setStyle("strong", {fontWeight:"bold"});
css.setStyle("somethingElse", {color:"#17829B", fontStyle:"italic"});
myTextField.styleSheet = css;

source

Bring to Front / Send to Back / Bring Forward / Send Backwards (Movieclip Arrange functions for AS3)

MovieClip.prototype.bringToFront = function():void {
	this.parent.setChildIndex(this, this.parent.numChildren-1);
}
MovieClip.prototype.sendToBack = function():void {
	this.parent.setChildIndex(this, 0);
}
MovieClip.prototype.bringForward = function():void{
	var currentDepth = this.parent.getChildIndex(this);
	if(currentDepth<this.parent.numChildren-1){
		this.parent.setChildIndex(this, currentDepth+1); 
	}
}
MovieClip.prototype.sendBackward = function():void{
	var currentDepth = this.parent.getChildIndex(this);
	if(currentDepth>0){
		this.parent.setChildIndex(this, currentDepth-1); 
	}
}
/*
blue.addEventListener(MouseEvent.MOUSE_UP, clicked);
green.addEventListener(MouseEvent.MOUSE_UP, clicked);
orange.addEventListener(MouseEvent.MOUSE_UP, clicked);

function clicked(e:MouseEvent){
	e.target.sendBackward();	
	//you also could say...
	// e.target.sendBackward();
	// e.target.bringForward();
	// e.target.sendToBack();
	// e.target.bringToFront();
}
*/

source

Constant Video Stream in Actionscript 3.0

package {
	//display
	import flash.display.MovieClip;
	
	//media
	import flash.media.SoundTransform;
	import flash.media.Video;
	
	//net
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	//events
	import flash.events.SecurityErrorEvent;
	import flash.events.AsyncErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	
	//utils
	import flash.utils.Timer;
	
	//transitions
	import fl.transitions.Tween;
	import fl.transitions.easing.Strong;
	import fl.transitions.TweenEvent;
	
	public class VideoManager extends MovieClip {
		
		private var _url:String = "video/";
		private var _VideoList:XMLList;
		private var _VideoListLength:Number;
		private var _CurrentVideoPosition:Number = 0;
		private var _isPlaying:Boolean;
		
		private var _NetConnection:NetConnection;
		private var _NetStream:NetStream;
		private var _Video:Video;
		private var _customClient:Object;
		private var _videoDuration:Number;
		
		private var _SoundTransform:SoundTransform;
		
		private var _VideoContainer:MovieClip;
		
		private var _Timer:Timer;
		
		private var _fadeOut:Tween;
		private var _fadeIn:Tween;
		
		public function VideoManager(pVideoContainer:MovieClip):void {
			trace("Video Manager Instantiated");
			_VideoContainer = pVideoContainer;
		}//end of constructor
		
		public function setVideoList(pVideoList:XMLList):void {
			trace("
Set Video List");
			_VideoList = pVideoList;
			trace("Video List: 
" +_VideoList);
			_VideoListLength = _VideoList.length();
			trace("Video List Length: " + _VideoListLength);
		}//end of setVideoList
		
		public function StartVideo(pVideoPos:Number=NaN):void {
			
			trace("
Start Video");
			
			//is the position not a number
			if(isNaN(pVideoPos))
				pVideoPos = 0;
				
			//check to see if video is playing
			if (_isPlaying) {
				//stop video
				
				//the video is no longer playing
				_isPlaying = false;
			}

			//set up SoundTransform for Video
			_SoundTransform = new SoundTransform();
			_SoundTransform.volume = 0;
			
			//set up NetConnection
			_NetConnection = new NetConnection();
			_NetConnection.connect(null);
			
			//set up CustomClient
			_customClient = new Object();
			_customClient.onMetaData = metaDataHandler;
			
			//set up NetStream
			_NetStream = new NetStream(_NetConnection);
			_NetStream.client = _customClient;
			_NetStream.bufferTime = 1;
			_NetStream.receiveVideo(true);
			_NetStream.soundTransform = _SoundTransform;
			
			//create instance of Video
			_Video = new Video(640,360);
			_VideoContainer.addChild(_Video);
			_Video.attachNetStream(_NetStream);

			//Add Listeners
				//========Net Connection=======================
			_NetConnection.addEventListener(NetStatusEvent.NET_STATUS, doNetStatus);
			_NetConnection.addEventListener(IOErrorEvent.IO_ERROR, doIOError);
			_NetConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doSecurityError);
			
				//========Net Stream=======================
			_NetStream.addEventListener(NetStatusEvent.NET_STATUS, doNetStatus);
			_NetStream.addEventListener(IOErrorEvent.IO_ERROR, doIOError);
			_NetStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, doAsyncError);
			
			trace("Play Video: " + _VideoList[pVideoPos].attribute('filename'));
			_NetStream.play(_url+_VideoList[pVideoPos].attribute('filename'));
			
			_fadeIn = new Tween(_Video,"alpha",Strong.easeOut,0,1,3,true);
			_fadeIn.addEventListener(TweenEvent.MOTION_START, fadeInStart);
			_fadeIn.addEventListener(TweenEvent.MOTION_STOP, fadeInStop);
			_fadeIn.start();
		}
		
		//EVENT HANDLERS
		/*
			Example of Security Error
				Not having secruity rights on the server containing the FLV
			
			Solution:
				crossdomain.xml file
		*/
		protected function doSecurityError(evt:SecurityErrorEvent):void {
			trace("AbstractStream.securityError:"+evt.text);
			//do nothing
		}//end of doSecurityError
		/*
			Examples of IOErrors:
				connection drop
				loss of internet connection
				404 error
		*/
		protected function doIOError(evt:IOErrorEvent):void {
			trace("AbstractScreem.ioError:"+evt.text);
			//do nothing
		}//end of doIOError
		/*
			Examples of AsyncError
				usually due to streaming server
		*/
		protected function doAsyncError(evt:AsyncErrorEvent) {
			trace("AbstractScreem.asyncError:"+evt.text);			
			//do nothing
		}//end of doAsyncError
		
		protected function doNetStatus(e:NetStatusEvent):void {
			switch (e.info.code) {
				//NetStream
				case "NetStream.Play.StreamNotFound":
					trace("Stream not found");
					break;
				case "NetStream.Play.Stop":
					trace("Netstream Stopped");
					break;
				case "NetStream.Play.Start":
					trace("Netstream Playing");					 
					break;
				//NetConnection
				case "NetConnection.Connect.Closed":
					trace("NetConnection Closed");
					//dispatchEvent(new Event(VideoStream.STREAM_COMPLETE));
					break;
				case "NetConnection.Connect.Failed":
					trace("NetConnection Failed");
					break;
				case "NetConnection.Connect.Success":
					trace("NetConnection Success");
					break;
				default :
					break;
			}
		}//end of doNetStatus
		
		private function timerDone(e:TimerEvent):void{
			_Timer.removeEventListener(TimerEvent.TIMER, timerDone);
			_fadeOut = new Tween(_Video,"alpha",Strong.easeOut,1,0,3,true);
			_fadeOut.addEventListener(TweenEvent.MOTION_START, fadeOutStart);
			_fadeOut.addEventListener(TweenEvent.MOTION_STOP, fadeOutStop);
			_fadeOut.start();
			trace("Timer Done - Begin Transition Out");
		}
		
		private function fadeInStart(e:TweenEvent):void{
			_fadeIn.removeEventListener(TweenEvent.MOTION_START, fadeInStart);		
		}
		private function fadeInStop(e:TweenEvent):void{
			_fadeIn.removeEventListener(TweenEvent.MOTION_STOP, fadeInStop);		
		}
		
		private function fadeOutStart(e:TweenEvent):void{
			trace("Begin Transition of Next Video In");
			if(_CurrentVideoPosition+1 < _VideoListLength)
				_CurrentVideoPosition++;
			else
				_CurrentVideoPosition = 0;
			
			trace("Current Video POS: " + _CurrentVideoPosition);
			
			StartVideo(_CurrentVideoPosition);
			_fadeOut.removeEventListener(TweenEvent.MOTION_START, fadeOutStart);			
		}
		private function fadeOutStop(e:TweenEvent):void{
			trace("Remove Everything from Previous Video");
			_fadeOut.removeEventListener(TweenEvent.MOTION_STOP, fadeOutStop);
		}
		
		private function metaDataHandler(pObject:Object):void {
			for (var propName:String in pObject) {
				trace(propName + " = " + pObject[propName]);
			}

			if (pObject.duration != null)
				_videoDuration = pObject.duration;
				
				_Timer = new Timer((_videoDuration-2)*1000);
				_Timer.addEventListener(TimerEvent.TIMER, timerDone);
				_Timer.start();
		}
	}
	
}

source

Smooth a loaded Bitmap

var loadedBitmap = Bitmap (yourLoader.content);
loadedBitmap.smoothing = true;

source

Embedding Fonts for Anti Aliasing in Flash AS3

// To use this method you must embed and "Export for AS" the font in the Flash IDE (eg. Flash CS3, Flash CS4).
// You Can do this by clicking the menu on the library panel, then go "New Font"
// Then select "Export for Actionscript, and give your font class name.
// The class name is the name that you will reference on the line directly below this.

var myFont = new MyFontClassName();

var myFormat:TextFormat = new TextFormat();
myFormat.size = 40;
myFormat.font = myFont.fontName;

var myText:TextField = new TextField();
myText.defaultTextFormat = myFormat;
myText.embedFonts = true;
myText.antiAliasType = AntiAliasType.ADVANCED;
myText.autoSize = "left";
myText.text = "Here is some pretty that is anti-aliased!";
addChild(myText);

source

Spotlight Navigation effect using AS3 and Tweenlite

X ,y:startY,alpha:0, onComplete:removeFromDisplayList, onCompleteParams:[spots[clickIndex][i]] });
}
}

function continueTween(obj){
TweenLite.to(obj, 2, {x:clickPoint.x + Math.random() *16-8 ,y:clickPoint.y+ Math.random() *16-8,ease:Sine.easeInOut, onComplete:continueTween, onCompleteParams:[obj] });
}

function removeFromDisplayList(child){
var theParent = child.parent;
theParent.removeChild(child);
}
source

Create a fading circular mask

/*
Steve Ottenad - Nov 20, 2009 - AS3 Circle Drawing/ Clock Wipe Snippet

This Snippet works by drawing and then fading a succession of small triangles to make a circle/polygon.
It relies on TweenMax, a free tweening engine found at <a href="http://blog.greensock.com/tweenmax/." >http://blog.greensock.com/tweenmax/.</a>

Parameters:
centerX        (Number) - The X coordinate of the center of the circle to be drawn.
centerY        (Number) - The Y coordinate of the center of the circle to be drawn.
radius         (Number) - The desired radius of the circle.
increment      (Number) - The amount of degrees you want to add each time a wedge is drawn. If increment = 2, then 180 wedges will be drawn (360/2). If increment = 36, then 10 wedges will be drawn (360/10).
timeIncrement  (Number) - The amount of milliseconds between each wedge being drawn.
Container   (MovieClip) - The containing element.

Sample Usage: drawCircle(centerX, centerY , radius, increment , timeIncrement, container);
*/


function drawCircle(centerX:Number, centerY:Number, radius:Number, increment:Number, timeIncrement:Number, container:MovieClip){

//Define Variables
var radius:Number = radius;
var iniX:Number = centerX;
var iniY:Number = centerY;
var increment:Number = increment;
var totalDegrees:Number = 0;
var degInRad:Number;
var degInRad1:Number;
outline.visible = true;

//Create Timer
var maskTimer:Timer = new Timer(timeIncrement);
maskTimer.addEventListener(TimerEvent.TIMER, drawSegment);
maskTimer.start();

//Start Drawing
container.graphics.moveTo(iniX, iniY);
	function drawSegment(e:TimerEvent){
		if(totalDegrees < 362 && keepLoading){
			//Convert to Radians
			degInRad = totalDegrees * (Math.PI / 180);
			degInRad1 = Number(totalDegrees+increment) * (Math.PI / 180);
			//Find X,Y
			var x1:Number = radius * Math.cos( degInRad ) + iniX;
			var y1:Number = radius * Math.sin( degInRad ) + iniY;
			var x2:Number = radius * Math.cos( degInRad1 ) + iniX;
			var y2:Number = radius * Math.sin( degInRad1 ) + iniY;
	
			var wedge:Sprite = new Sprite();
			wedge.alpha = 0;
			
			//Define Color
			wedge.graphics.beginFill(0x000000);
			wedge.graphics.moveTo(iniX, iniY);
			wedge.graphics.lineTo(x1, y1);
			wedge.graphics.lineTo(x2, y2);
			wedge.graphics.lineTo(iniX, iniY);
			
			//Fade In (Time == 1 in this example
			TweenMax.to (wedge, 1, {alpha:1});
			container.addChild(wedge);
			
			totalDegrees+= increment
			;
		}else{
			maskTimer.stop();
		}
	}
}

source