hey, i have a lua file of all the functions for my game and a regular scene where all my objects are created. i want to add an event listener to one of my objects so that i can drag it and shoot it(like angry birds). but to do that, i need to access the functions in my other lua file, how would i be able to do that. thx in advance
There are a couple way to do this
- pass in via composer goto
example
--level1.lua local function myTestFunc () print("hello") end local options = { effect = "fade", time = 800, params = { testFunc=myTestFunc} } composer.gotoScene( "level2", options ) --level2.lua local composer = require( "composer" ) local scene = composer.newScene() -- "scene:create()" function scene:create( event ) local myTestFunc = event.params.testFunc myTestFunc() end scene:addEventListener( "create", scene ) return scene
- via module
example:
--myDataStore.lua local m = {} function m.helloWorld () print("hello world") end return m -- main.lua local myDataStore = require("myDataStore") myDataStore.helloWorld()
thx for replying, but what if it was like this.
--level 1.lua local functions = require("functions") circle = display.newCircle(60, 70, 20 ); circle:addEventListener("touch",functions.practice) --functions.lua local functions = {}; function functions.practice() --code to apply drag and shoot animation like in angry birds... end return functions
i tried coding it similar to this style, but it keeps saying “attempt to index global ‘circle’ (a nil value)”
I made a demo, try to compare my code to yours. At first glance it looks good, but I sure there is something that I am not see.
yea your code works perfectly, but try to do something with the object, like print out the circle.x or move it around. Thats what i cant figure out.
try this
--level 1.lua local functions = require("functions") circle = display.newCircle(60, 70, 20 ); circle:addEventListener("touch",functions.practice) --functions.lua local functions = {}; function functions.practice(event) local circle= event.target print(circle.x) --code to apply drag and shoot animation like in angry birds... end return functions
ahh thank you soo much scott!@#$%^&*()
what i really needed was “event.target”
There are a couple way to do this
- pass in via composer goto
example
--level1.lua local function myTestFunc () print("hello") end local options = { effect = "fade", time = 800, params = { testFunc=myTestFunc} } composer.gotoScene( "level2", options ) --level2.lua local composer = require( "composer" ) local scene = composer.newScene() -- "scene:create()" function scene:create( event ) local myTestFunc = event.params.testFunc myTestFunc() end scene:addEventListener( "create", scene ) return scene
- via module
example:
--myDataStore.lua local m = {} function m.helloWorld () print("hello world") end return m -- main.lua local myDataStore = require("myDataStore") myDataStore.helloWorld()
thx for replying, but what if it was like this.
--level 1.lua local functions = require("functions") circle = display.newCircle(60, 70, 20 ); circle:addEventListener("touch",functions.practice) --functions.lua local functions = {}; function functions.practice() --code to apply drag and shoot animation like in angry birds... end return functions
i tried coding it similar to this style, but it keeps saying “attempt to index global ‘circle’ (a nil value)”
I made a demo, try to compare my code to yours. At first glance it looks good, but I sure there is something that I am not see.
yea your code works perfectly, but try to do something with the object, like print out the circle.x or move it around. Thats what i cant figure out.
try this
--level 1.lua local functions = require("functions") circle = display.newCircle(60, 70, 20 ); circle:addEventListener("touch",functions.practice) --functions.lua local functions = {}; function functions.practice(event) local circle= event.target print(circle.x) --code to apply drag and shoot animation like in angry birds... end return functions
ahh thank you soo much scott!@#$%^&*()
what i really needed was “event.target”