Portal 2D project

@Chell You should be doing it yourself, but from the code you had above, it looks like you don’t understand how Codea works. I’m just giving you examples so you can see that a lot of things can be done with very little code. You just need to understand how things work and that’s done by writing small programs and building on them. Everything that you want can be done with Codea, it just takes time to learn how to do it. When you have questions, we’re here to help.

Thank you, now I just need to know where to go to learn more.

I left you a link above.

@Ignatz The link isn’t working

@Chell Try the Wiki link at the top of the forum.

@Ignatz When I checked the Wiki link above, I noticed your link to a shader ebook that I haven’t seen before. I’ll have to read thru it.

The link works now (I hate autocorrect, it mangled a tag), but here it is anyway

https://www.dropbox.com/sh/mr2yzp07vffskxt/AACqVnmzpAKOkNDWENPmN4psa

Don’t forget, read Lua first, then Codea (Codea is built using Lua).

@Ignatz Thank you for your help!

Okay, I’ve finished the Lua manual and started the third chapter of Codea. But now I’ve run into another problem. One of my portals won’t work. Here is my code:

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
   
    
    px,py=300,400
    hx,hy= 100,400
    sx,sy=200,380
    
    
end

function draw()
    background(40, 40, 50)
    fill(181, 141, 64, 255)
    stroke(255, 105, 0, 255)
    strokeWidth(2)
    ellipse(px,py,60,120)
     fill(64, 76, 181, 255)
    stroke(0, 17, 255, 255)
    strokeWidth(2)
    ellipse(hx,hy,60,120)   
    stroke(255)
    line(50,340,350,340)
    line(700,240,900,240)
    
    
    sprite("Documents:Chell",sx,sy,100,100)
    
    
    if px>sx-20 and px<sx+20 and py>sy-20 and py<sy+20 then
        sx = hx
        sy = hy
        
    
    
    
        
  end
   
end

function touched(t)
    
    if t.tapCount== 3 then
        px=t.x
        py=t.y
end
      if t.state== MOVING then
            sx=t.x
            sy=t.y
        end
    if t.tapCount== 2 then
        hx=t.x
        hy=t.y
end
    end

@Chell Your tapCount of 2 will always execute if you try to tap 3 times.

I know tht, but I just need to know how to teleport through the blue portal as well as the Orange. I tried something like this:

elseif hx+20>sx-20 and hx-20<sx+20 and hy+12>sy-20 and hy-20<sy+20 then
sx = py
sy = px

@Chell Try this code. You have to be touching the Sprite to move it. You can move thru either portal. Tap 2 time or 3 times to move the portals.

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    et,tc=0,0
    px,py=300,400
    hx,hy= 700,300
    sx,sy=200,380
end

function draw()
    background(40, 40, 50)
    fill(181, 141, 64, 255)
    stroke(255, 105, 0, 255)
    strokeWidth(2)
    ellipse(px,py,60,120)
    fill(64, 76, 181, 255)
    stroke(0, 17, 255, 255)
    strokeWidth(2)
    ellipse(hx,hy,60,120)   
    stroke(255)
    line(50,340,350,340)
    line(700,240,900,240)
    if ElapsedTime-et>.2 and tc>0 then
        if tc==2 then
            hx=tx
            hy=ty
        elseif tc==3 then
            px=tx
            py=ty
        end
        tc=0
    end
    sprite("Planet Cute:Character Horn Girl",sx,sy,100,100)
    if not moved then
        if px>sx-20 and px<sx+20 and py>sy-20 and py<sy+20 then
            sx = hx
            sy = hy
            moved=true
            moved1=true
        elseif hx>sx-20 and hx<sx+20 and hy>sy-20 and hy<sy+20 then
            sx = px
            sy = py
            moved=true
            moved1=true
        end
    end
    if moved then
        if px>sx-20 and px<sx+20 and py>sy-20 and py<sy+20 then
            -- still in the portal
        elseif hx>sx-20 and hx<sx+20 and hy>sy-20 and hy<sy+20 then
            -- still in the portal
        else
            moved1=false
            moved=false
        end
    end
end

function touched(t)
    if t.state==BEGAN then
        et=ElapsedTime
        tc=t.tapCount
        tx=t.x
        ty=t.y
    end
    if t.x>sx-30 and t.x<sx+30 and t.y>sy-30 and t.y<sy+30 then
        if t.state==MOVING and not moved1 then
            sx=t.x
            sy=t.y   
        end     
    end
    if t.state==ENDED then
        moved1=false
    end    
end

Thank you for your help. Sorry that I got so impatient.

@Chell I don’t know if you can use this or not, but I thought I’d post it anyways. It starts with 2 portals and double tapping the screen creates another portal at that point that links randomly to one of the previous portals. There isn’t any limit to the number of portals created.

displayMode(FULLSCREEN)

function setup()
    portals={}
    table.insert(portals,vec3(100,300,2))
    table.insert(portals,vec3(800,500,1))
    sx,sy=WIDTH/2,HEIGHT/2
end

function draw()
    background(40, 40, 50)
    fill(181, 141, 64, 255)
    stroke(255, 105, 0, 255)
    strokeWidth(2)
    for a,b in pairs(portals) do
        ellipse(b.x,b.y,60,120)
    end
    sprite("Planet Cute:Character Horn Girl",sx,sy,100,100)
    if not moved then
        for a,b in pairs(portals) do
            if sx>b.x-20 and sx<b.x+20 and sy>b.y-20 and sy<b.y+20 then
                sx=portals[b.z].x
                sy=portals[b.z].y
                moved=true
                moved1=true
                break
            end
        end
    end    
    if moved then
        outOfPortal=true
        for a,b in pairs(portals) do
            if sx>b.x-20 and sx<b.x+20 and sy>b.y-20 and sy<b.y+20 then
                outOfPortal=false
            end
        end
        if outOfPortal then
            moved=false
            moved1=false
        end
    end
end

function touched(t)
    if t.state==BEGAN and t.tapCount==2 then
        table.insert(portals,vec3(t.x,t.y,math.random(#portals)))
    end
    if t.x>sx-30 and t.x<sx+30 and t.y>sy-30 and t.y<sy+30 then
        if t.state==MOVING and not moved1 then
            sx=t.x
            sy=t.y   
        end     
    end
    if t.state==ENDED then
        moved1=false
    end    
end

I messed with the code above and see the purpose of the moved Boolean. I took out the checks for position and watched the Sprite flicker between the portals. However, I don’t see the purpose of the moved1 Boolean. The sx and sy parameters get changed in draw and you set moved1 to true. Dave set moved1 to false when the touch ends. … Aha … I had commented out the lines setting moved1 to true and nothing bad happened. I kept my finger on the screen and moved it over to the other portal and the Sprite transported. Moved1 says you need to end the touch before you can transport again.

I put the portals right next to each other, and without moved1 and the transport took place as long as my finger kept the Sprite in the sweet spot of the portal. When the portals are far apart not having moved1 is seems like a feature. When they are close it is not clear if Sprite moved or transported.

I like how Dave prevented this ambiguity.

It works pretty well, but how do I apply gravity to this? If you can just move the character from one platform to another without gravity it defeats the purpose of the portals.

@Chell In your Nov 8 post above, you said the character is controlled by your touch. Now you want it controlled by gravity. Lookup the gravity command and replace the touch movement with the gravity movement.

Actually, I want both.

I think now I’ve handled the gravity, how do I create new levels?

@Chell, please first search the forum for questions like this, which have been answered many times.

While we’re happy to help solve problems, we don’t have time to provide personal tuition.