Tests to see whether touch event is moving?

I need help on testing to see which direction a touch event is moving. If it is moving right, I want the character to move right, if it is moving left, I want the character to move left, and if it is moving up, I want the character to jump. Any ideas? Thanks. Here is the code for the file (dashes are around the lines that have the code that I need help on):



– level1.lua


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

local widget = require “widget”


– BEGINNING OF YOUR IMPLEMENTATION

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


local physics = require(“physics”)
physics.start()
motionx = 0
speed = 2

function scene:createScene( event )
    local group = self.view

    – display a background1 image
    local background = display.newImageRect(“Images/Level1.png”, dW,dH*1.2, true)
    background.anchorX = 0
    background.anchorY = 0
    background.x, background.y = 160, 240
    group:insert(background)

    local ground1 = display.newImageRect(“Images/Ground1.png”, dW, 90, true)
    ground1.x, ground1.y = dW*0.5, 480
    group:insert(ground1)
    physics.addBody(ground1, “static”, {friction=0.5,bounce=0})
    ground1:setReferencePoint(display.BottomLeftReferencePoint)

    local ground2 = display.newImageRect(“Images/Ground2.png”, dW, 90, true)
    ground2.x, ground2.y = dW*0.5, 480
    group:insert(ground2)
    physics.addBody(ground2, “static”, {friction=0.5,bounce=0})
    ground2:setReferencePoint(display.BottomLeftReferencePoint)

    local character = display.newImage(“Images/Character.png”, 20, 20)
    character.x, character.y = 40, 400
    group:insert(ground1)
    physics.addBody(character, “dynamic”, {friction=0.5, bounce=0.3})
    self.character = character

    group:addEventListener(“touch”, self)

    function scene:touch(e)
        local phase = e.phase
        --if(phase == ‘moved’) then
            --code to determine whether moving right
            motionx = speed*2
            character.x = character.x + motionx
        end
        if(phase == ‘ended’) then
            motionx = 0
        end
    end
end

if event.x > event.xStart then

That test will see if the movement is to the right of where the start touched.  However if you want the finger to stay touched and move back and forth, you will have to remember the last event.x and event.y. 

local lastX = 0

local lastY = 0

if event.phase == “began” then

     lastX = event.x

     lastY = event.y

elseif event.phase == “moved” then

    if event.x > lastX then – moved right

    else – moved left

    if event.y > last.y then – moved down

    else – moved up

elseif event.phase == “ended” then

    – do whatever

end

lastX  = event.x

lastY = event.y

return true

or something thing like that.

Hi

I tried the code, but it only moved right and rarely jumped. I put those piece of code in the section with the comments to do so. Also it never moves left when I put the code in that section. Here’s the code:

function scene:createScene( event )
    local group = self.view

    local physics = require(“physics”)
    physics.start()
    motionx = 0
    speed = 2
    scrollSpeed = 8

    local background = display.newImageRect(“Images/Level1.png”, dW,dH*1.2, true)
    background.anchorX = 0
    background.anchorY = 0
    background.x, background.y = 160, 240
    group:insert(background)

    local ground1 = display.newImageRect(“Images/Ground1.png”, dW, 90, true)
    ground1.x, ground1.y = dW*0.5, 480
    group:insert(ground1)
    physics.addBody(ground1, “static”, {friction=0.5,bounce=0})
    ground1:setReferencePoint(display.BottomLeftReferencePoint)

    local ground2 = display.newImageRect(“Images/Ground2.png”, dW, 90, true)
    ground2.x, ground2.y = (dW*0.5)+180, 480
    group:insert(ground2)
    physics.addBody(ground2, “static”, {friction=0.5,bounce=0})
    ground2:setReferencePoint(display.BottomLeftReferencePoint)

    local ground3 = display.newImageRect(“Images/Ground1.png”, dW, 90, true)
    ground3.x, ground3.y = (dW*0.5)+360, 480
    group:insert(ground3)
    physics.addBody(ground3, “static”, {friction=0.5,bounce=0})
    ground3:setReferencePoint(display.BottomLeftReferencePoint)

    local character = display.newImage(“Images/Character.png”, 20, 20)
    character.x, character.y = 40, 400
    group:insert(ground1)
    physics.addBody(character, “dynamic”, {friction=0.5, bounce=0.3})
    self.character = character

end

function scene:enterScene(event)
    local group = self.view
    
    group:addEventListener(“touch”, self)

    local character = self.character

    function scene:touch(e)
        if(e.x > e.xStart) then
            local lastX = 0
            local lastY = 0
            phase = e.phase    
            if(e.phase == ‘began’) then
                lastX = e.x
                lastY = e.y
            elseif(e.phase == ‘moved’) then
                if(e.x > lastX) then
                    motionx = speed
                    character.x = character.x + motionx
                elseif(e.x < lastX) then
                    motionx = speed
                    character.x = character.x - motionx
                end
                if(e.y > lastY) then
                else
                    character:setLinearVelocity(0, -200)
                end
            end
            lastX = e.x
            lastY = e.y
        return true
        end
        if(phase == ‘ended’) then
            motionx = 0
        end
    end

    function scene:scrolling(e)
        local scrollView = widget.newScrollView{left = 0, width = 200, height = 0, verticalScrollDisabled = true}
        scrollView:insert(ground1)
        scrollView:insert(ground2)
        scrollView:insert(ground3)
    end
    – INSERT code here (e.g. start timers, load audio, start listeners, etc.)
end
 

Thanks

You are starting your if statement with

if(e.x \> e.xStart) then

So basically you are telling it to execute only if it is supposed to move right.  Try this:

 

local lastX local lastY function group:touch(e) local phase = e.phase if(e.phase == 'began') then lastX = e.x lastY = e.y elseif(e.phase == 'moved') then if(e.x \> lastX) then print("Moving Right") elseif(e.x \< lastX) then print("Moving Left") end if(e.y \< lastY-5) then print("Jumping") else end elseif(phase == 'ended') then print("Stopped") end lastX = e.x lastY = e.y return true end

if event.x > event.xStart then

That test will see if the movement is to the right of where the start touched.  However if you want the finger to stay touched and move back and forth, you will have to remember the last event.x and event.y. 

local lastX = 0

local lastY = 0

if event.phase == “began” then

     lastX = event.x

     lastY = event.y

elseif event.phase == “moved” then

    if event.x > lastX then – moved right

    else – moved left

    if event.y > last.y then – moved down

    else – moved up

elseif event.phase == “ended” then

    – do whatever

end

lastX  = event.x

lastY = event.y

return true

or something thing like that.

Hi

I tried the code, but it only moved right and rarely jumped. I put those piece of code in the section with the comments to do so. Also it never moves left when I put the code in that section. Here’s the code:

function scene:createScene( event )
    local group = self.view

    local physics = require(“physics”)
    physics.start()
    motionx = 0
    speed = 2
    scrollSpeed = 8

    local background = display.newImageRect(“Images/Level1.png”, dW,dH*1.2, true)
    background.anchorX = 0
    background.anchorY = 0
    background.x, background.y = 160, 240
    group:insert(background)

    local ground1 = display.newImageRect(“Images/Ground1.png”, dW, 90, true)
    ground1.x, ground1.y = dW*0.5, 480
    group:insert(ground1)
    physics.addBody(ground1, “static”, {friction=0.5,bounce=0})
    ground1:setReferencePoint(display.BottomLeftReferencePoint)

    local ground2 = display.newImageRect(“Images/Ground2.png”, dW, 90, true)
    ground2.x, ground2.y = (dW*0.5)+180, 480
    group:insert(ground2)
    physics.addBody(ground2, “static”, {friction=0.5,bounce=0})
    ground2:setReferencePoint(display.BottomLeftReferencePoint)

    local ground3 = display.newImageRect(“Images/Ground1.png”, dW, 90, true)
    ground3.x, ground3.y = (dW*0.5)+360, 480
    group:insert(ground3)
    physics.addBody(ground3, “static”, {friction=0.5,bounce=0})
    ground3:setReferencePoint(display.BottomLeftReferencePoint)

    local character = display.newImage(“Images/Character.png”, 20, 20)
    character.x, character.y = 40, 400
    group:insert(ground1)
    physics.addBody(character, “dynamic”, {friction=0.5, bounce=0.3})
    self.character = character

end

function scene:enterScene(event)
    local group = self.view
    
    group:addEventListener(“touch”, self)

    local character = self.character

    function scene:touch(e)
        if(e.x > e.xStart) then
            local lastX = 0
            local lastY = 0
            phase = e.phase    
            if(e.phase == ‘began’) then
                lastX = e.x
                lastY = e.y
            elseif(e.phase == ‘moved’) then
                if(e.x > lastX) then
                    motionx = speed
                    character.x = character.x + motionx
                elseif(e.x < lastX) then
                    motionx = speed
                    character.x = character.x - motionx
                end
                if(e.y > lastY) then
                else
                    character:setLinearVelocity(0, -200)
                end
            end
            lastX = e.x
            lastY = e.y
        return true
        end
        if(phase == ‘ended’) then
            motionx = 0
        end
    end

    function scene:scrolling(e)
        local scrollView = widget.newScrollView{left = 0, width = 200, height = 0, verticalScrollDisabled = true}
        scrollView:insert(ground1)
        scrollView:insert(ground2)
        scrollView:insert(ground3)
    end
    – INSERT code here (e.g. start timers, load audio, start listeners, etc.)
end
 

Thanks

You are starting your if statement with

if(e.x \> e.xStart) then

So basically you are telling it to execute only if it is supposed to move right.  Try this:

 

local lastX local lastY function group:touch(e) local phase = e.phase if(e.phase == 'began') then lastX = e.x lastY = e.y elseif(e.phase == 'moved') then if(e.x \> lastX) then print("Moving Right") elseif(e.x \< lastX) then print("Moving Left") end if(e.y \< lastY-5) then print("Jumping") else end elseif(phase == 'ended') then print("Stopped") end lastX = e.x lastY = e.y return true end