Gamua / Starling-Framework

The Cross Platform Game Engine
http://www.starling-framework.org
Other
2.85k stars 819 forks source link

Applying mask post scale has unexpected results #899

Closed B-Interactive closed 8 years ago

B-Interactive commented 8 years ago

Hi Daniel. When applying a mask in this order (scale, then mask) I encounter an issue where the mask is too small:

image.scale = 0.5; image.mask = new Quad(image.width, image.height, 0x000000);

However, if I apply it in this order (mask, then scale), it's ok:

image.mask = new Quad(image.width, image.height, 0x000000); image.scale = 0.5;

PrimaryFeather commented 8 years ago

That works as expected, but it's easy to get confused with that.

If a mask doesn't have a parent, it is always placed in that object's local coordinate system. Think of it as if the masked object (image) was a sprite, and the mask was a child of that sprite. When you call image.width, you get the size of the image in the parent's coordinate system.

In your sample, the mask in case (1) is only half the size of the mask in case (2). They are both placed in the same coordinate system (the local coordinate system of image). In the image's local coordinate system, the image always takes up the same size (namely, texture.width x texture.height), though.

Another solution would be to add mask and image to the same parent:

var image:Image = ...;
var mask:Quad = new Quad(image.width, image.height, 0x0);

addChild(image);
addChild(mask);

That way, they are in the same coordinate system, and the confusion might go away. :wink:

... Does that explain it?

B-Interactive commented 8 years ago

Thanks Daniel :)

PrimaryFeather commented 8 years ago

You're welcome! :smile: