for those interested:
/* demonstration of Flash DisplayObject masking implications.
*
* reference:
* http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#mask
*/
package
{
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.geom.Matrix;
import flash.events.Event;
public class TestMask extends Sprite
{
private var mMask:Shape;
public function TestMask():void
{
// parent container for the target (rotate it a little)
const cont:Sprite = new Sprite();
cont.rotation = 5;
this.addChild(cont);
// create a fancy rectagle target to be masked
const target:Sprite = new Sprite();
const mtx:Matrix = new Matrix();
mtx.createGradientBox(100, 20, 0, 0, 0);
target.graphics.beginGradientFill(GradientType.LINEAR,
[0xFF0000, 0x0000FF],
[1.0, 1.0],
[0x00, 0xFF],
mtx,
SpreadMethod.REPEAT);
target.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
target.graphics.endFill();
cont.addChild(target);
// create a rectangle mask
mMask = new Shape();
mMask.graphics.beginFill(0x00FF00);
mMask.graphics.drawRect(0, 0, 100, 100);
mMask.graphics.endFill();
/* adding the mask to the DL will apply the parent DOC's transformation to it,
* yet the mask will still mask the target. */
// cont.addChild(mMask); // <-- line of interest
target.mask = mMask;
/* (1) if you skip adding the mask to the DL, it still can be adjusted
* relative to the stage but without the stage transformation... */
this.addEventListener(Event.ENTER_FRAME, enterFrame);
}
private function enterFrame(_e:Event):void
{
/* (2) ...but not repetitively, until it's added to the DL.
* if not added, the bellow hunk will execute only once for Flash CS frame code
* (e.g. in a lambda function) and not a singe time if this is in a
* package class (as is)!
*
*/
mMask.x = Math.random() * this.stage.stageWidth;
mMask.y = Math.random() * this.stage.stageHeight;
}
}
}
@Daniel
I'll have to play around with this a little more, maybe I find a solution that's more flexible. One way I could imagine is to use the mask as child object (as it is now) if it's not connected to the stage; when on the stage, I could use the conventional (i.e. classic Flash) approach.
i'd say this sounds very reasonable.
if not on the DL it will act like .clipRect and apply to the target coordinate space, while if on the DL the transformation matrix of the parent is considered.
if the mask is added to the target (if the target is a DOC) it would be the same as not adding it to the DL at all (am i correct about this?).
i think the option in Flash to have the mask on the stage without transformation is non-optimal and it has the limitation of not allowing animations as i describe in the above example. Starling can probably avoid that, but i question how many users in Flash use masks without adding them to the DL. if we can assume that Flash users mostly always add masks to the DL, then Starling masks not being on the DL yet acting on the target coordinate space can be a bonus feature.
comments by others?