Problem with Masks and setFillColor

I’m working on a project where I have a series of symbols/drawings that are white on transparency. When they are stacked on top of each other (think of Layers) they form another image.

I want the player to be able to tap any of the smaller images and when they do, I want to change the color to show it’s been tapped.

It was recommended that I use masks to do this and I’ve had partial success. Once I got my masks on a 4 byte boundary things started somewhat working.

But here are my problems.

If I apply the mask and setup a touch handler on the layer, the event goes to that layer, but as the mask is applied, :setFillColor does not work.

So I got the bright idea to remove the mask, set the Fill color, then re-apply the mask. When I do this the entire image disappears. In other words, I see the background.

I decided to try (well it was more of a discovery that I was actually not setting the mask back afterwards) not re-set the mask. The :setFillColor works as expected, but now that layer is absorbing all of my touch events since the mask isn’t there.

Is this buggy behavior? Is this what’s to be expected? Here’s my code:

local n = 2  
  
local pics = {}  
local mask = {}  
bg = display.newRect(0,0,1024,768)  
bg:setFillColor(64,256,64)  
bg.x = display.contentWidth / 2  
bg.y = display.contentHeight / 2  
  
local function fred(event)  
 print(event.phase,event.target.filename,event.target.index)  
 if event.phase == "ended" then  
 event.target:setMask(nil) -- if I don't do this, I don't get a fill  
 event.target:setFillColor(255,0,0)  
 print(event.target.mask) -- if I do this, the image disappears  
 -- if I don't and I remove the mask above  
 -- my taps get taken by the top layer.  
 event.target:setMask(event.target.mask)  
 end  
 return true  
end  
  
for i = 1, n do  
 pics[i] = display.newImageRect(string.format("image%02d.png",i), 984, 652)  
 pics[i].x = display.contentWidth / 2  
 pics[i].y = display.contentHeight / 2  
 pics[i].filename = string.format("image%02d.png",i)  
 pics[i].index = i  
  
 mask[i] = graphics.newMask(string.format("image%02d\_mask.png",i))  
 pics[i]:setMask(mask[i])  
 pics[i].mask = mask[i]  
  
 pics[i]:addEventListener("touch",fred)  
end  

anyone? [import]uid: 19626 topic_id: 24555 reply_id: 324555[/import]

Any thoughts on this? Should I just go ahead and file a bug report? [import]uid: 19626 topic_id: 24555 reply_id: 99513[/import]

Hey Rob,

I’m not an expert with masks but to me this does sound like a bug, would you please file?

Thanks,
Peach :slight_smile: [import]uid: 52491 topic_id: 24555 reply_id: 99731[/import]

I filed a bug report yesterday, but I must have deleted the message and I can’t find the number now. So I figured I would hop over to the bug site and get it, only to find that the bug system seems to have changed in that its now showing logs of bugs and not just the ones I reported.

EDIT:
Found it

#13478 (13478_9456hdi7ct0v8qlv)

[import]uid: 19626 topic_id: 24555 reply_id: 99773[/import]

Thank you Rob, most appreciated. :slight_smile: [import]uid: 52491 topic_id: 24555 reply_id: 100036[/import]

Some things to try:

After doing setFillColor, translate your object (1,1) and then (-1,-1) to force a positioning refresh.

If you must remove and reapply the mask, try rotating the mask by 360 after reapplying.

This may be similar to a bug I reported, 13340.

Also, if the original problem is to do with events in the masked area of an object, have you looked into the object.isHitTestedMasked property? [import]uid: 44647 topic_id: 24555 reply_id: 101640[/import]

Thanks for the suggestions @toby2. I normally do a .alpha = 0.9 then do an alpha = 1.0 to make the page refresh, so I’m not thinking it’s that bug.

I’ll try the rotation trick at some point. We removed that activity from the app and put another option in. Hopefully it will get solved before we release the next app in this series.

object.isHitTestedMasked I thought was to turn off events from bubbling through transparent areas. [import]uid: 19626 topic_id: 24555 reply_id: 101652[/import]