Major Bug!

I did an if statement like this:

if playerYVel == 0 then

It says that playerYVel is 0, but it still won’t activate the code. Please help!

We need more code to figure out what’s happening. Nothing wrong with what you’re showing so far.

It’s too long, is there somewhere else I could put it?

Can you just show some of the code before and after where you think you have a problem.

Here. playerYVel is a parameter which is set to a variable that controls the players Y velocity. It doesn’t work with the plain variable yVel either by the way. What is happening is that when I click the jump button it doesn’t jump, even though the parameter says 0. I’ve tested it and jump works without the if playerYVel == 0 condition. But this is so you can’t jump in mid air.
This is in the touch function.

        --Jump Button
       if touch.x > 95 and touch.x < 295 and CanMove == true and
        touch.y < 200 and touch.y > 100 and playerYVel == 0 then
                yVel = 170
              updatePlayerVel()  
        end

Can you replicate this bug in a separate project?

I’ll see…

Are you saying that the 2 statements after “then” isn’t being executed. If so, then put print statements before the “if” statement so you can see the values of all the variables in the “if” statement. That should help you determine what’s happening.

It’s printing nil.
But it’s printing nil even when it’s definatly not nil, like when the player is falling.

If it’s nil, then that’s the problem. You have to determine why. Is the spelling of the variable correct. Is one letter capital in one place and not the other.

playerYVel

See my last post, I added stuff before your last response. One thing about programming, it does exactly what you write, not what you want.

now it prints -4. I made the if statement if yVel > 10 and yVel < 0. But it prints -4 and he still won’t jump.

You have to look thru your code and determine why the value of yVel or playerYVel isn’t what you think it is. Since your code is too big to show, you’ll have to do that yourself. Try putting print statements where you have those variables and try to determine what’s happening.

The value of yVel can’t be greater than 10 and less than 0 at the same time. That condition will always be false.

Check the CanMove variable. I’m suspicious since it has a leading cap here that it might not match elsewhere.

@Mark CanMove is correct because @EpicPotato said things worked when playerYVel was removed from the “if” statement.

@EpicPotato - it looks very like an ordinary coding error.

You’ve already found the value of playerYVel was negative when you thought it was zero, and now your replacement code is incorrect, as pointed out by @Mark.

Please keep analysing the problem, until you know exactly what is happening. Otherwise, asking for help is just wasting everyone’s time, especially when you refer to it as “Major Bug!”. Please don’t do that unless you are really, really sure, because you will probably embarrass yourself. Nearly every time, it is a user bug, not a bug in Codea.

I’d try something like this.


    if touch.x > 95 and touch.x < 295 then
        print("valid x")
        If canMove then
            print("can move")
            If touch.y < 200 and touch.y > 100 then
                print("valid y")
                if playerYVel == 0 then
                    print("jumped")
                    yVel = 170
                    updatePlayerVel()  
                end
            end
        end
    end

Just until you’re sure of the failure point. Then you can remove the prints and merge the ifs.