Modules Code Problem

Hey Guys,

I’m trying to modulize some of my code to keep the clutter out of my GameScene.lua but am having some issues. I suspect I am doing it wrong but was wondering if anyone could shed some guidance on what I’m doing wrong or whether if what I’m trying to achieve is even possible.

OK…

so I have a separate file called powerupFunctions.lua with the following code

local powerUpFunctions = {} powerUpFunctions.powerup\_hourglassTouch = function ( event ) --IF touch has began create another icon and get ready for player to move it if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --'event.target' is the touched object print( "touch began" ) print( event.target.name ) powerup\_hourglassACT = display.newImageRect( "powerup\_hourglassA.png", 40, 46 ) powerup\_hourglassACT.x = event.target.x powerup\_hourglassACT.y = event.target.y powerup\_hourglassACT.alpha = 0.50 -- IF touch has STOPED then remove ICON and if on GAMESCENE then complete POWERUP FUNCTION EG (STOP EXT) elseif ( event.phase == "ended" or event.phase == "cancelled" ) then display.getCurrentStage():setFocus( nil ) --setting focus to 'nil' removes the focus if powerup\_hourglassACT.y \> 100 then powerup\_hourglassACT:removeSelf( ) powerup\_hourglassACT = nil --OBJECT has been moved into GAMESCENE so perform powerup POWERUP FUNCTION EG(STOP ECT) print( "touch ended" ) else powerup\_hourglassACT:removeSelf( ) powerup\_hourglassACT = nil end -- IF MOVING the POWERUP then move it to where the players fingers are. elseif event.phase == "moved" then local x = event.x local y = event.y powerup\_hourglassACT.x, powerup\_hourglassACT.y = x, y -- move object based on calculations above -- IF POWERUP is tapped then show help function or description for that POWERUP elseif not event.phase then print("TAPPED!" ) end return true end return powerUpFunctions

and in my GameScene I am doing the following

 --INSERT POWERUP ICONS --Create HourGlass PowerUp for 50 RAGE powerup\_hourglassA = display.newImageRect( "powerup\_hourglassA.png", 40, 46 ) powerup\_hourglassA.name = "hourglassA" powerup\_hourglassA.y = -40 powerup\_hourglassA.x = 30 gameLayer:insert( powerup\_hourglassA ) powerup\_hourglassA:addEventListener( "touch", powerUpFunctions.powerup\_hourglassTouch() ) powerup\_hourglassA:addEventListener( "tap", powerUpFunctions.powerup\_hourglassTouch() ) powerup\_hourglassDA = display.newImageRect( "powerup\_hourglassDA.png", 40, 46 ) powerup\_hourglassDA.name = "hourglassA" powerup\_hourglassDA.y = -40 powerup\_hourglassDA.x = 30 gameLayer:insert( powerup\_hourglassDA ) powerup\_hourglassDA:addEventListener( "touch", powerUpFunctions.powerup\_hourglassTouch() ) powerup\_hourglassDA:addEventListener( "tap", powerUpFunctions.powerup\_hourglassTouch() )

When I try to run my application I am getting the following error

File: ...AMES/AOM (temp) - GAME/powerUpFunctions.lua Line: 5 Attempt to index local 'event' (a nil value) stack traceback: [C]: ? ...AMES/AOM (temp) - GAME/powerUpFunctions.lua:5: in function 'powerup\_hourglassTouch' ...ames/\_GAMES/AOM (temp) - GAME/gameScene.lua:180: in function 'createGameMenu' ...ames/\_GAMES/AOM (temp) - GAME/gameScene.lua:375: in function \<...ames/\_GAMES/AOM (temp) - GAME/gameScene.lua:365\> ?: in function 'dispatchEvent' ?: in function \<?:1096\> (tail call): ? ?: in function \<?:466\> ?: in function \<?:218\>

I take it this will not work as its not passing the tap or touch event through

Anyone know if this is possible to pass the .event into a modular function?

I believe your issue is quite small:

powerup\_hourglassDA:addEventListener( "touch", powerUpFunctions.powerup\_hourglassTouch() ) powerup\_hourglassDA:addEventListener( "tap", powerUpFunctions.powerup\_hourglassTouch() ) -- you are actually firing the function by calling it with () so it passes a nil event -- e.g powerUpFunctions.powerup\_hourglassTouch() -- convert your code to this: powerup\_hourglassDA:addEventListener( "touch", powerUpFunctions.powerup\_hourglassTouch ) powerup\_hourglassDA:addEventListener( "tap", powerUpFunctions.powerup\_hourglassTouch )

Anyone know if this is possible to pass the .event into a modular function?

I believe your issue is quite small:

powerup\_hourglassDA:addEventListener( "touch", powerUpFunctions.powerup\_hourglassTouch() ) powerup\_hourglassDA:addEventListener( "tap", powerUpFunctions.powerup\_hourglassTouch() ) -- you are actually firing the function by calling it with () so it passes a nil event -- e.g powerUpFunctions.powerup\_hourglassTouch() -- convert your code to this: powerup\_hourglassDA:addEventListener( "touch", powerUpFunctions.powerup\_hourglassTouch ) powerup\_hourglassDA:addEventListener( "tap", powerUpFunctions.powerup\_hourglassTouch )