Hi there!
I’m making my first game with Corona, I have made games for windows in the past but wanted to try mobile.
Anyway, the problem I’m having is that I’m not quite sure how to layout the code. One example is this (this is a platformer with dpad, A and B buttons)
I’ve made a hud.lua file that has two functions, one for loading, positioning the images for the buttons. And another function to bind the eventlisteners.
The relevant part of my level1.lua is this;
-- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view hud.load() end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view hud.start() end
The hud.lua looks like this;
-- Load the HUD local dpad, buttonA, buttonB local buttonAAction, buttonBAction = false, false function load() -- DPad dpad = display.newImageRect( "images/dpad/dpad\_0\_normal.png", 128, 128 ) dpad:setReferencePoint(display.BottomLeftReferencePoint) dpad.y = display.contentHeight dpad.x = 0 -- Button A buttonA = display.newImageRect( "images/dpad/button\_A.png", 64, 64 ) buttonA:setReferencePoint(display.BottomRightReferencePoint) buttonA.y = display.contentHeight - 15 buttonA.x = display.contentWidth - 80 -- Button B buttonB = display.newImageRect( "images/dpad/button\_B.png", 64, 64 ) buttonB:setReferencePoint(display.BottomRightReferencePoint) buttonB.y = display.contentHeight - 15 buttonB.x = display.contentWidth end -- Start HUD (add the event listeners) function start() -- Button A function buttonA:touch(event) if event.phase == "began" then buttonAAction = true end end buttonA:addEventListener("touch", buttonA) -- Button B function buttonB:touch(event) if event.phase == "began" then buttonBAction = true end end buttonB:addEventListener("touch", buttonB) end
This eventListeners work fine when I jusr print() something. But now i want the buttons to influjence the player.
I can’t for example in the listener for button A I want to run player.jump() to start the jump action for the player.
With the framework I used for windows I would always have a reference to the main game object so I could from (basicly) anywhere go Game.player.jump()
I hope you understand my problem.
How do you do handle stuff like this in Corona?
Thanks in advance,
Richard