how to delete a display object in a table returned by a function attached to a module

I’m a beginning programmer so excuse this if an easy answer.  I am using a module to create a gamePad.  It is called dpad.lua (code attached below).  I create the gamePad in scene:create using the call 

gamePad = dpad.newDPad( 450, “filename.png”, 0.6, false)

The dpad is showing up in the scene, and it works great.  But the problem is that I cannot remove the dpad after the transition, and so it lingers throughout the rest of my game scenes.

I have tried about a million things but can’t get the thing to go away.  I’ve tried cycling through the table and removing everything.  I’ve tried cycle through the table and inserting any display objects into a sceneGroup.  I’ve tried package.loaded() = nil.

Any thoughts much appreciated!  Here is the module.

>

local M = {}

local padSize

local padImage

local padGroup

local padGraphic

local padButtonUp

local padButtonDown

local padButtonLeft

local padButtonRight

local padButtonUpLeft

local padButtonUpRight

local padButtonDownLeft

local padButtonDownRight

local centerX = display.contentCenterX

local centerY = display.contentCenterY

local originX = display.screenOriginX

local originY = display.screenOriginY

local width = display.actualContentWidth

local height = display.actualContentHeight

– Set movement magnitudes

local movementX = 0

local movementY = 0

– Movement property variables

M.isMoving = false

M.isMovingUp = false

M.isMovingDown = false

M.isMovingLeft = false

M.isMovingRight = false



local function setMovement( x, y )

local updatedMovement = false

– Horizontal movement checks

if movementX ~= x and x ~= nil then

movementX = x

updatedMovement = true

end

– Vertical movement checks

if movementY ~= y and y ~= nil then

movementY = y

updatedMovement = true

end

– Since key events can fire multiple times with the same values,

– just return if nothing is actually changing.

if not updatedMovement then

return

end

– If the player is moving along X - determine direction.

if movementX ~= 0 then

if movementX > 0 then

M.isMovingRight = true

else

M.isMovingLeft = true

end

end

– If the player is moving along Y - determine direction.

if movementY ~= 0 then

if movementY > 0 then

M.isMovingDown = true

else

M.isMovingUp = true

end

end

– Is the player moving or not?

if movementX == 0 and movementY == 0 then

M.isMoving = false

else

M.isMoving = true

end

– If the player is not moving along X - reset left and right.

if movementX == 0 then

M.isMovingLeft = false

M.isMovingRight = false

end

– If the player is not moving along Y - reset up and down.

if movementY == 0 then

M.isMovingUp = false

M.isMovingDown = false

end

end



local function onTouchPad( event )

local phase = event.phase

local targetID = event.target.id

if “began” == phase or “moved” == phase then

if “up” == targetID then

setMovement( 0, -1 )

elseif “down” == targetID then

setMovement( 0, 1 )

elseif “left” == targetID then

setMovement( -1, 0 )

elseif “right” == targetID then

setMovement( 1, 0 )

elseif “up_left” == targetID then

setMovement( -1, -1 )

elseif “up_right” == targetID then

setMovement( 1, -1 )

elseif “down_left” == targetID then

setMovement( -1, 1 )

elseif “down_right” == targetID then

setMovement( 1, 1 )

elseif “padGraphic” == targetID then

setMovement( 0, 0 )

end

elseif “ended” == phase or “cancelled”  == phase then

if “up” == targetID or “down” == targetID then

setMovement( nil, 0 )

elseif “left” == targetID or “right” == targetID then

setMovement( 0, nil )

else

setMovement( 0, 0 )

end

end

return true

end


– Creates one of the invisible virtual D-pad buttons that lays on top 

– the pad graphic.


local function createPadButton( buttonID, offsetX, offsetY )

local btn = display.newRect( padGroup,

                         padGraphic.x + offsetX,

                         padGraphic.y + offsetY,

                         padSize/3,

                         padSize/3 )

btn:addEventListener( “touch”, onTouchPad )

btn.id = buttonID

btn.isVisible = false

btn.isHitTestable = true

return btn

end


– Create a new D-pad or directional control pad.


M.newDPad = function( size, image, alpha, enableDiagonalButtons )

padSize = size

padImage = image

if alpha == nil then

alpha = 1.0

end

if enableDiagonalButtons == nil then

enableDiagonalButtons = true

end

padGroup = display.newGroup()

– Create the visuals for the on-screen D-pad.

padGraphic = display.newImageRect( padGroup, padImage, padSize, padSize )

padGraphic.x = 165

padGraphic.y = 1265

padGraphic.alpha = alpha

padGraphic.id = “padGraphic”

padGraphic:addEventListener( “touch”, onTouchPad )

– Create buttons for handling the D-pad input

padButtonUp = createPadButton( “up”, 0, padSize/-5 )

padButtonDown = createPadButton( “down”, 0, padSize/5 )

padButtonLeft = createPadButton( “left”, padSize/-3, 0 )

padButtonRight = createPadButton( “right”, padSize/3, 0 )

if enableDiagonalButtons then

padButtonUpLeft = createPadButton( “up_left”, padSize/-5, padSize/-5 )

padButtonUpRight = createPadButton( “up_right”, padSize/5, padSize/-5 )

padButtonDownLeft = createPadButton( “down_left”, padSize/-3, padSize/3 )

padButtonDownRight = createPadButton( “down_right”, padSize/3, padSize/3 )

end

return M

end

return M

hi,

if you are using this “gamePad = dpad.newDPad( 450, “filename.png”, 0.6, false)”

maybe try this “local gamePad = dpad.newDPad( 450, “filename.png”, 0.6, false)”

you are making a global object from what i can see

Sorry, I should have specified that I did declare gamePad as a local variable.  Apologies for that.

I had emailed a friend three days ago, and just after posting this he responded with an idea.  He suggested I add a displayGroup as a parameter to pass into the module function, and then when the dpad table was passed back to the scene it would have the appropriate displayGroup attached.  I had never considered that, but I tried it and it worked.  Just like that!  Four days of head banging and one simple idea solved the problem.

Thanks for your input, @anaqim.  Thankfully, I can sleep again and move onto other aspects of this game!

Great you found the solution mate!

I’d say using display groups is almost as important as using tables with corona/lua  :wink:

hi,

if you are using this “gamePad = dpad.newDPad( 450, “filename.png”, 0.6, false)”

maybe try this “local gamePad = dpad.newDPad( 450, “filename.png”, 0.6, false)”

you are making a global object from what i can see

Sorry, I should have specified that I did declare gamePad as a local variable.  Apologies for that.

I had emailed a friend three days ago, and just after posting this he responded with an idea.  He suggested I add a displayGroup as a parameter to pass into the module function, and then when the dpad table was passed back to the scene it would have the appropriate displayGroup attached.  I had never considered that, but I tried it and it worked.  Just like that!  Four days of head banging and one simple idea solved the problem.

Thanks for your input, @anaqim.  Thankfully, I can sleep again and move onto other aspects of this game!

Great you found the solution mate!

I’d say using display groups is almost as important as using tables with corona/lua  :wink: