I’m currently having trouble with moving an object about the screen in my game… When you press the right d-pad, the character moves right, then the same with the left. But when you go to press right again it moves to the left. Then if you press right again it moves to the right… If anyone knows what is going on and how to fix it please help… any other comments/suggestions are also welcome…
[code]
function new()
local localGroup = display.newGroup()
local isPaused = false
– Game
– Variables
local direction = { left = “Left”, right = “Right” }
currentDirection = direction.right
– Buttons and Stats
local score = 0
local background = display.newImage(“images/bk-default.png”)
background.x = display.contentCenterX
background.y = display.contentCenterY
localGroup:insert(background)
require “sprite”
local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 8.9 )
local scoreText = display.newText( "Score: "… score, 0, 0, native.systemFontBold, 16 )
scoreText:setTextColor(0, 0, 0)
scoreText.x = display.contentWidth / 1.1
scoreText.y = display.contentHeight / 11
localGroup:insert(scoreText)
local pause = display.newImage(“images/pause.png”)
pause.x = display.contentWidth / 1.1
pause.y = display.contentHeight / 5
pause.alpha = .5
localGroup:insert(pause)
back = display.newCircle( 0, 0, 55 )
up = display.newCircle( 0, 0, 20 )
right = display.newCircle( 0, 0, 20 )
down = display.newCircle( 0, 0, 20 )
left = display.newCircle( 0, 0, 20 )
localGroup:insert(back)
localGroup:insert(up)
localGroup:insert(right)
localGroup:insert(down)
localGroup:insert(left)
up.x = display.contentWidth / 5; up.y = display.contentHeight / 1.47
right.x = display.contentWidth / 4; right.y = display.contentHeight / 1.27
down.x = display.contentWidth / 5; down.y = display.contentHeight / 1.12
left.x = display.contentWidth / 6.5; left.y = display.contentHeight / 1.27
back.x = display.contentWidth / 5; back.y = display.contentHeight / 1.27
up:setFillColor(96, 4, 4, 75)
right:setFillColor(96, 4, 4, 75)
down:setFillColor(96, 4, 4, 75)
left:setFillColor(96, 4, 4, 75)
back:setFillColor(0, 0, 0, 110)
– End Buttons and Stats
local ground = display.newImage( “images/Ground.png” )
ground.x = display.contentWidth / 2
ground.y = display.contentHeight / 1.75
physics.addBody( ground, “static”, {bounce=0.2, friction=0.7} )
localGroup:insert( ground )
local playerSheet = sprite.newSpriteSheet(“images/Walking/Walk.png”, 27, 99.5)
local playerSet = sprite.newSpriteSet( playerSheet, 1, 8 )
sprite.add( playerSet, “player”, 1, 8, 1000, 0 )
player = sprite.newSprite( playerSet )
player.x = display.contentWidth / 4
player.y = display.contentHeight / 3
physics.addBody( player, {bounce=0.2, friction=0.5} )
localGroup:insert(player)
– Game Events
local leftEvent = function( event )
if currentDirection == direction.left then
if event.phase == “began” then
currentDirection = direction.left
player:prepare(“player”)
player:play()
local move = function( event )
player.x = player.x - 5
print(“Moving Left”)
end
leftT = timer.performWithDelay(1, move, 0)
elseif event.phase == “ended” then
timer.cancel( leftT )
player:pause()
end
elseif currentDirection == direction.right then
player:scale(-1, 1)
currentDirection = direction.left
if event.phase == “began” then
player:prepare(“player”)
player:play()
local move = function( event )
player.x = player.x - 5
print(“Moving Left”)
end
leftT = timer.performWithDelay(1, move, 0)
elseif event.phase == “ended” then
timer.cancel( leftT )
player:pause()
end
end
end
local rightEvent = function( event )
if currentDirection == direction.right then
if event.phase == “began” then
currentDirection = direction.right
player:prepare(“player”)
player:play()
local moveR = function( event )
player.x = player.x + 5
print(“Moving Right”)
end
timer.performWithDelay(1, moveR, 1)
rightT = timer.performWithDelay(1, moveR, 0)
elseif event.phase == “ended” then
timer.cancel( rightT )
player:pause()
end
elseif currentDirection == direction.left then
player:scale(-1, 1)
currentDirection = direction.right
if event.phase == “began” then
player:prepare(“player”)
player:play()
local moveR = function( event )
player.x = player.x - 5
print(“Moving Right”)
end
rightT = timer.performWithDelay(1, moveR, 0)
elseif event.phase == “ended” then
timer.cancel( rightT )
player:pause()
end
end
end
left:addEventListener( “touch”, leftEvent )
right:addEventListener( “touch”, rightEvent )
– End Game Events
– End Game
local paused = function( event )
physics.pause()
if isPaused == false then
– Touch to go back
local touched = function( event )
director:changeScene(“menu”,“fade”)
end
isPaused = true
local pauseMenu = display.newImage(“images/Panel.png”)
pauseMenu.x = display.contentWidth / 2
pauseMenu.y = display.contentHeight / 2
pauseMenu.alpha = .5
localGroup:insert(pauseMenu)
exitMenu= ui.newButton{
defaultSrc = “images/Button.png”,
defaultX = 160,
defaultY = 32,
overSrc = “images/Button Over.png”,
overX = 160,
overY = 32,
id = “resumeMenu”,
text = “Exit”,
font = “Helvetica”,
textColor = { 0, 0, 0, 255 },
emboss = false
}
exitMenu:addEventListener( “tap”, touched )
exitMenu.x = display.contentWidth / 2
exitMenu.y = display.contentHeight / 2
resumeMenu= ui.newButton{
defaultSrc = “images/Button.png”,
defaultX = 160,
defaultY = 32,
overSrc = “images/Button Over.png”,
overX = 160,
overY = 32,
id = “resumeMenu”,
text = “Resume”,
font = “Helvetica”,
textColor = { 0, 0, 0, 255 },
emboss = false
}
resumeMenu.x = display.contentWidth / 2
resumeMenu.y = display.contentHeight / 3
local resume = function( event )
pauseMenu.alpha = 0
exitMenu.alpha = 0
resumeMenu.alpha = 0
isPaused = false
physics.start()
end
resumeMenu:addEventListener(“tap”, resume)
end
end
pause:addEventListener(“tap”, paused)
unloadMe = function()
end
– MUST return a display.newGroup()
return localGroup
end
[/code] [import]uid: 26946 topic_id: 13503 reply_id: 313503[/import]