Portal 2D project

I’ve been having some problems with my project. My character can’t move when I place a portal let alone travel through it. Here is my code:

-- My Little Portal Demo 0.87

-- Simple Physics

-- Use this function to perform your initial setup
function setup()

    
touches = {}




function touched(touch)
    -- When you touch the screen, create a random portal
function draw()
    -- Store current style
         

    function touched(touch)
        function draw()
    ellipse(CurrentTouch.x, CurrentTouch.y, 70, 125)
    stroke(0, 75, 255, 255)
    strokeWidth(4)
    fill(118, 190, 211, 255)
    end
    
           function touched(touch)
        function draw()
    ellipse(CurrentTouch.x, CurrentTouch.y, 70, 125)
    stroke(255, 128, 0, 255)
    strokeWidth(4)
    fill(211, 162, 118, 255)
    end
    end
end
    end
        end

    -- Table to store our physics bodies
    bodies = {}
    
    -- Create some static boxes (not effected by gravity or collisions)
    local left = makeBox(WIDTH/4, HEIGHT/2, WIDTH/3, 10, 0)
    left.type = STATIC
    
    local right = makeBox(WIDTH - WIDTH/4, HEIGHT/2, WIDTH/3, 10, 0)
    right.type = STATIC
    
    table.insert(bodies, left)
    table.insert(bodies, right)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color
    background(49, 49, 84, 255)
    
    -- Draw all our physics bodies
    for k,body in pairs(bodies) do
        drawBody(body)
    end
    
      for i = 1,8 do
            sprite("Documents:Floor", 0, 0, 10000, 200)
        
        
    

    end
    
         parameter.number("X", 0, WIDTH, X)
    
sprite("Documents:Chell", X, 110, 200, 200)
    
    end
function touched(touch)
    -- If the user drags their finger we'll
    --  update the number slider controlling 'X'
    -- This shows how code can affect parameter widgets
    if touch.state == MOVING then
        X = touch.x
    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

-- Helper function to draw a physics body
function drawBody(body)
    -- Push style and transform matrix so we can restore them after
    pushStyle()
    pushMatrix()
    
    strokeWidth(5)
    stroke(255, 255, 255, 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
    elseif body.shapeType == CHAIN or body.shapeType == EDGE then
        strokeWidth(3.0)
        local points = body.points
        for j = 1,#points-1 do
            a = points[j]
            b = points[j+1]
            line(a.x, a.y, b.x, b.y)
        end
    elseif body.shapeType == CIRCLE then
        strokeWidth(3.0)
        line(0,0,body.radius-3,0)
        ellipse(0,0,body.radius*2)
    end
    
    -- Restore style and transform
    popMatrix()
    popStyle()
    

end

@Chell You have 4 draw() and 4 touched() functions. The last ones executed will be the ones used, so unless you understand what can happen, there should be just one of each. Also, can you explain what you’re trying to do in your program.

@Chell - I’m surprised that Codea doesn’t explode, because you have about 6 different copies of the touched and draw functions nested inside each other and formatting like this makes it impossible to read.

end
    end
end
    end
        end

Basically, there is so much wrong, that there is little point trying to debug it. (That’s not meant to be insulting - Codea is a bit unusual, and I wrote some horrible code when I first started).

I suggest you do some reading of the help material and tutorials on Codea - I have some ebooks here - start with Lua first, then Codea.

What I’m trying to do is recreate portal in codea. Also, that’s the sum of all the studying I’ve done so far.

@dave1707 I tried removing the one of each but it gives me the error table expected got nil at line 56.

@Ignatz
Your link isn’t working for some reason.

Sorry, but I don’t know what Portal is (a game I assume) or what you are trying to do in your code above. I’m sure there’s a big difference in what the game does and what you’re actually doing.

You aren’t going to be able to recreate anything until you learn more about how to program in Codea.

You need to start very, very small, learning how the language works and writing extremely simple programs. Trying to write portal comes a lot later.

@dave1707
You might remember it’s sequal more, portal 2.
I’m trying to create a level where you go from point a to point b. Problem is that my charachgter stops moving when a portal is placed.

@Ignatz
I actually took three coding classes, two for Html and Css, and one for Java. I just can’t figure out which one to use.

Answer - none of them.

Codea is different.

@Chell I don’t play games, so Portal 2 doesn’t help. Looking at your code, you’re creating physics objects and I’m not sure you need to. If you want a player to move thru a portal and appear in another part of the play area, you don’t need physics.

PS I did a google on a Portal.

@Chell I tried your program, but I’m not sure what’s happening. I can move something back and forth with the parameter slider, or I can move an ellipse around the screen, but I’m not sure what the 2 skinny rectangles are for. I’d like to give you more info to help, but I don’t know where to start.

@dave1707 The two skinny rectangles are platforms, and you can only move left toright. So you have to get to the platform on the right using portals.You tap the screen to create a portal and the slider controls the charachter.

@Chell This isn’t what your program is doing, but is this something like what you want. Move your finger on the screen to move the character thru the portal.

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    px,py=100,400
end

function draw()
    background(40, 40, 50)
    fill(223, 214, 162, 255)
    stroke(255,0,0)
    strokeWidth(2)
    ellipse(300,400,60,120)
    ellipse(800,300,60,120)
    stroke(255)
    line(50,340,350,340)
    line(700,240,900,240)
    sprite("Platformer Art:Guy Standing",px,py)
    if px>280 and px<320 and py>380 and py<420 then
        px=800
        py=300
    end
end

function touched(t)
    if t.state==MOVING then
        px=px+t.deltaX
        py=py+t.deltaY
    end
end

@dave1707
Thank you

@dave1707 I only have one last question, how do I move the portals?

@Chell Right now nothing is coded to move them. Is the character supposed to move randomly and you have to move the portal for it to go thru.

@dave1707 Actually, you have to solve a puzzle to get from one place to another using portals. But that’s kind of the idea.

@Chell Tap the screen to place the portal where you touch.

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    px,py=100,400
    sx,sy=WIDTH/2,HEIGHT/2
    c=300
end

function draw()
    background(40, 40, 50)
    fill(223, 214, 162, 255)
    stroke(255,0,0)
    strokeWidth(2)
    ellipse(px,py,60,120)
    ellipse(800,300,60,120)
    stroke(255)
    line(700,240,900,240)
    sprite("Platformer Art:Guy Standing",sx,sy)
    if px>sx-20 and px<sx+20 and py>sy-20 and py<sy+20 then
        sx=800
        sy=300
    end
    c=c+1
    if c>200 then
        dx=math.random(-3,3)
        dy=math.random(-3,3)
        c=0
    end
    sx=sx+dx
    sy=sy+dy
    if sx>WIDTH then
        sx=0
    end
    if sx<0 then
        sx=WIDTH
    end
    if sy>HEIGHT then
        sy=0
    end
    if sy<0 then
        sy=HEIGHT
    end
end

function touched(t)
    if t.state==BEGAN then
        px=t.x
        py=t.y
    end
end

@dave1707 Thanks for being such a big help. But I didn’t mean for the player to be bouncing around. I meant for him to be controlled by your touch and you double tap for a portal. But that’s probably asking for too much. I probably should be doing this myself.