在java中,如何让玩家在没有速度的情况下跳跃到对象上

在java中,如何让玩家在没有速度的情况下跳跃到对象上,java,animation,collision,Java,Animation,Collision,我的问题是,我想让我的角色(玩家)跳到我稍后添加的对象上。我原以为瓷砖碰撞会起作用,但我不明白这是怎么回事。此外,我的比赛只是一次一级,而不是一整场比赛 这是我到目前为止所拥有的。(我没有编写代码让玩家跳上任何东西,因为我不知道从哪里开始): Game.java: public class Game extends gameLoop{ public void init() { setSize(854,480); Thread th = new T

我的问题是,我想让我的角色(玩家)跳到我稍后添加的对象上。我原以为瓷砖碰撞会起作用,但我不明白这是怎么回事。此外,我的比赛只是一次一级,而不是一整场比赛

这是我到目前为止所拥有的。(我没有编写代码让玩家跳上任何东西,因为我不知道从哪里开始):

Game.java:

public class Game extends gameLoop{

    public void init()
    {
        setSize(854,480);
        Thread th = new Thread(this);
        th.start();
        offscreen = createImage(854, 480);
        d = offscreen.getGraphics();
        addKeyListener(this);

    }

    public void paint(Graphics g){
        d.clearRect(0, 0, 854, 480);
        d.drawImage(background, 0 , 0 , this);
        d.drawImage(Player, x, y, this);
        g.drawImage(offscreen, 0 , 0, this);    
    }
    public void update(Graphics g){
        paint(g);
    }
}
gameLoop.java:

public class gameLoop extends Applet implements Runnable, KeyListener {

    public int x ,y;
    public Image offscreen;
    public Graphics d;
    public boolean up, down, left, right;
    public BufferedImage background, run1, run2, run3, run4, run5, run6, run7, run8, runL1, runL2, runL3, runL4, runL5, stand, Player;
    public int counter;
    public int counterLeft;
    public double counter2 = 4; 


    public void run(){
        x = 100;
        y = 100;
        try {
            background = ImageIO.read(new File("background2.png"));
            stand = ImageIO.read(new File("stick.gif"));
            run1 = ImageIO.read(new File("Runner.png"));
            run2 = ImageIO.read(new File("Runner1.png"));
            run3 = ImageIO.read(new File("Runner2.png"));
            run4 = ImageIO.read(new File("Runner3.png"));
            run5 = ImageIO.read(new File("Runner4.png"));
            runL1 = ImageIO.read(new File("Runner(1).png"));
            runL2 = ImageIO.read(new File("Runner1(1).png"));
            runL3 = ImageIO.read(new File("Runner2(1).png"));
            runL4 = ImageIO.read(new File("Runner3(1).png"));
            runL5 = ImageIO.read(new File("Runner4(1).png"));

        } catch (IOException e1) {
            e1.printStackTrace();
        }

        Player = stand;
        while(true){
            if(y <= 319 && up != true){
                y+=10;
            }



            counter++;
            counterLeft ++;
            if (counter >= 90){
                counter = 0;
            }
            if (counter >= 10 &&  right == true && left == false){
                Player = run1;
            }
            if (counter >= 20 &&  right == true && left == false){
                Player = run2;
            }
            if (counter >= 30 &&  right == true && left == false){
                Player = run3;
            }
            if (counter >= 40 &&  right == true && left == false){
                Player = run4;
            }
            if (counter >= 50 &&   right == true && left == false){
                Player = run5;
            }
            if (counter >= 60 &&  right == true && left == false){
                Player = run1;
            }
            if (counter >= 70 &&  right == true && left == false){
                Player = run2;
            }
            if(counter >= 80 && right == true && left == false){
                Player = run3;
            }

            // LEFT
            if (counterLeft >= 55){
                counterLeft = 0;
            }
            if (counterLeft >= 10 &&  left == true && right == false){
                Player = runL1;
            }
            if (counterLeft >= 20 &&  left == true && right == false){
                Player = runL2;
            }
            if (counterLeft >= 30 && left == true && right == false){
                Player = runL3;
             }
            if (counterLeft >= 40 && left == true && right == false){
                Player = runL4;
            }
            if (counterLeft >= 50 && left == true && right == false){
                Player = runL5;
            }




            if (left == true){
                x--;
            }
            if (right == true){
                x++;
            }

            if (up == true){
                counter2 += 0.05;
                y = y + (int) ((Math.sin(counter2) + Math.cos(counter2))* 5);
                if(counter2 >= 7){
                    counter2 = 4;

                }
            }
            if(down == true){
                y++;
            }
            if(y >= 319){
                y = 319;
            }


            repaint();
            try {
                Thread.sleep(5);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        }



    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == 37 && e.getKeyCode() != 38){ //left
            left = true;
        }
        if (e.getKeyCode() == 38 && e.getKeyCode() != 37){ //up or jump
            up = true;
        }
        if (e.getKeyCode() == 39){ //right
            right = true;
        }
        if (e.getKeyCode() == 40){ //down
            down = true;
        }

    }


    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == 37){  
            left = false;
            Player = stand;
        }
        if (e.getKeyCode() == 38){
            up = false;
            Player = stand;

        }
        if (e.getKeyCode() == 39){
            right = false;
            Player = stand;
        }
        if (e.getKeyCode() == 40){
            down = false;
            Player = stand;
        }
    }


    public void keyTyped(KeyEvent e) {}
}
public类gameLoop扩展Applet实现可运行的KeyListener{
公共整数x,y;
屏幕外的公众形象;
公共图形d;
公共布尔上、下、左、右;
公共缓冲区映像背景、run1、run2、run3、run4、run5、run6、run7、run8、runL1、runL2、runL3、runL4、runL5、stand、Player;
公共int计数器;
公共int靠左;
公共双计数器2=4;
公开募捐{
x=100;
y=100;
试一试{
background=ImageIO.read(新文件(“background2.png”);
stand=ImageIO.read(新文件(“stick.gif”);
run1=ImageIO.read(新文件(“Runner.png”);
run2=ImageIO.read(新文件(“Runner1.png”);
run3=ImageIO.read(新文件(“Runner2.png”);
run4=ImageIO.read(新文件(“Runner3.png”);
run5=ImageIO.read(新文件(“Runner4.png”);
runL1=ImageIO.read(新文件(“Runner(1.png));
runL2=ImageIO.read(新文件(“Runner1(1).png”);
runL3=ImageIO.read(新文件(“Runner2(1.png”));
runL4=ImageIO.read(新文件(“Runner3(1.png”));
runL5=ImageIO.read(新文件(“Runner4(1.png”));
}捕获(IOE1异常){
e1.printStackTrace();
}
运动员=站立;
while(true){
如果(y=90){
计数器=0;
}
如果(计数器>=10&&right==true&&left==false){
Player=run1;
}
如果(计数器>=20&&right==true&&left==false){
Player=run2;
}
如果(计数器>=30&&right==true&&left==false){
Player=run3;
}
如果(计数器>=40&&right==true&&left==false){
Player=run4;
}
如果(计数器>=50&&right==true&&left==false){
Player=run5;
}
如果(计数器>=60&&right==true&&left==false){
Player=run1;
}
如果(计数器>=70&&right==true&&left==false){
Player=run2;
}
如果(计数器>=80&&right==true&&left==false){
Player=run3;
}
//左
如果(对左>=55){
对左=0;
}
如果(反左>=10&&left==true&&right==false){
Player=runL1;
}
如果(反左>=20&&left==true&&right==false){
Player=runL2;
}
如果(反左>=30&&left==true&&right==false){
Player=runL3;
}
如果(反左>=40&&left==true&&right==false){
Player=runL4;
}
如果(反左>=50&&left==true&&right==false){
Player=runL5;
}
如果(左==真){
x--;
}
if(right==true){
x++;
}
如果(向上==真){
计数器2+=0.05;
y=y+(int)((数学sin(counter2)+数学cos(counter2))*5);
如果(计数器2>=7){
2=4;
}
}
如果(向下==真){
y++;
}
如果(y>=319){
y=319;
}
重新油漆();
试一试{
睡眠(5);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
按下公共无效键(按键事件e){
如果(e.getKeyCode()=37和&e.getKeyCode()!=38){//左
左=真;
}
如果(e.getKeyCode()=38和&e.getKeyCode()!=37){//向上或跳转
向上=真;
}
如果(e.getKeyCode()==39){//right
右=真;
}
如果(例如getKeyCode()==40){//down
向下=真;
}
}
公共无效密钥已释放(密钥事件e){
如果(e.getKeyCode()==37){
左=假;
运动员=站立;
}
如果(例如getKeyCode()==38){
向上=错误;
运动员=站立;
}
如果(例如getKeyCode()==39){
右=假;
运动员=站立;
}
如果(例如getKeyCode()==40){
向下=假;
运动员=站立;
}
}
public void keyTyped(KeyEvent e){}
}

到目前为止,他所能做的就是跑和跳 Applet < /代码>,考虑使用<代码> JApplet < /C> >,实际上,将核心图形放在<代码> jPoCT/<代码>上,然后可以添加到<代码> JApplet < />代码> <代码> jFrase>代码>或其他容器中。小程序在部署时将无法访问文件系统。这是否适用于BlueJ?因为现在我正在使用eclipse,但我需要向我在学校的老师展示BlueJ。在我看来,有两件事值得投资:1)键绑定和2)摆动计时器。注意,“right==true&&left==false”在代码中是过度冗余的。。。如果没有其他东西,它“应该”是
正确的&&!左
。是的,这对我的问题没有帮助。我不明白如何才能让玩家跳到某个东西上。如果我的代码与TileMap无关,我显然不明白如何使用Tile碰撞。除了瓷砖,还有什么我能用的吗