Making character move left and right by touching screen (and another question)

I’m making a game that will be similar to Run Bird Run. Just wondering how I would make the player move left and right based on touching the left or right side of the screen. Would I create 2 invisible backgrounds, one on the left and one on the right with touch functions?

Also, I want the player to be able to attack with a sword to the left, right and up. Without creating a separate sprite, how would I make the sword (which i’ve already drawn) appear in the directions he’s attacking?

You can just add a touch handler to the one background and check whether event.x is greater or less than display.contentCenterX.

Not sure about the sword. Depends how you’re handling it.Does the sword have a swipe animation? Does it jab?

You can try sword.rotation = 90, 180, 270, etc., see if that works for you. Depending on your characters direction, you might need to set the sword to the character’s left, right, top, bottom.

Is there an example code posted for this type of event (moving character left and right based on where the player touched the screen)??

I’ve tried a few different things that haven’t worked. One of which was transition.to but that makes the character walk to wherever you touch the screen. Not exactly what I’m looking for. There’s probably no sense in reinventing the wheel if this has already been done before, eh? Haha.

Thanks.

This issue has been resolved. If anyone cares for the code, here it is:

P.S. Thank you hasty for suggesting the greater than / less than method for my touch event. That’s what tipped me off to write this code below.

motionx = 0; – Variable used to move character along x axis

speed = 4; – Set Walking Speed

– Insert the background image and stretch to fit the screen

local background = display.newImage(“skybg.png”)

background.x = display.contentWidth / 2

background.y = display.contentHeight / 2

local function movePlayer(event)

myPlayer.x = myPlayer.x + motionx;

end

Runtime:addEventListener(“enterFrame”, movePlayer)

local function stopPlayer (event)

if event.phase ==“ended” then

motionx = 0;

end

end

Runtime:addEventListener(“touch”, stopPlayer )

function playerVelocity(event)

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

    if event.x  >= display.contentCenterX then

    motionx = speed

    myPlayer.xScale = 1

    elseif event.x <= display.contentCenterX then

    motionx = -speed

    myPlayer.xScale = -1

end

    end

end

Runtime:addEventListener(“touch”, playerVelocity)

kudos. Have your tried it without the stop function? You seem to be just replicating what’s already in movePlayer. Also, moveGuy and movePlayer “sound” like they do the same thing. As your code gets bigger and your memory of what does what gets foggier, it’s worth having more precise names. You could change movePlayer to setPlayerVelocity, or something like that. Save yourself some confusion down the line.

I totally agree with you and I’m just as confused. If I take out moveguy, the character will only move one pixel per click. If I take out stop, the character runs continuously even after releasing the touch.

I tried putting the stop function inside movePlayer as if (event.phase == “ended”) but that didn’t work (player doesn’t stop upon release). But when I created a separate function, he stopped lol. Weird.

The only way it seems to work is in separate functions. Here’s the updated code:

local function movePlayer(event)

myPlayer.x = myPlayer.x + motionx;

end

Runtime:addEventListener(“enterFrame”, movePlayer)

local function stopPlayer (event)

if event.phase ==“ended” then

motionx = 0;

end

end

Runtime:addEventListener(“touch”, stopPlayer )

function playerVelocity(event)

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

    if event.x  >= display.contentCenterX then

    motionx = speed

    myPlayer.xScale = 1

    elseif event.x <= display.contentCenterX then

    motionx = -speed

    myPlayer.xScale = -1

end

    end

end

Runtime:addEventListener(“touch”, playerVelocity)

You probably just got your ifs and elseifs a bit mixed up. Give it another go and see if you can get it.

If you get stuck, you can check the solution below, but it’s worth trying as it’s not terribly difficult.

function playerVelocity(event) if (event.phase == "began") then if event.x \>= display.contentCenterX then motionx = speed myPlayer.xScale = 1 elseif event.x \<= display.contentCenterX then motionx = -speed myPlayer.xScale = -1 end elseif event.phase == "ended" then motionx = 0 end end

You’re right. I did have it mixed up. That solved the problem of needing a separate function for stop. Still needing a separate function to actually move the player. Oh well, I could manage!! Thanks man. Here’s the final code for anyone that wants to move their character left and right based on touching the left/right side of screen:

motionx = 0 – Variable used to move character along x axis

speed = 6 – Set Walking Speed

– Insert the background image and stretch to fit the screen

local background = display.newImageRect(“layer-1.png”, xMax-xMin, yMax-yMin)

background.x = _W*0.5

background.y = _H*0.5

local function movePlayer(event)

myPlayer.x = myPlayer.x + motionx

end

Runtime:addEventListener(“enterFrame”, movePlayer)

function playerVelocity(event)

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

    if event.x  >= display.contentCenterX then

    motionx = speed

    myPlayer.xScale = 1

    elseif event.x <= display.contentCenterX then

    motionx = -speed

    myPlayer.xScale = -1

end

elseif (event.phase == “ended”) then

motionx = 0

end

end

background:addEventListener(“touch”, playerVelocity)

 Would you mind if I PM you? I’ve got a few other questions to ask about feature I want to implement (but have no clue how to). I’m working on this Run Bird Run clone to make it available as a template for other new developers. I think there’s a shortage of source code/templates out there that are compatible with Graphics 2.0. Just thought I would contribute (even though I’m a beginner myself).

PM away. Just be warned that my knowledge is limited to the basics.

You can just add a touch handler to the one background and check whether event.x is greater or less than display.contentCenterX.

Not sure about the sword. Depends how you’re handling it.Does the sword have a swipe animation? Does it jab?

You can try sword.rotation = 90, 180, 270, etc., see if that works for you. Depending on your characters direction, you might need to set the sword to the character’s left, right, top, bottom.

Is there an example code posted for this type of event (moving character left and right based on where the player touched the screen)??

I’ve tried a few different things that haven’t worked. One of which was transition.to but that makes the character walk to wherever you touch the screen. Not exactly what I’m looking for. There’s probably no sense in reinventing the wheel if this has already been done before, eh? Haha.

Thanks.

This issue has been resolved. If anyone cares for the code, here it is:

P.S. Thank you hasty for suggesting the greater than / less than method for my touch event. That’s what tipped me off to write this code below.

motionx = 0; – Variable used to move character along x axis

speed = 4; – Set Walking Speed

– Insert the background image and stretch to fit the screen

local background = display.newImage(“skybg.png”)

background.x = display.contentWidth / 2

background.y = display.contentHeight / 2

local function movePlayer(event)

myPlayer.x = myPlayer.x + motionx;

end

Runtime:addEventListener(“enterFrame”, movePlayer)

local function stopPlayer (event)

if event.phase ==“ended” then

motionx = 0;

end

end

Runtime:addEventListener(“touch”, stopPlayer )

function playerVelocity(event)

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

    if event.x  >= display.contentCenterX then

    motionx = speed

    myPlayer.xScale = 1

    elseif event.x <= display.contentCenterX then

    motionx = -speed

    myPlayer.xScale = -1

end

    end

end

Runtime:addEventListener(“touch”, playerVelocity)

kudos. Have your tried it without the stop function? You seem to be just replicating what’s already in movePlayer. Also, moveGuy and movePlayer “sound” like they do the same thing. As your code gets bigger and your memory of what does what gets foggier, it’s worth having more precise names. You could change movePlayer to setPlayerVelocity, or something like that. Save yourself some confusion down the line.

I totally agree with you and I’m just as confused. If I take out moveguy, the character will only move one pixel per click. If I take out stop, the character runs continuously even after releasing the touch.

I tried putting the stop function inside movePlayer as if (event.phase == “ended”) but that didn’t work (player doesn’t stop upon release). But when I created a separate function, he stopped lol. Weird.

The only way it seems to work is in separate functions. Here’s the updated code:

local function movePlayer(event)

myPlayer.x = myPlayer.x + motionx;

end

Runtime:addEventListener(“enterFrame”, movePlayer)

local function stopPlayer (event)

if event.phase ==“ended” then

motionx = 0;

end

end

Runtime:addEventListener(“touch”, stopPlayer )

function playerVelocity(event)

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

    if event.x  >= display.contentCenterX then

    motionx = speed

    myPlayer.xScale = 1

    elseif event.x <= display.contentCenterX then

    motionx = -speed

    myPlayer.xScale = -1

end

    end

end

Runtime:addEventListener(“touch”, playerVelocity)

You probably just got your ifs and elseifs a bit mixed up. Give it another go and see if you can get it.

If you get stuck, you can check the solution below, but it’s worth trying as it’s not terribly difficult.

function playerVelocity(event) if (event.phase == "began") then if event.x \>= display.contentCenterX then motionx = speed myPlayer.xScale = 1 elseif event.x \<= display.contentCenterX then motionx = -speed myPlayer.xScale = -1 end elseif event.phase == "ended" then motionx = 0 end end

You’re right. I did have it mixed up. That solved the problem of needing a separate function for stop. Still needing a separate function to actually move the player. Oh well, I could manage!! Thanks man. Here’s the final code for anyone that wants to move their character left and right based on touching the left/right side of screen:

motionx = 0 – Variable used to move character along x axis

speed = 6 – Set Walking Speed

– Insert the background image and stretch to fit the screen

local background = display.newImageRect(“layer-1.png”, xMax-xMin, yMax-yMin)

background.x = _W*0.5

background.y = _H*0.5

local function movePlayer(event)

myPlayer.x = myPlayer.x + motionx

end

Runtime:addEventListener(“enterFrame”, movePlayer)

function playerVelocity(event)

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

    if event.x  >= display.contentCenterX then

    motionx = speed

    myPlayer.xScale = 1

    elseif event.x <= display.contentCenterX then

    motionx = -speed

    myPlayer.xScale = -1

end

elseif (event.phase == “ended”) then

motionx = 0

end

end

background:addEventListener(“touch”, playerVelocity)

 Would you mind if I PM you? I’ve got a few other questions to ask about feature I want to implement (but have no clue how to). I’m working on this Run Bird Run clone to make it available as a template for other new developers. I think there’s a shortage of source code/templates out there that are compatible with Graphics 2.0. Just thought I would contribute (even though I’m a beginner myself).

PM away. Just be warned that my knowledge is limited to the basics.