Hey Peach!
I was adapting your code, but I had problems early on with the routine below:
local function stop (event)
if event.phase =="ended" then
motionx = 0
motiony = 0
end
end
Runtime:addEventListener("touch", stop )
I had to look way to detect which button had been released so I read with more details on event propagation and setfocus and decided to do some more tests on old code.
I added Setfocus to each button, but now there was always the problem when two buttons were played simultaneously, there seems to be a conflict event, then the variable changed event internal to each function (I thing this is a bug) and everything worked perfectly.
The final code:
system.activate( "multitouch" )
local physics = require( "physics" )
physics.start()
physics.setGravity(0, 2.5)
local leftdir
local rightdir
local breaker
local ship
local ground
breaker = display.newImage( "botaoredondo.png");
breaker.x = 45
breaker.y = 290
leftdir = display.newImage( "setaesq.png");
leftdir.x = 375
leftdir.y = 290
rightdir = display.newImage( "setadir.png");
rightdir.x = 450
rightdir.y = 290
ground = display.newImage( "ground.png" )
ground.x = display.contentWidth / 2
ground.y = 250
physics.addBody( ground, "static", { friction=1, bounce=0, filter = {categoryBits = 4, maskBits = 2} })
ship = display.newImage("nave25.png")
ship.x = 200
ship.y = 30
-- nave.linearDamping = 5
shipshape01 = {-5,5, -5,-6, -1,-12, 0,-16, 6,-7, 6,5, 6,11, 4,14, -3,14, -5,10 }
shipshape02 = {-12,8, -10,4, -6,5, -6,10, -8,16, -11,16, -12,11}
shipshape03 = {7,6, 10,4, 12,8, 12,15, 10,16, 8,15, 7,11}
physics.addBody(ship, "dynamic",
{ density = 0.1, friction=0.01, shape = shipshape01, filter = {categoryBits = 2, maskBits = 12}},
{ density = 0.1, friction=0.01, shape = shipshape02, filter = {categoryBits = 2, maskBits = 12}},
{ density = 0.1, friction=0.01, shape = shipshape03, filter = {categoryBits = 2, maskBits = 12}}
)
ship.isFixedRotation = true
function btBreak( eventb )
if eventb.phase == "began" then
function impulseBreak()
ship:applyForce( 0, -0.5, ship.x, ship.y )
end
display.getCurrentStage():setFocus(breaker, eventb.id)
Runtime:addEventListener("enterFrame", impulseBreak)
else
display.getCurrentStage():setFocus(breaker, nil)
Runtime:removeEventListener("enterFrame", impulseBreak)
end
end
function btRight( eventr )
if eventr.phase == "began" then
function impulseRight()
ship:applyForce( -0.15, 0, ship.x, ship.y )
end
display.getCurrentStage():setFocus(rightdir, eventr.id)
Runtime:addEventListener("enterFrame", impulseRight)
else
display.getCurrentStage():setFocus(rightdir, nil)
Runtime:removeEventListener("enterFrame", impulseRight)
end
end
function btLeft( eventl )
if eventl.phase == "began" then
function impulseLeft()
ship:applyForce( 0.15, 0, ship.x, ship.y )
end
display.getCurrentStage():setFocus(leftdir, eventl.id)
Runtime:addEventListener("enterFrame", impulseLeft)
else
display.getCurrentStage():setFocus(leftdir, nil)
Runtime:removeEventListener("enterFrame", impulseLeft)
end
end
breaker:addEventListener("touch", btBreak)
leftdir:addEventListener("touch", btLeft)
rightdir:addEventListener("touch", btRight)
[import]uid: 57791 topic_id: 14585 reply_id: 54294[/import]