user3456833
user3456833

Reputation: 45

Corona SDK - Moving Player Left and Right Using Touch (Code Included)

Apologies if this was asked before, I tried searching to no avail. Anyways, I have some sort of logic error that is preventing my player object from moving left and right when I attempt to move it. Any tips would be appreciated, as I am a beginner to Lua. Thank you for your time.

function spawnPlayer()
player = display.newImage('player.png') 
player.x = display.contentWidth * 0.5
player.y = display.contentHeight - player.height
player.name = 'player'
physics.addBody(player)
Score()
end

function Score()
score = display.newText('Score: ', 1, 0, native.systemFontBold, 14)
score.x = _W / 9
score.y = 0
score.text = score.text .. tostring(intscore)
Listeners('add')
end

function Listeners(event)

if event == 'add' then
    --Runtime:addEventListener("accelerometer", accelPlayer)
    --player:addEventListener("touch", touchPlayer)
    player:addEventListener('touch', touchPlayer)
    player:addEventListener('accelerometer', accelPlayer)
elseif event == 'remove' then
    Runtime:removeEventListener("accelerometer", accelPlayer)
    player:removeEventListener("touch", touchPlayer)
end
end

function touchPlayer:touch(event)

if isSimulator then

    if event.phase == "began" then
        moveX = event.x - player.x
    elseif event.phase == "moved" then
        player.x = event.x - moveX
    end

    if((player.x - player.width * 0.5) < 0) then
        player.x = player.width * 0.5
    elseif((player.x + player.width * 0.5) > display.contentWidth)
    then
        player.x = display.contentWidth - player.width * 0.5
    end 
end
end

function accelPlayer:accelerometer(event)

player.x = display.contentCenterX - (display.contentCenterX * (event.yGravity * 3))

if((player.x - player.width * 0.5) < 0) then
    player.x = player.width * 0.5
elseif((player.x + player.width * 0.5) > display.contentWidth)
then
    player.x = display.contentWidth - player.width * 0.5
end
end

Upvotes: 0

Views: 1292

Answers (2)

Oliver
Oliver

Reputation: 29571

Try

print("in touch event")
if isSimulator then
    if event.phase == "began" then
        print("touch started")
        startX = event.x

    elseif event.phase == "moved" then
        local changeSinceLast = event.x - startX
        player.x = player.x + changeSinceLast 
        startX = event.x
        print("touch moved")

    end

Update:

If that prints nothing, then you have to look at what would cause touchPlayer:touch(event) to get called. It is Listeners('add'). Also, that function has touchPlayer but is touchPlayer exist when this function is called? So try:

function Listeners(event)
    if event == 'add' then
        print('adding touch listener:', touchPlayer)
        player:addEventListener('touch', touchPlayer)

Upvotes: 1

Chomu
Chomu

Reputation: 224

Because you didn't assign touch listener on device. You have assigned touch listener only for simulator and you can not test touch event on simulator. So assign it for device also and add below line in your begin condition.

display.getCurrentStage():setFocus( event.target )

Upvotes: 0

Related Questions