Java applet游戏跳跃bug



我目前正在制作一款java游戏,我只是设法让角色跳跃,但它只工作了一半的时间。有时他跳得很好,但有时他会被困在空中。我认为这与重力变量的变化有关,但我不确定,你能看一下吗?

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;
public class meatboy extends Applet implements KeyListener{
    public int x = 10, y = 300, gravity = 3;
    public int xs = 380, ys = 230 , jump_count, jump_move;
    double jumptime = 0;
    public boolean right, left, up, jump = true, start, grounded, grav = true;
    public void init(){
        setSize(800,400);
        setBackground(Color.black);
        addKeyListener(this);
        /////Movement//////
        Timer t = new Timer();
        t.schedule(new TimerTask(){public void run(){
        if (start == true){
        if (right == true){
            x = x + 3;
        }if (left == true){
            x = x - 3;
        }if(up == true && jump == true){
            jump_count++;
            if (jump_count >= 100){
                jump_count = 0;
            }
            if (jump_count >= 0 && jump_count <= 25){
                //jump_move = 1;
                y = y - 5;
            }
            if (jump_count >= 25 && jump_count <= 50){
                //jump_move = 2;
                y = y - 5;
            }
            if (jump_count >= 50 && jump_count <= 75){
                //jump_move = 3;
                y = y - 5;
            }
            if (jump_count >= 75 && jump_count <= 100){
                //jump_move = 0;
                y = y - 5;
                jump = false;
            }
        }
        /////GRAVITY//////
        if (grav == true){y = y + gravity;}
        ///////Collision Dectection/////
        if(x > 790){//right of screen stop
            x = 10;
        }if(x < 10){// left stop
            x = 789;
        }if(y > 350){
            //y = y - 3;
            grav = false;
            jump = true;
        }else{grav = true;}
        ////////////End of collision///////////
        repaint();
        }}},10,10);

        ///////movement end////////
        }
    public void paint(Graphics g){
        ////////CREATES MEATBOY///////////////
        if (start == false){
            x = 10; 
            y = 300;
            g.setColor(Color.DARK_GRAY);
            g.fillRect(0, 0, 800, 400);
            g.setColor(Color.white);
            g.drawString("MeatBoy 4K", 358, 180);
            g.drawString("Press Z to start!!!!", 350, 200);
            g.setColor(Color.RED);
            g.fillRect(xs, ys, 16, 16);//meatboys body
            g.fillRect(xs + 16, ys + 6, 4, 4);//arm
            g.fillRect(xs - 4, ys + 6, 4, 4);//arm
            g.fillRect(xs, ys + 12, 4, 6);//leg
            g.fillRect(xs + 12, ys + 12, 4, 6);//leg
            g.setColor(Color.black);
            g.fillRect(xs + 2, ys + 2, 5, 5);//eye
            g.fillRect(xs + 10, ys + 2, 5, 5);//eye
        }
        if (start == true){
        g.setColor(Color.RED);
        g.fillRect(x, y, 16, 16);//meatboys body
        g.fillRect(x + 16, y + 6, 4, 4);//arm
        g.fillRect(x - 4, y + 6, 4, 4);//arm
        g.fillRect(x, y + 12, 4, 6);//leg
        g.fillRect(x + 12, y + 12, 4, 6);//leg
        g.setColor(Color.black);
        g.fillRect(x + 2, y + 2, 5, 5);//eye
        g.fillRect(x + 10, y + 2, 5, 5);//eye
        ///////////END OF MEATBOY//////////////////
        ////////Creates Floor///////////////////
        g.setColor(Color.GRAY);
        g.fillRect(0, 370, 800, 30);
        }
    }
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_Z){
            //right = true;
            start = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT){
            //right = true;
            right = true;
        }if (e.getKeyCode() == KeyEvent.VK_LEFT){
            left = true;
        }if (e.getKeyCode() == KeyEvent.VK_UP){
            up = true;
        }if (e.getKeyCode() == KeyEvent.VK_SPACE){
            up = true;
        }
    }
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT){
            //right = true;
            right = false;
        }if (e.getKeyCode() == KeyEvent.VK_LEFT){
            left = false;
        }if (e.getKeyCode() == KeyEvent.VK_UP){
            up = false;
        }if (e.getKeyCode() == KeyEvent.VK_SPACE){
            up = false;
        }
    }
    public void keyTyped(KeyEvent e) {
    }
}

玩家在哪里被困在空中?

如果靠近地面,请确保y坐标在地面上。

在这段代码中:

}if(y > 350){
        //y = y - 3;
        grav = false;
        jump = true;
}else{grav = true;}

添加
}if(y > 350){
        //y = y - 3;
        grav = false;
        jump = true;
        //I'm on the ground all right
        y = 350;
}else{grav = true;}

最新更新