Why do Masks hate me?

So, bitmap masks have been the bane of my existence with Corona. I have yet to get them to work successfully. Here is my current sample case:

I have a 1024x768 background image.
I have a mask that is also 1024x768. It is all black except for a wide white square in the center. I have made sure to leave a 4 pixel black border.

I load the image using display.newImage()
I load the mask using graphics.newMask()

then I do img:setMask (mask)

The end result is that the entire image is masked out. What am I doing wrong?
This is my usual outcome. Help please. I have had some many cases where I would love to use a mask but never have any success. [import]uid: 69863 topic_id: 21736 reply_id: 321736[/import]

Minor Update:

It appears that if I add my main object, with mask, to a display group, then the entire object gets masked. If I keep the object separate then the mask works as expected. Why would adding it to a display group mess with the mask properties? Is this a bug? [import]uid: 69863 topic_id: 21736 reply_id: 86334[/import]

I’ve had several problems with masks and wasted many days. I ended up not using masks at all, and just found other ways to do it. Just submit a bug for it. Masks are a very basic part of 2D, so I hope Ansca can comment on when this issue will be resolved. [import]uid: 99244 topic_id: 21736 reply_id: 86404[/import]

I banged my head against this in my early Corona days as well - and got absolutely no responses. I always add all my display objects to groups, so that might explain why the feature just seemed broken to me.

But good luck at figuring this out! It would really be a boon to the community if you did. [import]uid: 65996 topic_id: 21736 reply_id: 86407[/import]

I’v had a lot of problems with the mask myself.
There are a couple of things you’ll have to think of when using masks.

* You have to have atleast 1 pixel black border around the mask, otherwise it will skrew everything up.

* The size of the mask must be in multiples of 4 ( the width and height should be dividable by 4 )

Why these rules exist, i have now idea. Ansca should really fix the mask functionality because right now, it is not user friendly at all.

I’m used to set the width and height of the mask when i’m coding actionscript for example, but with corona you can only set scale.

[import]uid: 103182 topic_id: 21736 reply_id: 86413[/import]

^ Thanks, those tips sound interesting.

I have a recent experience I want to share just because it’s so funny. I’m using a very simple mask of some (not divisible by 4 anyway) dimensions to prevent out of boundary drawing, where there’s a filled white rectangle with some perhaps 10 pixel thick black borders around it.

I’m masking out some text with this that would otherwise spill out of its GUI element. Everything works predictably on simulator. On Android however there’s an arbitrary continuous part of text masked out (in addition to already properly masked areas). It looks like a 8 pixel wide tilted band - coincidentally it looks cool and part of the design, lol. But it’s certainly not intended, and I don’t like the idea of API arbitrarily interpreting my code. :wink:

So bottom line is, I’ll be quite careful to trust masks anytime soon, and will try to do without them. [import]uid: 58849 topic_id: 21736 reply_id: 86414[/import]

In their older build, masks had worked. Even if you get past all the rules of the image, it now runs very sluggishly if you have too many on screen. In my case I had 8 pieces and it was still too slow. Here is an older Ansca example of a puzzle game using masks, it no longer touches right, and moves at a very low fps. So it seems like they had it working long ago and it broke down over time.

http://developer.anscamobile.com/code/puzzleapp [import]uid: 99244 topic_id: 21736 reply_id: 86488[/import]

Also know that there is a .maskX and .maskY property for every object you set a mask to, so you might try to play with that.

I believe the mask’s reference point is directly in the center of the mask, so you might try:

local mask = graphics.newMask( "mask.png" )local object = display.newImage( "image.png" )object:setMask( mask )object.maskX = object.contentWidth * 0.5object.maskY = object.contentHeight * 0.5[/code]See if that works! [import]uid: 52430 topic_id: 21736 reply_id: 86679[/import]

I played with this as well to no avail. I have it working but don’t understand why. As I stated earlier, my success is all dependent upon whether of not I add the the object being masked to a display group. When I comment out the displaygroup:insert () line, the mask works as intended. When I add the object to the display group, the entire object gets masked out.

I am using the Director class, not sure if that is the problem or not. With the Directory class, you create a primary display group for each screen, add all of the screen elements, and pass them to the screen director. Again, if I do not add the masked object to that primary display group, I am fine. If I do, I see nothing due to a complete masking.

Thoughts? [import]uid: 69863 topic_id: 21736 reply_id: 86691[/import]

@Christian.Henne: Have you tried adding to the display group first, and applying the mask after? Or is that what you have been doing already? [import]uid: 52430 topic_id: 21736 reply_id: 86766[/import]

Actually this worked. I usually create most of my objects, manipulate them as needed, then add them to groups to ensure proper layering. Applying the mask after the add worked. Any thoughts as to why this ordering mattered? (Oh, and Thanks!) [import]uid: 69863 topic_id: 21736 reply_id: 86876[/import]

I’m not sure what exactly you mean by the last statement, but it’s making lightbulbs go off in my head. I think you mean this:

bg = display.newImage("background.png")  
bg.x = 40  
bg.y = 50  
group:insert(bg)  
  
myMask = (create your mask with whatever params)  
bg:setMask(myMask)  
myMask.x = 0  
myMask.y = 15  
group:insert(myMask)  
  

produces badness, which is what we all seem to be experiencing. But do you mean that if you call setMask AFTER the group:insert line allows the mask to function properly? That actually could make a bunch of sense - since before the mask is in a group it is “masking” nothing - and thus you just get black. Then you add that black into the display group…

Did I understand you correctly? [import]uid: 65996 topic_id: 21736 reply_id: 86897[/import]

No, sorry. Here is an overview of the differences in my code

-- This sequence is resulting in the bg getting masked completely  
-- Here we add the image to the group after applying the mask  
bg = display.newImage("background.png")  
bg.x = 40  
bg.y = 50  
  
myMask = (create your mask with whatever params)  
bg:setMask(myMask)  
myMask.x = 0  
myMask.y = 15  
group:insert(bg)  
-- This sequence seems to work  
-- Here we add the image to the group and then apply the mask  
bg = display.newImage("background.png")  
bg.x = 40  
bg.y = 50  
group:insert(bg)  
  
myMask = (create your mask with whatever params)  
bg:setMask(myMask)  
myMask.x = 0  
myMask.y = 15  

Notice, as per the suggestion, the “bg” is added to the group BEFORE the mask is applied. This results in a working mask. If I attempt to apply the mask to “bg” and then add “bg” to the group, “bg” gets masked out completely.

I never add the mask to the group as I heard that was incorrect and will result in issues.

[import]uid: 69863 topic_id: 21736 reply_id: 86906[/import]

Thanks for these tips!
[import]uid: 141981 topic_id: 21736 reply_id: 102828[/import]