Have a problem with 2D platformer

Basic 2D Platformer.zip (89.9 KB)
I have a problem with my 2D platformer game. The player sometimes got stuck in the mid air.
I think it’s because of the ground tile physics, but I’m not sure what to modify…

I’m not getting the player stuck in the air like it was before. If the player runs all the way left and runs into the edge, then when it goes right, it gets stuck trying to go left again.

Some changes to the code.

Add viewer.mode=FULLSCREEN before setup() to start the game in FULLSCREEN.

The below code is for the reverse of the player.

I sent you info in MESSAGE, don’t know if you ever looked at it.

    else
    -- Draw the player when facing left
    -- I have only player sprites that are facing right,
    --so I'll basically flip the sprite sidewards
        if self.physicsBody.linearVelocity == vec2(0, 0) then -- Idle state
            sprite(self.idleAnimationFrames[self.idleAnimationCurrentFrame], self.physicsBody.position.x, self.physicsBody.position.y, -80, 80) 
        elseif self.physicsBody.linearVelocity.x < 0 and self.physicsBody.linearVelocity.y == 0 then -- Moving left
            sprite(self.runningAnimationFrames[self.runningAnimationCurrentFrame], self.physicsBody.position.x, self.physicsBody.position.y, -80, 80)
        elseif self.physicsBody.linearVelocity.y > 0 then -- Jumping
            sprite(self.jumpingSprite, self.physicsBody.position.x, self.physicsBody.position.y, -80, 80)
        elseif self.physicsBody.linearVelocity.y < 0 then -- Falling
            sprite(self.fallingSprite, self.physicsBody.position.x, self.physicsBody.position.y, -80, 80)
        end
    end

Thanks this should optimize the performance

Your table can get very large if you have a wide ground area. Here’s some code that will let you draw a very wide ground with not much table size. The table data is in pairs. The first number is a count and the second is the table image. So a pair of 20,1 will draw 20 images of image 1.

viewer.mode=FULLSCREEN

function setup()
    img={}
    img[1]=readImage(asset.builtin.Platformer_Art.Block_Grass)
    img[2]=readImage(asset.builtin.Platformer_Art.Block_Brick)
    img[3]=readImage(asset.builtin.Platformer_Art.Block_Special_Brick)
    img[4]=readImage(asset.builtin.Platformer_Art.Block_Special)
    img[5]=readImage(asset.builtin.Platformer_Art.Crate)
    img[6]=readImage(asset.builtin.Platformer_Art.Crate)
    groundWidth=33
    tab={   -- groups of 2 (number of images to draw, image table nbr)
        20,1,4,0,9,1,       -- sum of images should equal ground width
        20,2,4,0,9,2,
        20,2,4,0,9,2,
        1,2,18,2,1,3,3,0,1,1,8,2,1,3,
        1,4,18,5,1,6,3,0,1,4,8,5,1,6,
        1,4,18,5,1,6,3,0,1,4,8,5,1,6,
        1,4,18,5,1,6,3,0,1,4,8,5,1,6,
        1,4,18,5,1,6,3,0,1,4,8,5,1,6,}
    size=25
end

function draw()
    background(0)
    drawGround()
end

function drawGround()
    x,y=1,1
    for z=1,#tab,2 do
        imgNbr=tab[z+1]
        for a=1,tab[z] do
            if imgNbr>0 then
                sprite(img[imgNbr],x*size,500-y*size,size,size)
            end
            x=x+1
            if x>groundWidth then
                x=1
                y=y+1
            end
        end
    end    
end

To be honest, I don’t understatnd all the code… But I’ll try. Thanks for providing me the more simplified code!

Ok I understanded all the code you provided, and managed to add physics body to each tile. But the problem remains…

I couldn’t get the player to hang in the air, but I did have a problem when the player went all the way left, then right, but got stuck going left again.