Finally im working on a project

So heres the case im creating a game with some of my friends, however ive run into a bit of a problem, without further ado heres the code:

-- TheAdventuresOfRocky

function setup()
    displayMode(FULLSCREEN)
    print("Hello World!")
    bounce = .1
    rx = WIDTH/2 -- the x value
    ry = HEIGHT/2 -- the y value
    rad = 30
    rocky = physics.body(CIRCLE, rad)
    rocky.x = rx
    rocky.y = ry
    rocky.gravityScale = 1
    ground = physics.body(EDGE, vec2(0,105),
                                vec2(WIDTH*2,105))
    ground.type = STATIC
    ground.restitution = bounce
    move = 0
    touches = {}
    jump = 0
    tx = WIDTH/2
    ty = HEIGHT-50
end

function draw()
    background(40, 40, 50)
    strokeWidth(5)
    ellipse(rocky.x,rocky.y,rad)
    spriteMode(CORNER)
    if CurrentOrientation == LANDSCAPE_LEFT or LANDSCAPE_RIGHT then
        for i=0,10 do
            sprite("Planet Cute:Brown Block",i*101,0)
        end
    elseif CurrentOrientation == PORTRAIT or PORTRAIT_UPSIDE_DOWN then
        for i=0,8 do
            sprite("Planet Cute:Brown Block",i*101,0)
        end
    end
    rocky.x = rocky.x + move
    rocky.y = rocky.y + jump
    textSize(60)
    font("AmericanTypewriter")
    fontSize(60)
    text("i fell from a volcano",tx,ty)
end

function touched(touch)
    if touch.state == BEGAN and touch.x > WIDTH/2 + 60 and touch.y < 105 then
        move = 10
        touches[touch.id] = touch
    end
    if touch.state == BEGAN and touch.x < WIDTH/2 - 60 and touch.y < 105 then
        move = -10
        touches[touch.id] = touch
    end
    if touch.state == ENDED then
        move = 0
    end
end

function collide(contact)
end

--[[--============================================================
                                                      This code is by dave1707 from Codea Forums
           This code demonstrates how to scroll the screen based on the position of a circle
function setup()
    displayMode(FULLSCREEN)
    gdx=WIDTH/2    -- grid position
    gdy=HEIGHT/2
    dx=0           -- circle position
    dy=HEIGHT/2
end

function draw()
    background(40, 40, 50)
    -- scroll the grid if the circle is off the screen
    if dx>WIDTH-50-gdx then
        gdx=gdx-30
    elseif dx<gdx*-1+50 then
        gdx=gdx+30
    end
    translate(gdx,400)    -- move grid and circle
    fill(255) 
    ellipse(dx,100,100,100)    -- move just the circle
    stroke(255)
    strokeWidth(4) 
    fontSize(30)
    for x=-3000,3000,100 do
        line(x,-3000,x,3000)
        text(x,x,0)
    end
    for y=-3000,3000,100 do
        line(-3000,y,3000,y)
        text(y,0,y)
    end
end

function touched(t)
    if t.state==BEGAN then
        y1=t.y
    elseif t.state==MOVING then
        dx=dx+t.y-y1
        y1=t.y
    end
end
--]]--==================================================

@dave1707 thanks for the sidescroller code
When the user taps the bottom right or bottom left, rocky moves to the respective location, although i want rocky to “jump” when the user taps the screen, another thing i cant seem to do is to scroll the screen @dave1707 i think i may have just disgraced you, anyways if i could get some insight (not necesarrily code) on how to do those things that’d be great, thanks in advance

just to recap: need help to understand how to organise touches based on their id and i need an explanation of how the side scrollikng codeuld be applied to rocky once again thanks in advance.

@Dalorbi

I have to go shopping today (black Friday), so I’m not sure how soon I’ll be able to help you with the side scrolling. I’m sure it will be sometime today.

@Dalorbi

I added bgX = 0 in setup() and 7 lines in draw() following background(40,40,50) to handle the background scrolling. Touching the bottom left or right of the screen will move rocky, but once rocky reaches the screen edge, the background will scroll. As for the jump, I added jump=0 after rocky.y=rocky.y+jump in draw() and two jump=60 in function(touch). You can change the value for jump to suit your needs. I also added rocky.sleepingAllowed=false in setup() so rocky doesn’t go to sleep once he reaches the bottom of the screen.


function setup()
    displayMode(FULLSCREEN)
    bgX=0    -- background x position
    bounce = .1
    rx = WIDTH/2 -- the x value
    ry = HEIGHT/2 -- the y value
    rad = 30
    rocky = physics.body(CIRCLE, rad)
    rocky.x = rx
    rocky.y = ry
    rocky.gravityScale = 1
    rocky.sleepingAllowed=false
    ground = physics.body(EDGE, vec2(0,105),
                                vec2(WIDTH*2,105))
    ground.type = STATIC
    ground.restitution = bounce
    move = 0
    touches = {}
    jump = 0
    tx = WIDTH/2
    ty = HEIGHT-50
end

function draw()
    background(40, 40, 50)
    
    -- keep the circle on the screen, but calculate the background x position
    if rocky.x > WIDTH - rad/2 - bgX then
        bgX = WIDTH - rad/2 - rocky.x
    elseif rocky.x < bgX * -1 + rad/2 then
        bgX = rad/2 - rocky.x
    end  
    translate(bgX,0)    -- move the background left or right
         
    strokeWidth(5)
    ellipse(rocky.x,rocky.y,rad)
    spriteMode(CORNER)
    if CurrentOrientation == LANDSCAPE_LEFT or LANDSCAPE_RIGHT then
        for i=0,10 do
            sprite("Planet Cute:Brown Block",i*101,0)
        end
    elseif CurrentOrientation == PORTRAIT or PORTRAIT_UPSIDE_DOWN then
        for i=0,8 do
            sprite("Planet Cute:Brown Block",i*101,0)
        end
    end
    rocky.x = rocky.x + move
    rocky.y = rocky.y + jump
    jump=0
    textSize(60)
    font("AmericanTypewriter")
    fontSize(60)
    text("i fell from a volcano",tx,ty)
end

function touched(touch)
    if touch.state == BEGAN and touch.x > WIDTH/2 + 60 and touch.y < 105 then
        move = 10
        jump = 60
        touches[touch.id] = touch
    end
    if touch.state == BEGAN and touch.x < WIDTH/2 - 60 and touch.y < 105 then
        move = -10
        jump = 60
        touches[touch.id] = touch
    end
    if touch.state == ENDED then
        move = 0
    end
end

function collide(contact)
end

Thanks dave, your a life saver.