How to use ‘touch’ event listener to call a function in a different file.

Hi

I want to do a pinball game that have many levels and for each level I will have a different game settings

I want to make this game using the Compressor lib and I want to split my code on few files

Mydaya.lua file that will hold the game and levels settings

myFunc.lua file that will hold my game functions

levelN.lua files, a file for each level, in each file I will place my scene object, call my functions and listeners

In the game I have a button that on each touch event change the paddle rotation  

I need help to understand how to call a function from the touch event.

This code works when I have it on the same file, I don’t know how and what to transfer with the function call  (btnLeft:addEventListener(‘touch’, myFunc.movePaddle))

****************

**Level1.lua

****************

function gameListeners(action)

                                if(action == ‘add’) then

                                                btnLeft:addEventListener(‘touch’, myFunc.movePaddle)

                                else

                                                btnLeft:removeEventListener(‘touch’, myFunc.movePaddle)

                                end

end

– “scene:create()”

function scene:create( event )

                                local sceneGroup = self.view

– add paddle to the scene

                                paddleLeft = display.newImage(‘paddleLeft.png’)

                                paddleLeft.name = ‘padLeft’

                                paddleLeft.x = _W

                                paddleLeft.y = _H

                                paddleLeft.rotation = 45

                                paddleLeft.anchorX = 0.5

                                paddleLeft.anchorY =  0.8

                                physics.addBody(paddleLeft, ‘static’, {shape = paddleLshape})

                                sceneGroup:insert (paddleLeft);

–add buttons to the scene

                                btnBlue = display.newImage(‘btnBlue.png’)

                                btnBlue.x= _HW/2; btnBlue.y = _HH*25.5/16; --btnBlue.alpha = 0.35

                                btnBlue.name = ‘btnblue’

                                sceneGroup:insert (btnBlue);

                end

– “scene:show()”

function scene:show( event )

   local sceneGroup = self.view

                   local phase = event.phase

                if ( phase == “will” ) then

     

elseif ( phase == “did” ) then

                                                gameListeners (‘add’)

                                end

end

************

**myFunc.lua

************

local myFunc = {}

function movePaddle(e)

                                if(e.phase == ‘began’ and e.target.name == ‘btnleft’ and paddleMove ~=  true) then

                                                paddleleft.rotation =  paddleleft.rotation*-1

                                                elseif(e.phase == ‘began’ and e.target.name == ‘btnRight’ and paddleMove ~=  true) then

                                                paddleRight.rotation =  paddleRight.rotation*-1

                                end

end

func.movePaddle = movePaddle

return  myFunc

Thank you

Guy

Any ideas of how can I use an event listener on one object to control a second object but holding the function on a different lua file? thanks, Guy

Hi @guy.kastoriano,

There are a few different ways to access functions between modules, but one method I prefer (when using a scene management library like Composer) is to set the function to a Composer variable using “composer.setVariable()”, so it’s known to the overall library, then call that function from another module by accessing that variable with “composer.getVariable()”.

Something like this:

[lua]

– gameFunctions.lua

local composer = require( “composer” )

local function movePaddle( paddleObject )

   – your code

   – print( paddleObject )

end

– Assign function to Composer variable for access in other modules

composer.setVariable( “function_movePaddle”, movePaddle )

[/lua]

Then, in another module, you can access it like this:

[lua]

– level1.lua

local composer = require( “composer” )

local function_movePaddle = composer.getVariable( “function_movePaddle” )

– Call the function…

function_movePaddle( paddleObject )

[/lua]

Brent

Thank you Brent for you reply,

composer.setVariable()" seems to be very handy but it did not work in my case, probably I didn’t done it correctly.

Can it work from eventListener?

I finally found a solution in which I took out from the eventListner the function calling and use an intermediate function for that.

*****************Before******************

function gameListeners(action)

                                if(action == ‘add’) then

                                                btnLeft:addEventListener(‘touch’, myFunc.movePaddle)

                                else

                                                btnLeft:removeEventListener(‘touch’, myFunc.movePaddle)

                                end

end

******************After*****************

function gameListeners(action)

                                if(action == ‘add’) then

                                                btnLeft:addEventListener(‘touch’, handleMovePaddle)

                                else

                                                btnLeft:removeEventListener(‘touch’, handleMovePaddle)

                                end

end

function  handleMovePaddle (e)

            myFunc.movePaddle(paddleObject, e.phase, e.target.name)

end

and  in myFunc.lua

function movePaddle(paddleObject, phase, target)

                                if(phase == ‘began’ and target == ‘btnleft’ and paddleMove ~=  true) then

                                                paddleleft.rotation =  paddleleft.rotation*-1

                                 elseif(phase == ‘began’ and target.name == ‘btnRight’ and paddleMove ~=  true) then

                                                paddleRight.rotation =  paddleRight.rotation*-1

                                end

end

regards,

Guy

Hello Guy,

So you have achieved your goal, or is there something still not working?

Brent

Hi Brent,

Issue is close, thank you.

Do I need to close/mark the post?

Regards,

Guy

Any ideas of how can I use an event listener on one object to control a second object but holding the function on a different lua file? thanks, Guy

Hi @guy.kastoriano,

There are a few different ways to access functions between modules, but one method I prefer (when using a scene management library like Composer) is to set the function to a Composer variable using “composer.setVariable()”, so it’s known to the overall library, then call that function from another module by accessing that variable with “composer.getVariable()”.

Something like this:

[lua]

– gameFunctions.lua

local composer = require( “composer” )

local function movePaddle( paddleObject )

   – your code

   – print( paddleObject )

end

– Assign function to Composer variable for access in other modules

composer.setVariable( “function_movePaddle”, movePaddle )

[/lua]

Then, in another module, you can access it like this:

[lua]

– level1.lua

local composer = require( “composer” )

local function_movePaddle = composer.getVariable( “function_movePaddle” )

– Call the function…

function_movePaddle( paddleObject )

[/lua]

Brent

Thank you Brent for you reply,

composer.setVariable()" seems to be very handy but it did not work in my case, probably I didn’t done it correctly.

Can it work from eventListener?

I finally found a solution in which I took out from the eventListner the function calling and use an intermediate function for that.

*****************Before******************

function gameListeners(action)

                                if(action == ‘add’) then

                                                btnLeft:addEventListener(‘touch’, myFunc.movePaddle)

                                else

                                                btnLeft:removeEventListener(‘touch’, myFunc.movePaddle)

                                end

end

******************After*****************

function gameListeners(action)

                                if(action == ‘add’) then

                                                btnLeft:addEventListener(‘touch’, handleMovePaddle)

                                else

                                                btnLeft:removeEventListener(‘touch’, handleMovePaddle)

                                end

end

function  handleMovePaddle (e)

            myFunc.movePaddle(paddleObject, e.phase, e.target.name)

end

and  in myFunc.lua

function movePaddle(paddleObject, phase, target)

                                if(phase == ‘began’ and target == ‘btnleft’ and paddleMove ~=  true) then

                                                paddleleft.rotation =  paddleleft.rotation*-1

                                 elseif(phase == ‘began’ and target.name == ‘btnRight’ and paddleMove ~=  true) then

                                                paddleRight.rotation =  paddleRight.rotation*-1

                                end

end

regards,

Guy

Hello Guy,

So you have achieved your goal, or is there something still not working?

Brent

Hi Brent,

Issue is close, thank you.

Do I need to close/mark the post?

Regards,

Guy