Hi all,
I’m creating an adventure game and have reached a snag. The game is structured around ‘rooms’ (which are Storyboard lua files), each of which use the same touch logic for moving your character, opening the inventory, etc.
Rather than place the same redundant code in every storyboard file, I want to place the code for the room’s “touch” event in an external module. This works, but the touch event needs to access variables that are local to the storyboard file, things like the player character, the camera, etc. I thought that since I’m using “require” to place the touch event in a local function, this would work, but it seems not.
This is the relevant portion of the storyboard file:
[lua]-- Load common functions
local lunaCommon = require(“craters.luna_common”)
local Camera = nil
Camera = cameraActor.new(roomWidth, roomHeight)
– roomGroup is a display group that holds the scene
roomGroup.touch = lunaCommon.roomTouch
roomGroup:addEventListener(“touch”, roomGroup)[/lua]
The code in luna_common:
[lua]-- Basic room touches and inventory opening functionality
roomTouch = function(self, e)
– Camera movement, player movement, inventory opening, etc.
Camera.pan(e.x, e.y)
end[/lua]
When this code is run, I get an error: “attempt to index global ‘Camera’ (a nil value)”.
Is there any way to do this, or a better way to create a re-usable touch listener?
Thanks!
- David [import]uid: 149111 topic_id: 31509 reply_id: 331509[/import]