Having the screen move with an object.

My objective is to have the screen move with my rectangle object.

I know that this discussion has been posted before, but I can’t follow the exmaples listed—especially the Lua Jump code for this. I have tried using CurrentTouch to control my x and y variables, but it is really jerky as I use if statements to check all four directions.

I have a quick scenario below if anyone knows how to make this happen. And sorry about the formatting, I don’t know how to make the blue box show all the code.


function setup()

    -- this sets up the position of the boxes in a table

    table1 = {}

    for i=1, 100 do
        table.insert(table1, vec2(WIDTH/2, -225+i*100))
    end

    for i=1, 100 do
        table.insert(table1, vec2(-225+i*100, WIDTH/2))
    end

end

-- This function gets called once every frame

function draw()

    background(255, 255, 255, 255)

    -- this allows the boxes to be displayed on the screen

    for a,b in pairs(table1) do

        sprite("Cargo Bot:Crate Blue 2", b.x, b.y)

    end

    -- this rectangle is the figure I wish to have the screen focused on, so any movememt will cuase the screen to move a particular way.

    rect(CurrentTouch.x,CurrentTouch.y,50,50)

end

@YoloSwag Try this example. Move your finger on the screen to move the character.

EDIT:Code was removed. I found the original code I posted. See the link below.

@YoloSwag Here’s your code that I modified.


displayMode(FULLSCREEN)

function setup()
    rectMode(CENTER)
    speed=10
    w2=WIDTH/2
    h2=HEIGHT/2
    gdx,gdy=0,0
    dx,dy=WIDTH/2,HEIGHT/2
    move=false
    table1 = {}
    for i=-250,250 do
        table.insert(table1, vec2(WIDTH/2,i*100))
        table.insert(table1, vec2(i*100, WIDTH/2))
    end
end

function draw()
    background(40, 40, 50)
   
    if move then    -- move the rect, change speed if needed
        dx=dx-(sx-mx)/speed
        dy=dy-(sy-my)/speed
    end
    
    -- scroll the grid and character left or right
    if dx>WIDTH-50-gdx then
        gdx=WIDTH-50-dx
    elseif dx<gdx*-1+50 then
        gdx=50-dx
    end  
    
    -- scroll the grid and character up or down
    if dy>HEIGHT-50-gdy then
        gdy=HEIGHT-50-dy
    elseif dy<gdy*-1+50 then
        gdy=50-dy
    end  

    pushMatrix()
    translate(gdx,gdy)    -- move grid and character    
    for a,b in pairs(table1) do
        sprite("Cargo Bot:Crate Blue 2", b.x, b.y)
    end
    rect(dx,dy,50,50)
    popMatrix()
end

function touched(t)
    if t.tapCount==2 then    -- double tap to move character back to center position
        dx=WIDTH/2
        dy=HEIGHT/2
        gdx=0
        gdy=0
    end
    if t.state==BEGAN then    -- finger starting x,y position
        sx=t.x
        sy=t.y
    elseif t.state==MOVING then    -- finger direction to move character
        move=true
        mx=t.x
        my=t.y
    elseif t.state==ENDED then    -- finger raised, stop moving character
        move=false
    end
end

@YoloSwag To format the code, put 3 ~ on a separate line before and after your code. I added them in your code above.

@YoloSwag If you want to take this code and modify it for your own, it’s yours. It was originally posted here for anyone to take.

http://codea.io/talk/discussion/2166/scrolling-game-starter/p1