Portal 2D project

Here is the reworked code for the program for everything but level 2.

http://pastebin.com/H5bZSg9Y

The program breaks when you drag the level portal over the character Sprite.

Clean up level2 the code for level 2. Then we can look at pushing the chest. Things get complicated if you want to push the chest off the ledge. Is that what you are thinking. If so we need to switch from a Sprite (images) to physics.body.

Look up the poly simplify thread for the type of logic we would need to worry about when mixing.

@SkyTheCoder although this is 2D portal. It’s still a little complex for a starter project, especially if using box2D. It’s not that different to the way the game arena wraps in Asteroids, or at the sides of the maze in pacman. It’s generally easier if the character completely disappears on one side of the portal before beginning to appear through the other, because then you don’t have to deal with the problem of the character being in two places at once, both graphically and in the physics engine. This is where it could be tricky to use box2D, as it can get glitchy if you try to set positions of a dynamic body directly (dynamic bodies are meant to be acted on by forces, not teleported). You could temporarily switch the body type to kinematic during the teleport action, and then back to dynamic on the other side. Or you could create a new physics body on the other side, and destroy the one that went into the portal, or possibly some combination of both methods.

@SkyTheCoder Thanks for the video link. It looks interesting, but Chell has a long way to go.

@SkytheCoder I have thought of trying something much simpler, but my 2D portal project has gone a very long way. aside from that, I don’t know where to start.

@dave1707 Portal is a 3D first-person game in which you solve puzzles using a portal gun to pass obstacles and get to an elevator that takes you to the next level. Here’s a video of gameplay, skip a few minutes into it to see what the puzzles are like:

http://youtu.be/0P2dzIa6pZY

@Chell I would highly recommend starting with something much, much simpler.

I now have a new problem, I’m trying to have a player pick up a box when the box is tapped and once the box is tapped again, the box would be dropped. I’m working on gravity after this problem is solved.

I still have the same problem, but no one’s responded to my replies. Hopefully someone could solve my problem of having my box stuck between two portals.

Here is my code

-- My Little Portal Beta Mode
displayMode(FULLSCREEN)
 
function setup()
 --et=elapsed time
 --tc=touch count
    et,tc=0,0
 --px,py = coordinates of orange portal
    px,py=300,400
 --hx,hy = coordinates of blue portal
    hx,hy= 700,300
 --vx,vy = landing place between levels
    vx,vy=200,435
 --sx,sy= coordinates of character
    sx,sy=vx,vy
 -- level transport location
    dx,dy=800,100
 --location of object on level 2
    zx,zy=200,250
    

end
 
function draw()  
    -- taps?
    drawTestPortalMoved()
 
    if not moved then
        drawTestForPlayerInPortal()
    else -- moved set to true
        drawTestPlayerLeftPortal()
    end
	--paint level
    level2()
    -- place player sprite
    sprite("Documents:Chell 2",sx,sy,100)
end

function touched(t)
    if t.state==BEGAN then
        et=ElapsedTime
        tc=t.tapCount
        tx=t.x
        ty=t.y
    elseif t.state==MOVING then
        --Move character
        if t.x>sx-30 and t.x<sx+30 and t.y>sy-30 and t.y<sy+30 then
            sx=t.x
            sy=t.y  
        end                
        --Move cube.  Once Character is over cube then cube & character move together.
         if t.x>zx-30 and t.x<zx+30 and t.y>zy-30 and t.y<zy+30 then
            zx=t.x
            zy=t.y  
        end                  
    elseif t.state==ENDED then
        moved1=false
    end    
end
 
function drawTestPortalMoved()
    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
end
 
function drawTestForPlayerInPortal()
    if px>sx-20 and px<sx+20 and py>sy-20 and py<sy+20 and not moved then
        -- move player to other portal
        sx = hx
        sy = hy
        moved=true
        moved1=true    
    elseif hx>sx-20 and hx<sx+20 and hy>sy-20 and hy<sy+20 and not moved then
        -- move player to other portal    
        sx = px
        sy = py
        moved=true
        moved1=true
    end
end
 
function drawTestPlayerLeftPortal()
    -- if player has left the portals
    if px>sx-20 and px<sx+20 and py>sy-20 and py<sy+20 then
        return
    end      
    if hx>sx-20 and hx<sx+20 and hy>sy-20 and hy<sy+20 then
        return
    end
    -- then    
    moved1=false
    moved=false
end
 
function drawTestForCubeInPortal()
    if px>zx-20 and px<zx+20 and py>zy-20 and py<zy+20 then
        -- move cube to other portal
        zx = hx
        zy = hy
        moved=true
        moved1=true
    elseif hx>zx-20 and hx<zx+20 and hy>zy-20 and hy<zy+20 then
        -- move cube to other portal    
        zx = px
        zy = py
        moved=true
        moved1=true
    end

end
 
function drawTestCubeLeftPortal()
    -- if cube has left the portals
    if px>zx-20 and px<zx+20 and py>zy-20 and py<zy+20 then
        return
    end      
    if hx>zx-20 and hx<zx+20 and hy>zy-20 and hy<zy+20 then
        return
    end
  --   then  -- need seperate variables for character and cube  
-- moved1=false
-- moved=false
end
 
function level2Paint()
    pushMatrix()

	-- white bars
    fill(255, 255, 255, 255)
	-- black outline
    stroke(0, 0, 0, 255)
	-- 3 pixel wide
    strokeWidth(3)
    rect(300,50,500,15)
    rect(100,370,150,15)
    rect(100,220,150,15)
	-- set stroke for ovals and portal
	strokeWidth(2)
	-- orange oval
    fill(181, 141, 64, 255)
    stroke(255, 105, 0, 255)
    ellipse(px,py,60,120)
	-- blue oval
    fill(64, 76, 181, 255)
    stroke(0, 17, 255, 255)
    ellipse(hx,hy,60,120) 
	--portal (not working without level logic)
    fill(47, 47, 47, 255)
    stroke(255, 255, 255, 255)
    ellipse(dx,dy,120,120)        

    popMatrix()
end
 
function level2()  
    background(50, 61, 130, 255)
	level2Paint()
	-- this sprite only defined in level functions it is called in
     sprite("Documents:Cube",zx,zy,50,50)
	 
    --in Portal?   
    if dx>sx-20 and dx<sx+20 and dy>sy-20 and dy<sy+20 then
        sx = vx
        sy = vy
        
        --in case xport elipse overlaps portals
        moved=true
        moved1=true     
    end
	
	-- when character and object are in the portal at the same time moved variables get messed up
    if not moved then
        drawTestForCubeInPortal()
    else -- moved set to true
        drawTestCubeLeftPortal()
    end
end

It’s been a very long while since the last person replied to anything on this thread, even if it was a move on to something else response. But I still work on my project alone as I used to do awhile back. But I wish someone could help me now with all of the small and short problems in the now seven levels of my game.

@Chell I don’t know what you want. I’ve already given you several examples of moving a character from portal to portal going to different levels.

Actually, I’ve moved on from the portals and onto the game’s physics. I need to know how to create a button than activates the door’s teleport function.

I’ve been trying really hard to work on the problem on my own. But with nfgdayton missing I’m having a hard time solving the problem I’ve stated. So can you please help me?

Just create a button on the screen. When it’s touched, set a flag to true. When it’s touched again, set the flag to false. If the flag is true, allow what ever function the portal does to execute. If it’s false, then don’t allow what ever function the portal does.

I’m having some trouble. I’m trying to create a function where you place the sprite on a red rectangle (or the weighted cube on the button), the door function should be set to true. But this is only working on my second level where this function is first used. Here is my code.

function level2()
    background(50, 61, 130, 255)
    level2Paint()
    
    
    
if zx>ex and ey<zy and dx>sx-20 and dx<sx+20 and dy>sy-20 and dy<sy+20 then
        sx = vx
        sy = vy
        lv=lv+1
        
    
        
    end
    
    
    
    
    
sprite("Documents:Cube",zx,zy,50,50)

     
    

     --in Portal?   
    if dx>sx+20 and dx<sx+20 and dy>sy-20 and dy<sy+20 then
        sx = vx
        sy = vy
        
        --in case xport elipse overlaps portals
        moved=true
        moved1=true  
    end
	end
    
	-- when character and object are in the portal at the same time moved variables get messed 
    
    
    
    function level3Paint()   
    pushMatrix()
    fill(255, 255, 255, 255)
    stroke(0, 0, 0, 255)
    strokeWidth(3)
    rect(0,50,2000,15)
    popMatrix()
    pushMatrix()
    fill(255, 255, 255, 255)
    stroke(0, 0, 0, 255)
    strokeWidth(3)
    rect(0,370,2000,15)
    popMatrix()
    pushMatrix()
    rotate(90)
    fill(255, 255, 255, 255)
    stroke(0, 0, 0, 255)
    strokeWidth(3)
    rect(50, -500,2000,15)
    popMatrix()
    pushMatrix()
    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)        
    fill(47, 47, 47, 255)
    stroke(255, 255, 255, 255)
    strokeWidth(2)
    ellipse(dx,dy,120,120) 
    popMatrix()
    pushMatrix()
    fill(255, 0, 0, 255)
    stroke(0, 0, 0, 255)
    strokeWidth(1)
    rect(lx,ly,80,15)
    popMatrix()
    pushMatrix()
    
     
    
    
end
function level3()   
    background(66, 78, 120, 255)
    level3Paint()
    
    
    
    if fx<lx and ly>fy and dx<sx-20 and dx>sx+20 and dy<sy-20 and dy>sy+20 then
        sx = vx
        sy = vy
        lv=lv+1
        
    
        
    end
    
    
    
    
    
sprite("Documents:Cube",fx,fy,50,50)

     
    


	end