Mask Releasing

As mask is image and occupy memory, how to release mask created with graphics.newMask function and free memory?

Thanks [import]uid: 9058 topic_id: 17160 reply_id: 317160[/import]

Hey Andriy,

http://developer.anscamobile.com/node/5249

Peach :slight_smile: [import]uid: 52491 topic_id: 17160 reply_id: 64948[/import]

@Peach

Thanks for the link, but I didn’t find there how to release memory occupied by mask (something like removeSelf() for display object)? [import]uid: 9058 topic_id: 17160 reply_id: 65458[/import]

Removing the image will remove the mask. Here’s an example where you can see that. Paste it into the X-ray sample code’s main folder, replacing what is there;

[lua]local function monitorMem(event)
collectgarbage(“collect”)

print( “\nMemUsage: " … (collectgarbage(“count”)/1000) … " MB”)
print("Texture Usage " … system.getInfo( “textureMemoryUsed” ) / 1000000)

return true
end

Runtime:addEventListener(“enterFrame”, monitorMem)

display.setStatusBar( display.HiddenStatusBar )

local halfW = display.contentCenterX
local halfH = display.contentCenterY

– Background image
local bkg = display.newImage( “paper_bkg.png”, true )
bkg.x = halfW
bkg.y = halfH

– Image that the x-ray reveals. In display object rendering,
– this is above the background. The mask effect makes it look
– underneath
local function addMask ()
local image = display.newImage( “grid.png”, true )
image.alpha = 0.7

– The mask that creates the X-ray effect.
local mask = graphics.newMask( “circlemask.png” )
image:setMask( mask )

function onTouch( event )
local t = event.target

local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )

– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true

– Store initial position
t.x0 = event.x - t.maskX
t.y0 = event.y - t.maskY
elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.maskX = event.x - t.x0
t.maskY = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end

return true
end
local function killMask()
image:removeSelf()
end
image:addEventListener( “touch”, onTouch )
timer.performWithDelay(10000,killMask,1)
end

– Display instructions
local labelFont = “Zapfino”
local myLabel = display.newText( “Move circle to see behind paper”, 0, 0, labelFont, 34 )
myLabel:setTextColor( 255, 255, 255, 180 )
myLabel.x = display.contentWidth/2
myLabel.y = 200
timer.performWithDelay(5000,addMask,1)[/lua]

Then watch the terminal :slight_smile:

It’s very cobbled together but it will show the memory side of things clearly.

Peach [import]uid: 52491 topic_id: 17160 reply_id: 65591[/import]

@peach pellen
OK, thanks! [import]uid: 9058 topic_id: 17160 reply_id: 66244[/import]

No worries :slight_smile: [import]uid: 52491 topic_id: 17160 reply_id: 66387[/import]