We help IT Professionals succeed at work.
Get Started

Need help creating a mask using ActionScript 3.0

366 Views
Last Modified: 2013-11-19
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

Comment
Watch Question
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE