Help needed???

Hi all

I do apologize in advance if this post is in the wrong place as i am really new. I am just getting to know corona and lua and i am stumped at what is probably a very simple problem.

I am making a “Mario” style plat-former and I am having trouble getting my character to move continuously. When testing my game he only moves one space for each time I press the left or right direction buttons and touching and holding the left or right direction buttons only move the character one space.

here is my code so far:

function leftBtn:touch(e)  
 if(e.phase == "began") then  
  
 character.x = character.x + speedL   
 end  
  
 end  
  
 function rightBtn:touch(e)  
 if(e.phase == "began") then  
 character.x = character.x + speedR   
  
  
 end  
 end  
  
  
  
 --- Object listener declarations go here  
 leftBtn:addEventListener("touch",leftBtn)  
 rightBtn:addEventListener("touch",rightBtn)  

How do i get my character to move for as long as my finger is touching thew left or right direction buttons?
Thanks in advance

Kane Lakin [import]uid: 217352 topic_id: 35579 reply_id: 335579[/import]

Something like this should do the trick. I’ve combined your touch listeners into one function, and created a gameLoop function which fires every frame. If the button is being held down, the character moves. You’ll probably need to adjust your speedL and speedR variables as these will be being applied 30 or 60 times a second.

[lua]leftBtn.pressed = false
rightBtn.pressed = false
local gameLoop = function ()

if leftBtn.pressed == true then
character.x = character.x + speedL
end

if rightBtn.pressed == true then
character.x = character.x + speedR
end

end

function touchBtn:touch(e)

local obj = event.target

if(e.phase == “began”) then

obj.pressed = true

end

if(e.phase == “ended”) then

obj.pressed = false

end

end
— Object listener declarations go here
leftBtn:addEventListener(“touch”,touchBtn)
rightBtn:addEventListener(“touch”,touchBtn)
Runtime:addEventListener(“enterFrame”,gameLoop)[/lua] [import]uid: 93133 topic_id: 35579 reply_id: 141400[/import]

Hi thank you for replying to my post I have a new problem however, "attempt to index local ‘touchBtn’ 'a nil value could you explain what this means please, and how to get around it? [import]uid: 217352 topic_id: 35579 reply_id: 141525[/import]

Try replacing the touch function with this:

[lua]local touchBtn = function (e)

local obj = event.target

if(e.phase == “began”) then

obj.pressed = true

end

if(e.phase == “ended”) then

obj.pressed = false

end

end[/lua] [import]uid: 93133 topic_id: 35579 reply_id: 141529[/import]

Thanks for the reply again but i’ve got another error on line 185 "attempt to index local ‘leftBtn’ a nil vaule. Does this mean the game cant find the object? Trying to get around these problems can be very frustrating sorry to keep annoying you with these questions. If i give you my entire code for the level you can exaime where the problem lies:

[code]


– level1


– this is where you add external modules

local storyboard = require(“storyboard”)
local scene = storyboard.newScene()
local movieclip = require(“movieclip”)


— Physics setup
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 9.8)
–physics.setDrawMode(“hybrid”)


— declare your variables here they should all be Local Variables
— Display Variables ------

local background
local bottonFloor
local character
local leftBtn
local rightBtn
local jumpBtn

— Function Variables -----

---- Group Variables --------

---- Sound Variables --------

— ordinary variables ----+

local speedR = 4.5
local speedL = -4.5

– image tables
local standing = {}
local walkLeft = {}
local walkRight = {}




– NOTE:

– Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.



– BEGINNING OF YOUR IMPLEMENTATION

– Called when the scene’s view does not exist:
function scene:createScene( event )
local view = self.view

—variable declarations go here -------


– Declare your display groups in this section

–standing = {“images/characterStanding.png”}
rightBtn = {“images/characterWalkingA.png”,“images/characterWalkingB.png”,“characterWalkingC.png”}
walkleft = {“images/leftCharacterWalkingA.png”,“images/leftCharacterWalkingB.png”,“images/leftCharacterWalkingC”}

– Add all of your objects here that appear at the start of the screen and position them

background = display.newImage(“images/backgroundA.png”)

bottomFloor = display.newImage(“images/platform450x30.png”)
bottomFloor.y = display.contentHeight * 0.9
bottomFloor.x = display.contentWidth * 0.4
physics.addBody(bottomFloor,“static”)

character = movieclip.newAnim({“images/characterStanding.png”})
physics.addBody(character, “dymamic”)

leftBtn = display.newImage(“images/leftButton.png”)
leftBtn.x = display.contentWidth * 0.02
leftBtn.y = display.contentHeight * 0.9
leftBtn.pressed = false

rightBtn = display.newImage(“images/rightButton.png”)
rightBtn.x = display.contentWidth * 0.07
rightBtn.y = display.contentHeight * 0.9
rightBtn.pressed = false

jumpBtn = display.newImage(“images/jumpButton.png”)
jumpBtn.x = display.contentWidth * 0.045
jumpBtn.y = display.contentHeight * 0.85

— load your sound files here -------------


— add your objects to the view group e.g. view:insert(objectName)

view:insert(background)
view:insert(bottomFloor)
view:insert(character)
view:insert(leftBtn)
view:insert(rightBtn)
view:insert(jumpBtn)

end

– Called immediately after scene has moved onscreen:

function scene:enterScene( event )
local view = self.view
end

— All Functions go in here

local gameLoop = function ()

if leftBtn.pressed == true then
character.x = character.x + speedL
end

if rightBtn.pressed == true then
character.x = character.x + speedR
end

end

local touchBtn = function (e)

local obj = event.target

if(e.phase == “began”) then

obj.pressed = true

end

if(e.phase == “ended”) then

obj.pressed = false

end

end


— Object listener declarations go here
leftBtn:addEventListener(“touch”,touchBtn)
rightBtn:addEventListener(“touch”,touchBtn)
Runtime:addEventListener(“enterFrame”,gameLoop)
jumpBtn:addEventListener(“touch”,jumpBtn)


— Timer Declarations go here


— Runtime listener “enterFrame” declarations go here
Runtime:addEventListener(“touch”,rightBtn)
Runtime:addEventListener(“touch”,leftBtn)
Runtime:addEventListener(“touch”,touchBtn)

– Called when scene is about to move offscreen:
function scene:exitScene( event )


— Remove Timers here


— Remove Runtime listener “enterFrame” declarations here

----- purge scene here ----------
storyboard.purgeScene(“level1”)
end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )

– this function is not normally used for simple games.

end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().

scene:addEventListener( “destroyScene”, scene )


return scene/code> [import]uid: 217352 topic_id: 35579 reply_id: 141535[/import]

It is because at the time you are trying to add the touch listeners, the buttons don’t exist as createScene has not run yet. Create scene is only fired around line 242.

Add your runtime listener in enterScene, and your touch listeners immediately after the object is created in createScene. You are also adding your event listeners twice, you don’t need the second group of ‘runtime listeners’.

I would also move all your non-Storyboard functions above createScene for ease of use - at the moment they are all mixed in together.

[import]uid: 93133 topic_id: 35579 reply_id: 141657[/import]

Something like this should do the trick. I’ve combined your touch listeners into one function, and created a gameLoop function which fires every frame. If the button is being held down, the character moves. You’ll probably need to adjust your speedL and speedR variables as these will be being applied 30 or 60 times a second.

[lua]leftBtn.pressed = false
rightBtn.pressed = false
local gameLoop = function ()

if leftBtn.pressed == true then
character.x = character.x + speedL
end

if rightBtn.pressed == true then
character.x = character.x + speedR
end

end

function touchBtn:touch(e)

local obj = event.target

if(e.phase == “began”) then

obj.pressed = true

end

if(e.phase == “ended”) then

obj.pressed = false

end

end
— Object listener declarations go here
leftBtn:addEventListener(“touch”,touchBtn)
rightBtn:addEventListener(“touch”,touchBtn)
Runtime:addEventListener(“enterFrame”,gameLoop)[/lua] [import]uid: 93133 topic_id: 35579 reply_id: 141400[/import]

Hi thank you for replying to my post I have a new problem however, "attempt to index local ‘touchBtn’ 'a nil value could you explain what this means please, and how to get around it? [import]uid: 217352 topic_id: 35579 reply_id: 141525[/import]

Try replacing the touch function with this:

[lua]local touchBtn = function (e)

local obj = event.target

if(e.phase == “began”) then

obj.pressed = true

end

if(e.phase == “ended”) then

obj.pressed = false

end

end[/lua] [import]uid: 93133 topic_id: 35579 reply_id: 141529[/import]

Thanks for the reply again but i’ve got another error on line 185 "attempt to index local ‘leftBtn’ a nil vaule. Does this mean the game cant find the object? Trying to get around these problems can be very frustrating sorry to keep annoying you with these questions. If i give you my entire code for the level you can exaime where the problem lies:

[code]


– level1


– this is where you add external modules

local storyboard = require(“storyboard”)
local scene = storyboard.newScene()
local movieclip = require(“movieclip”)


— Physics setup
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 9.8)
–physics.setDrawMode(“hybrid”)


— declare your variables here they should all be Local Variables
— Display Variables ------

local background
local bottonFloor
local character
local leftBtn
local rightBtn
local jumpBtn

— Function Variables -----

---- Group Variables --------

---- Sound Variables --------

— ordinary variables ----+

local speedR = 4.5
local speedL = -4.5

– image tables
local standing = {}
local walkLeft = {}
local walkRight = {}




– NOTE:

– Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.



– BEGINNING OF YOUR IMPLEMENTATION

– Called when the scene’s view does not exist:
function scene:createScene( event )
local view = self.view

—variable declarations go here -------


– Declare your display groups in this section

–standing = {“images/characterStanding.png”}
rightBtn = {“images/characterWalkingA.png”,“images/characterWalkingB.png”,“characterWalkingC.png”}
walkleft = {“images/leftCharacterWalkingA.png”,“images/leftCharacterWalkingB.png”,“images/leftCharacterWalkingC”}

– Add all of your objects here that appear at the start of the screen and position them

background = display.newImage(“images/backgroundA.png”)

bottomFloor = display.newImage(“images/platform450x30.png”)
bottomFloor.y = display.contentHeight * 0.9
bottomFloor.x = display.contentWidth * 0.4
physics.addBody(bottomFloor,“static”)

character = movieclip.newAnim({“images/characterStanding.png”})
physics.addBody(character, “dymamic”)

leftBtn = display.newImage(“images/leftButton.png”)
leftBtn.x = display.contentWidth * 0.02
leftBtn.y = display.contentHeight * 0.9
leftBtn.pressed = false

rightBtn = display.newImage(“images/rightButton.png”)
rightBtn.x = display.contentWidth * 0.07
rightBtn.y = display.contentHeight * 0.9
rightBtn.pressed = false

jumpBtn = display.newImage(“images/jumpButton.png”)
jumpBtn.x = display.contentWidth * 0.045
jumpBtn.y = display.contentHeight * 0.85

— load your sound files here -------------


— add your objects to the view group e.g. view:insert(objectName)

view:insert(background)
view:insert(bottomFloor)
view:insert(character)
view:insert(leftBtn)
view:insert(rightBtn)
view:insert(jumpBtn)

end

– Called immediately after scene has moved onscreen:

function scene:enterScene( event )
local view = self.view
end

— All Functions go in here

local gameLoop = function ()

if leftBtn.pressed == true then
character.x = character.x + speedL
end

if rightBtn.pressed == true then
character.x = character.x + speedR
end

end

local touchBtn = function (e)

local obj = event.target

if(e.phase == “began”) then

obj.pressed = true

end

if(e.phase == “ended”) then

obj.pressed = false

end

end


— Object listener declarations go here
leftBtn:addEventListener(“touch”,touchBtn)
rightBtn:addEventListener(“touch”,touchBtn)
Runtime:addEventListener(“enterFrame”,gameLoop)
jumpBtn:addEventListener(“touch”,jumpBtn)


— Timer Declarations go here


— Runtime listener “enterFrame” declarations go here
Runtime:addEventListener(“touch”,rightBtn)
Runtime:addEventListener(“touch”,leftBtn)
Runtime:addEventListener(“touch”,touchBtn)

– Called when scene is about to move offscreen:
function scene:exitScene( event )


— Remove Timers here


— Remove Runtime listener “enterFrame” declarations here

----- purge scene here ----------
storyboard.purgeScene(“level1”)
end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )

– this function is not normally used for simple games.

end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().

scene:addEventListener( “destroyScene”, scene )


return scene/code> [import]uid: 217352 topic_id: 35579 reply_id: 141535[/import]

It is because at the time you are trying to add the touch listeners, the buttons don’t exist as createScene has not run yet. Create scene is only fired around line 242.

Add your runtime listener in enterScene, and your touch listeners immediately after the object is created in createScene. You are also adding your event listeners twice, you don’t need the second group of ‘runtime listeners’.

I would also move all your non-Storyboard functions above createScene for ease of use - at the moment they are all mixed in together.

[import]uid: 93133 topic_id: 35579 reply_id: 141657[/import]