简体   繁体   中英

How to use a mask in actionscript 3.0?

I want to mask the png image pattern.png with another image - mask.png, but it doesn't work at all and I can't find the reason. Instead of masking the image, the mask just disappears and the pattern stays the same as it was.

I tried making a MovieClip, drawing eg a circle and using that as the mask instead of mask.png and it works just fine. Is it because you can't use loader objects as masks? How do I make it work?

edit: After changing the size of mask.png to be smaller than the pattern, I've realized that it actually does kind of work, but what happens is instead of cutting the pattern into the shape I've drawn in the png file it just cuts it into the shape of the entire file, as in, it counts the rectangular transparent background as well. How can I make it cut out just the shape?

var mask:Loader = new Loader();
mask.load(new URLRequest("mask.png"));
var pattern:Loader = new Loader();
pattern.load(new URLRequest("pattern.png"));

pattern.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

function loadComplete(e:Event):void {
addChild(pattern);
addChild(mask);
pattern.mask = mask;
}

Your code is looks correctly. The mask property of DisplayObject wants DisplayObject too. But try to make few things, to find the trouble:

  1. You have only one listener, to pattern.png. But you must be sure, that mask.png has loaded already too.
  2. Despite that Loader is DisplayObject too - try to get content from loader for mask, and just after that try to use it.

*Ah, and one more thing. You must at first add listener, and only later try to load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM