troubleshooting Question

Need help creating a mask using ActionScript 3.0

Avatar of dyarosh
dyarosh asked on
Web DevelopmentAdobe FlashWeb Development Software
8 Comments1 Solution367 ViewsLast Modified:
I am new to Flash and am trying to do something that I think should be simple.  I am loading pictures from an XML file using AS3.  What I want to do is to put an irregular border around the picture and show only the part of the picture that is within the border.  

When I was testing this I started by putting a picture on the stage on a layer and then putting the border on another layer and setting the layer as a mask.  The picture displayed the way I wanted.

Now I'm loading the picture dynamically onto the stage and want to apply the mask but I can't figure out how to do it.  Any help would be greatly appreciated.

In the code below, picMask is a movieclip on the stage that has the border in it.
import fl.transitions.Tween;
import fl.transitions.easing.*;
 
var imageX:XML;
var imageList:XMLList;
var preload:preloader = new preloader();
var sortArray:Array = new Array();
var zSpacing:Number = 0;
var currentImage:Number;
var currentMaskee:MovieClip;
var currentMask:MovieClip;
 
var picTimer:Timer = new Timer(5000);
picTimer.addEventListener(TimerEvent.TIMER, nextPic);
 
var images_mc:MovieClip = new MovieClip();
images_mc.visible = false;
addChild(images_mc);
var req:URLRequest = new URLRequest("images.xml");
var imageLoader:URLLoader = new URLLoader();
imageLoader.addEventListener(Event.COMPLETE, onComplete);
imageLoader.load(req);
root.transform.perspectiveProjection.projectionCenter = new Point(0,0);
 
function onComplete(e:Event):void
{
	imageX = new XML(imageLoader.data);
	imageList = new XMLList(imageX.image);
	for (var i:uint=0; i < imageList.length(); i++)
	{
		var pic:Picture = new Picture(imageList[i].url);
		pic.x = 0;
		pic.y = 0;
		pic.z = i * zSpacing;
		images_mc.addChild(pic);
		sortArray.push(pic);
	}
	
	sortArray.sortOn("z", Array.NUMERIC | Array.DESCENDING);
	for (var j:uint = 0; j<sortArray.length; j++)
	{
		images_mc.setChildIndex(sortArray[j],j);
	}
	currentImage = sortArray.length - 1;
	preload.x = stage.stageWidth/2 - preload.width/2;
	preload.y = stage.stageHeight/2 - preload.height/2;
	preload.bar_mc.scaleX = 0;
	addChild(preload);
	preload.addEventListener(Event.ENTER_FRAME, preloading);
}
 
function preloading(e:Event):void
{
	var totalPct:Number = 0;
	for (var i:uint = 0; i < sortArray.length; i++)
	{
		totalPct += sortArray[i].pctLoaded;
	}
	var avg:Number = totalPct / sortArray.length;
	preload.bar_mc.scaleX = avg;
	
	if (avg >= 1)
	{
		images_mc.visible = true;
		images_mc.mask(picMask);
		preload.removeEventListener(Event.ENTER_FRAME, preloading);
		removeChild(preload);
	
		// Change pictures every 5 seconds
		picTimer.start();
		
	}
}
 
function maskPicture(e:Event):void
{
	images_mc.mask(picMask);
	
}
 
function nextPic(e:TimerEvent):void
{
	for (var i:uint=0; i<sortArray.length; i++)
	{
		if (i == currentImage)
		{
			sortArray[i].z = (sortArray.length-1)*zSpacing;
			images_mc.setChildIndex(sortArray[i],0);
		}
		else
		{
			var startZ:Number = sortArray[i].z;
			var endZ:Number = startZ - zSpacing;
			new Tween(sortArray[i],"z",Regular.easeOut,startZ,endZ,12,false);
		}
	}
	if (currentImage > 0)
	{
		currentImage--;
	}
	else
	{
		currentImage = sortArray.length - 1;
	}
}
 
function onTick(event:TimerEvent):void 
{
	// displays the tick count so far
    // The target of this event is the Timer instance itself.
    trace("tick " + event.target.currentCount);
}

Open in new window

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros