Gravity problem

Hey guys, I’ve got a problem with my code.
I’m a total beginner, :slight_smile: but I’ve started a project. There you should change the gravity of the game by taping on the screen.
But my problem is, that the actor, which should move with the gravity doesn’t move and if you tap on the screen the whole App (Codea) crashes. Maybe you can detect my mistake. Thank you very much :slight_smile:

-- ASLO

-- Use this function to perform your initial setup

supportedOrientations( PORTRAIT_ANY )


function setup()
    
       -- Value for flydirection
        value=2
    
        if value == 2 then
        
        physics.gravity( 1, 0)
            value=1
        
        else
        
        
        physics.gravity( -1, 0)
            value=2
            
    end
    
    --Set the viewer to fullscreen
    --Hide back/pause/play buttons
    displayMode( FULLSCREEN )
    
    -- Table to store our physics bodies
    bodies = {}
    boder = {}
    
    -- Create some static boxes (not effected by gravity or collisions)
    local left = makeBox(150, 512, 1040, 1, 90)
    left.type = STATIC
    
    local right = makeBox(600, 512, 1040, 1, -90)
    right.type = STATIC
    
    local mid = makePlay ( 384, 200, 20, 20, 90)
    mid.type = DYNAMIC
    
    table.insert(bodies, left)
    table.insert(bodies, right)
    table.insert(boder, mid)
end


-- This function gets called once every frame
function draw()
    -- This sets a dark background color
    background(31, 31, 31, 255)
    
    -- Draw all our physics bodies
    for k,body in pairs(bodies) do
        drawBody(body)
    end
    
    for k,body in pairs(boder) do
        drawPlay(body)
    end
    
end

function touched(touch)
    -- When you touch the screen, create a random box
    if touch.state == BEGAN then
        if value == 2 then
        physics.gravity( -300, 0)
            value=1
        
        else
        physics.gravity( 300, 0)
            value=2
            
    end
end
    end

-- Helper function to create a box using a polygon body
function makeBox(x,y,w,h,r)
    -- Points are defined in counter-clockwise order
    local body = physics.body(POLYGON,vec2(-w/2, h/2),
    vec2(-w/2, -h/2), vec2(w/2, -h/2), vec2(w/2, h/2))
    
    
    -- Set the body's transform (position, angle)
    body.x = x
    body.y = y
    body.angle = r
    
    -- Make movement smoother regardless of framerate
    body.interpolate = true
    
    return body
end

function makePlay(x,y,w,h,r)
    -- Points are defined in counter-clockwise order
    local body = physics.body(POLYGON,vec2(-10, 10),
    vec2(-10, -10), vec2(10, -10), vec2(10, 10))
    
    
    -- Set the body's transform (position, angle)
    body.x = x
    body.y = y
    body.angle = r
    
    -- Make movement smoother regardless of framerate
    body.interpolate = true
    
    return body
end

-- Helper function to draw a physics body
function drawBody(body)
    -- Push style and transform matrix so we can restore them after
    pushStyle()
    pushMatrix()
    
    strokeWidth(1)
    stroke(134, 162, 127, 255)
    fill(132, 130, 130, 255)
    translate(body.x, body.y)
    rotate(body.angle)
    
    -- Draw body based on shape type
    if body.shapeType == POLYGON then
        strokeWidth(3.0)
        local points = body.points
        for j = 1,#points do
            a = points[j]
            b = points[(j % #points)+1]
            line(a.x, a.y, b.x, b.y)
        end
    
    -- Restore style and transform
    popMatrix()
    popStyle()
    end
end

function drawPlay(body)
    -- Push style and transform matrix so we can restore them after
    pushStyle()
    pushMatrix()
    
    local body = physics.body(POLYGON,vec2(-10, 10),
    vec2(-10, -10), vec2(10, -10), vec2(10, 10))
    
    body.type = DYNAMIC
    
    sprite("Documents:spcshi", 384, 200, 20, 20)
    translate(body.x, body.y)
    rotate(body.angle)
    
    -- Draw body based on shape type
    if body.shapeType == POLYGON then
        local points = body.points
        for j = 1,#points do
            a = points[j]
            b = points[(j % #points)+1]
            line(a.x, a.y, b.x, b.y)
        end
    
    -- Restore style and transform
    popMatrix()
    popStyle()
    end
end

@hallomio77 Replace your drawPlay with this one. Also, add physics.continuous=true as the first line in function setup().

function drawPlay(body)
    -- Push style and transform matrix so we can restore them after
    pushStyle()
    pushMatrix()
    --local body = physics.body(POLYGON,vec2(-10, 10),
    --vec2(-10, -10), vec2(10, -10), vec2(10, 10))
    --body.type = DYNAMIC
    translate(body.x, body.y)
    rotate(body.angle)
    sprite("Platformer Art:Battor Flap 1",0,0, 20, 20)
    -- Draw body based on shape type
    if body.shapeType == POLYGON then
        local points = body.points
        for j = 1,#points do
            a = points[j]
            b = points[(j % #points)+1]
            line(a.x, a.y, b.x, b.y)
        end
    -- Restore style and transform
    popMatrix()
    popStyle()
    end
end

EDIT: I replaced your sprite with a built in sprite since I didnt have access to your sprite.

Thank you very much :slight_smile: now everything works pervect. That very nice of you :slight_smile: