Load up Charecter, image sheets and handlers from another Lua file

Hi guys, I’m trying to make a game but have it so that I don’t have to load images for every scene again and again. In a way it’s simular to “Don’t touch the spikes” where the player Charecter immediately moves into the game at a tap from the menu screen.

So I can I do this with corona? I want to have the players params load up complete with physics settings as well as player control functions, image sheets all from a seperate file.

It’s definitely possible to accomplish this with Corona. Where exactly are you encountering issues? Are you able to call simple data from other modules from within your game scene? 

Hi Alex, I have setup a seperate lua file called gameConfig and its allowing me to call my player settings in menu.lua except without its properties such as physics body (I turn the body to dynamic and it doesn’t drop or anything) and also the movePlayer function creates a global event = nil error, which should move the player on the menu screen.

gameConfig.lua

local M = {} Anims = { "images/birdAnim.png", "images/birdAnim2.png", } local sheetData = {width=45, height=35, numFrames=2, sheetContentWidth=90, sheetContentHeight=35} local imagesheet = graphics.newImageSheet(Anims[math.random(1,2)], sheetData) local sequenceData = { {name="hop", start=1, count=1, time=300}, {name="drop", start=2, count=1, time=300} } local function createBird() bird = display.newSprite(imagesheet, sequenceData) bird.x = display.contentCenterX - 150 bird.y = 140 bird.name = "bird" bird.bodyType = "dynamic" bird.isFixedRotation = true bird.isSensor = true bird:setSequence("drop") bird:play() end local function movePlayer(event) if event.phase == "began" then bird:setLinearVelocity(0, -flapForce) playSFX(sndJump) bird:setSequence("hop") bird:play() elseif event.phase == "ended" then bird:setSequence("drop") bird:play() end return true end M.createBird = createBird M.movePlayer = movePlayer return M 

Menu.lua

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local physics = require "physics" physics.start() physics.setGravity( 0, 50 ) local status = require("gameConfig") local flapForce = 350 gameConfig.createBird() gameConfig.movePlayer() 

OK, so for the first issue, I don’t see where you’re creating your physics body. It just looks like you’re just setting the bodyType, which doesn’t do anything unless you actually create the physics body. 

Second issue, the movePlayer function you created appears to be intended as a touch listener, but you aren’t assigning it to any kind of object or Runtime event in your storyboard scene.

Take a look at the below video series to get a bit more info on how to structure your game and how to operate with functions, physics bodies and touch logic.

https://www.youtube.com/watch?v=0GtUxdSeWzk&feature=youtu.be

Sorry about that. I forgot to show the listener for movePlayer function. I actually had this in my enterScene for menu.lua but got the error. I then also had it in gameConfig.lua file and it didn’t work: “Attempt to index global ‘event’ (a nil value)” error still occured. Im not sure where I should have this code placed but ideally i want the player to be idle on the menu.lua scene and then player can tap and activate movePlayer.

Runtime:addEventListener("touch", movePlayer)

As for the physics body, that seemed to fixed that issue.

surely that seems the right thing

It’s definitely possible to accomplish this with Corona. Where exactly are you encountering issues? Are you able to call simple data from other modules from within your game scene? 

Hi Alex, I have setup a seperate lua file called gameConfig and its allowing me to call my player settings in menu.lua except without its properties such as physics body (I turn the body to dynamic and it doesn’t drop or anything) and also the movePlayer function creates a global event = nil error, which should move the player on the menu screen.

gameConfig.lua

local M = {} Anims = { "images/birdAnim.png", "images/birdAnim2.png", } local sheetData = {width=45, height=35, numFrames=2, sheetContentWidth=90, sheetContentHeight=35} local imagesheet = graphics.newImageSheet(Anims[math.random(1,2)], sheetData) local sequenceData = { {name="hop", start=1, count=1, time=300}, {name="drop", start=2, count=1, time=300} } local function createBird() bird = display.newSprite(imagesheet, sequenceData) bird.x = display.contentCenterX - 150 bird.y = 140 bird.name = "bird" bird.bodyType = "dynamic" bird.isFixedRotation = true bird.isSensor = true bird:setSequence("drop") bird:play() end local function movePlayer(event) if event.phase == "began" then bird:setLinearVelocity(0, -flapForce) playSFX(sndJump) bird:setSequence("hop") bird:play() elseif event.phase == "ended" then bird:setSequence("drop") bird:play() end return true end M.createBird = createBird M.movePlayer = movePlayer return M 

Menu.lua

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local physics = require "physics" physics.start() physics.setGravity( 0, 50 ) local status = require("gameConfig") local flapForce = 350 gameConfig.createBird() gameConfig.movePlayer() 

OK, so for the first issue, I don’t see where you’re creating your physics body. It just looks like you’re just setting the bodyType, which doesn’t do anything unless you actually create the physics body. 

Second issue, the movePlayer function you created appears to be intended as a touch listener, but you aren’t assigning it to any kind of object or Runtime event in your storyboard scene.

Take a look at the below video series to get a bit more info on how to structure your game and how to operate with functions, physics bodies and touch logic.

https://www.youtube.com/watch?v=0GtUxdSeWzk&feature=youtu.be

Sorry about that. I forgot to show the listener for movePlayer function. I actually had this in my enterScene for menu.lua but got the error. I then also had it in gameConfig.lua file and it didn’t work: “Attempt to index global ‘event’ (a nil value)” error still occured. Im not sure where I should have this code placed but ideally i want the player to be idle on the menu.lua scene and then player can tap and activate movePlayer.

Runtime:addEventListener("touch", movePlayer)

As for the physics body, that seemed to fixed that issue.

surely that seems the right thing