Log In  

im so close to finishing my biggest project yet, but another bug comes up!
anyboy know why the player wont jump?

function _update60()
 cls()
 if(btnp(⬆️))then
        yv=5
    end
    if(btn(➡️))then
        xv+=s
    end
    if(btn(⬅️))then
        xv-=s
    end
    if(xv>mxv)then
        xv=mxv
    end
    if(xv<0-mxv)then
        xv=0-mxv
    end
    yv+=g
    y+=yv
    x+=xv
    if(xv>0)then
        xv-=f
        if(xv<0)then
         xv=0
     end
    end
        if(xv<0)then
        xv+=f
        if(xv>0)then
         xv=0
     end
    end
    if(y>120)then
        y=120
    end
    spr(skin,x,y)
end
P#129904 2023-05-19 08:49

Unsure what your g variable is set to, but it looks like you’re adding or subtracting the wrong way around.

Assuming g is positive (so adding it moves the player down the screen), you therefore need:
y -= yv
So when yv = 5, the above line will instead move the player up the screen by 5 pixels.

Hope it helps!

P#129907 2023-05-19 10:23

@someguy17 its still broken, i think i already tried that one time, ill wait for more

P#129911 2023-05-19 11:57

It may be because you're trying to draw in the _update function instead of the _draw function. This might work if you only have _update with no _draw but if you also have a draw function and, in particular, if it clears the screen then your code may very well be working as intended but you're just not seeing the results because you're immediately redrawing over top of them.

Try moving the cls() and spr() calls to _draw and see if anything changes.

P#129912 2023-05-19 12:15

For logic errors, try adding print statements to see what your variables are doing when you run the cart. Whatever feedback they give should give you a hint, or at least tell you what's working as intended.

P#129916 2023-05-19 12:43
2

The issue is actually what people above said. You're adding your instantaneous jump velocity in the same direction as gravity. You really only need to make jump velocity negative.

To be sure I was right about this, I loaded up your code and filled out some stuff you didn't supply. This is what I ended up with, and you can run it as-is to confirm that it was just the sign of the jump velocity.

(Note I also set up an _init() and _draw(), since that's best practices on PICO-8 and in gamedev in general.)

function _init()
-- i had to guess at these
    x=64     -- x pos
    y=64     -- y pos
    xv=0     -- x vel
    yv=0     -- y vel
    mxv=2    -- max x vel
    s=0.125  -- x accel-not speed?
    f=0.0625 -- friction
    g=0.125  -- gravity
    skin=0   -- player sprite?
-- i added this one
    j=-5     -- jump instant accel
end

function _update60()
    if(btnp(⬆️))then
        yv=j
    end
    if(btn(➡️))then
        xv+=s
    end
    if(btn(⬅️))then
        xv-=s
    end
    if(xv>mxv)then
        xv=mxv
    end
    if(xv<0-mxv)then
        xv=0-mxv
    end
    yv+=g
    y+=yv
    x+=xv
    if(xv>0)then
        xv-=f
        if(xv<0)then
            xv=0
        end
    end
    if(xv<0)then
        xv+=f
        if(xv>0)then
            xv=0
        end
    end
-- i added this to clamp x
    x=mid(0,x,120)
-- you could clamp y similarly
-- with one of these:
-- y=min(y,120)   -- no ceiling
-- y=mid(0,y,120) -- ow my head
    if(y>120)then
        y=120
    end
end

function _draw()
-- i moved these here because 
-- it's always best to separate
-- update from draw, trust me
-- as an experienced gamedev :)
    cls()
    spr(skin,x,y)
end
P#129937 2023-05-19 21:13 ( Edited 2023-05-19 21:30)
1

BTW, it's absolutely not a big deal, but I notice you have mixed tabs and spaces for indenting.

This is fine within PICO-8, since they look the same by default, but it can cause your code to look all janky when you post it somewhere else, because tabs are usually more than one space wide, indeed usually 4 spaces wide for web-formatted code, and a whole 8 spaces wide for traditional plaintext documents.

By default in PICO-8, you can't see the difference, but you can make tabs visible by going the PICO-8 command prompt and entering config draw_tabs on.

You can also alter the width of tabs. I personally recommend config tab_width 2 for PICO-8, but tastes may vary. I just like being able to see where scopes begin and end by scanning up and down the vertical wine-colored lines they draw as.

This is what some of your code looks like on my screen, after I converted all indents to tabs:

P#129938 2023-05-19 21:30 ( Edited 2023-05-20 23:34)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2023-12-09 02:34:23 | 0.032s | Q:20