AS3

Mute / UnMute Sound AS3

Posted in AS3 on January 13th, 2010 by admin – 3 Comments

If you need to mute/unmute a sound in a MovieClips timeline (eg. if you have a player that loads external swfs)
Try using SoundMixer and soundTransform like this:

1
2
3
4
5
6
7
8
import flash.media.SoundTransform;
import flash.media.SoundMixer;
 
// Mute - sets volume to 0
SoundMixer.soundTransform = new SoundTransform(0);
 
//UnMute - sets volume to 1 (100%)
SoundMixer.soundTransform = new SoundTransform(1);

Of course there are other ways to do it but this was exactly what I needed for my project and it worked just fine.
Feel free to comment other ways to do it.

Cheers!

AS3 lightBox class

Posted in AS3 on April 24th, 2009 by admin – 5 Comments

This is not ready but I would like to share it anyways, for two reasons: I couldn’t find a free class (or didn’t look hard enough) and someone else might be looking for it. And also so people can take a look at my code and point something that could be improved.

I will post the final version once it is ready.

LightBox.as

//AS3///////////////////////////////////////////////////////////////////////////
// 
//  FlashInit.com
// 
////////////////////////////////////////////////////////////////////////////////
 
package
{	
	/**
	 *  LightBox Class
	 *    
	 *  @langversion ActionScript 3
	 *  @playerversion Flash 9.0.0
	 *
	 *  @author Pedro Canterini
	 *  @since  22.04.2009
	 */
 
	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
 
	public class LightBox extends Sprite
	{
 
		//---------------------------------------
		// PRIVATE VARIABLES
		//---------------------------------------
		private var _imageHolder:Sprite;
		private var _loader:Loader;
 
		/**
		 *   @constructor
		 */
		public function LightBox(url:String)
		{
			trace("lightbox is on! load: ", url);
			var req:URLRequest = new URLRequest(url);
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, handleProgress, false, 0, true);
			_loader.load(req);
		}
 
		//---------------------------------------
		// PUBLIC METHODS
		//---------------------------------------
		public function alignToCenter(object:DisplayObject):void
		{
			object.x = (object.stage.stageWidth - object.width) / 2;
			object.y = (object.stage.stageHeight - object.height) / 2;
		}
 
		//---------------------------------------
		// PRIVATE METHODS
		//---------------------------------------
		private function handleProgress(event:ProgressEvent):void
		{
			var percent:uint = int((event.bytesLoaded / event.bytesTotal) * 100);
			trace(percent);
		}
 
		private function onComplete(event:Event):void
		{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			handleImage();
		}
 
		private function handleImage():void
		{
			/* create holder for the bitmap */
			_imageHolder = new Sprite();
			addChild(_imageHolder);
			_imageHolder.addChild(_loader);
 
			/* align object */
			alignToCenter(_imageHolder);
		}
	}
}

in order to use it just create a new Instance of it:

var lightBox:LightBox = new LightBox("assets/imgs/retratos/img-001.jpg");
addChild(lightBox);

I am working on the background and a loader and I will post as soon as they are done and integrated.