simple class failure

I try to do a quite simple class object with no success.

I have this error :

attempt to index local ‘ball’ (a userdata value)

when clicking on ball

My main.lua :

local util = "utilities" local myBall = util.doBall()

My utilities.lua is :

local M = {} -- M.somevars are OK -- M.somefunctions are OK M.doBall = function ( ) local ball = display.newCircle ( 0, 0, 100 ) local function mask:tap( event )     self:removeEventListener( 'tap' )     self:removeSelf() end ball:addEventListener("tap") return ball end return M

Why do you have “mask:” in this function definition?

If you are doing an object listener, you would define it like:

function ball:tap( event )

    …

end

ball:addEventListener(“tap”, ball)

Or you can do a function listener:

local function tapListener( event )

    …

end

ball:addEventListener(“tap”, tapListener )

Rob

Actually my initial code was :

M.newMask = function ( ) -- some code local mask = graphics.newMask( "tmp.jpg", system.TemporaryDirectory )     -- ta da!     -- add auto remove on tap     local function mask:tap( event ) -- tried with and without local     timer.performWithDelay(1, function() -- tried with and without the timer     self:removeEventListener( 'tap' )     self:removeSelf() end ) end mask:addEventListener('tap',mask) end

I had tried :

function mask( event )

end

mask:addEventListener(“tap”, mask)

 

with the same attempt to index local ‘ball’ (a userdata value)…

The error message should have a line number that’s generating it as well as a full stack trace.  Can you provide that and the line of code it’s referring to?

In main.lua I do

pochoir:setMask( util.newMask(  ) )

The error refers to the line :

function mask:tap( event )

Okay thinks are starting to become a little more clear.   A “Mask” is not a display object.  It doesn’t have the methods and properties that come with display.* API calls.  I’m assuming the mask is a userdata object and not a table object like display.* things are.  That means you can’t add a listener to a mask.  Masks are applied to display objects like display.newImageRect(), display.newCircle() etc. 

local someImage = display.newImage(“someimage.png”)

someImage.mask = graphics.newMask( “tmp.jpg”, system.TemporaryDirectory )

BTW:  your mask should probably be a PNG since JPEG’s tend to alter the color values as part of its compression.  Masks have to be black or white pixels (though I guess gray might work). 

But in your second example you were building ball as a display object.  You would put the tap handler on the ball.

Rob

A “Mask” is not a display object.

Sorry I missed that … and thx for your answers.

Why do you have “mask:” in this function definition?

If you are doing an object listener, you would define it like:

function ball:tap( event )

    …

end

ball:addEventListener(“tap”, ball)

Or you can do a function listener:

local function tapListener( event )

    …

end

ball:addEventListener(“tap”, tapListener )

Rob

Actually my initial code was :

M.newMask = function ( ) -- some code local mask = graphics.newMask( "tmp.jpg", system.TemporaryDirectory )     -- ta da!     -- add auto remove on tap     local function mask:tap( event ) -- tried with and without local     timer.performWithDelay(1, function() -- tried with and without the timer     self:removeEventListener( 'tap' )     self:removeSelf() end ) end mask:addEventListener('tap',mask) end

I had tried :

function mask( event )

end

mask:addEventListener(“tap”, mask)

 

with the same attempt to index local ‘ball’ (a userdata value)…

The error message should have a line number that’s generating it as well as a full stack trace.  Can you provide that and the line of code it’s referring to?

In main.lua I do

pochoir:setMask( util.newMask(  ) )

The error refers to the line :

function mask:tap( event )

Okay thinks are starting to become a little more clear.   A “Mask” is not a display object.  It doesn’t have the methods and properties that come with display.* API calls.  I’m assuming the mask is a userdata object and not a table object like display.* things are.  That means you can’t add a listener to a mask.  Masks are applied to display objects like display.newImageRect(), display.newCircle() etc. 

local someImage = display.newImage(“someimage.png”)

someImage.mask = graphics.newMask( “tmp.jpg”, system.TemporaryDirectory )

BTW:  your mask should probably be a PNG since JPEG’s tend to alter the color values as part of its compression.  Masks have to be black or white pixels (though I guess gray might work). 

But in your second example you were building ball as a display object.  You would put the tap handler on the ball.

Rob

A “Mask” is not a display object.

Sorry I missed that … and thx for your answers.