Java游戏运动keyTyped

时间:2016-05-22 01:36:10

标签: java key

所以我试图用Java编写基本游戏(Eclipse)。我试着对它进行编码,这样当你按E时,玩家会移动。但是,将pixelSize(int 30)添加到我的ballX并不能在keyTyped / Pressed行中工作。 这是我的代码: package com.d1gbit.d1game; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; @SuppressWarnings("串行&#34) 公共类GameWindow扩展JPanel { int ballX = 100; int ballY = 0; int pixelSize = 10; 双球= 0; String dp =" o&#34 ;; 字符串jl =" p&#34 ;; public GameWindow(){     KeyListener listener = new MKeyListener();     addKeyListener(收听);     setFocusable(真); } class MKeyListener实现KeyListener {     @覆盖     public void keyTyped(KeyEvent e){         dp = KeyEvent.getKeyText(e.getKeyCode());            if(dp ==" E"){                 ballX = ballX + pixelSize;         }     }     public void keyPressed(KeyEvent e){         dp = KeyEvent.getKeyText(e.getKeyCode());         String jl =" keyPressed =" + KeyEvent.getKeyText(e.getKeyCode());         的System.out.println("的keyPressed =&#34 + KeyEvent.getKeyText(e.getKeyCode()));         的System.out.println(DP);         的System.out.println(JL);            if(jl ==" keyPressed = E"){                 ballX = ballX + pixelSize;             }     }     public void keyReleased(KeyEvent e){     } } private void moveBall(){ double accel = 0;     accel = accel + 0.1; ballY =(int)(ballY + accel); ballX = ballX + 0; }; @覆盖 public void paint(Graphics g){ super.paint(克); Graphics2D g2d =(Graphics2D)g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //忽略这一部分 g2d.fillRect(ballX,ballY,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize,ballY,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize * 2,ballY,pixelSize,pixelSize); g2d.fillRect(ballX,ballY + pixelSize,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize,ballY + pixelSize,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize,ballY + pixelSize * 2,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize * 2,ballY + pixelSize * 2,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize * 3,ballY + pixelSize * 2,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize,ballY + pixelSize * 3,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize * 3,ballY + pixelSize * 3,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize * 4,ballY + pixelSize * 3,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize,ballY + pixelSize * 4,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize * 2,ballY + pixelSize * 4,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize * 4,ballY + pixelSize * 4,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize * 5,ballY + pixelSize * 4,pixelSize,pixelSize); g2d.fillRect(ballX,ballY + pixelSize * 5,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize * 2,ballY + pixelSize * 5,pixelSize,pixelSize); g2d.fillRect(ballX + pixelSize * 4,ballY + pixelSize * 5,pixelSize,pixelSize); } public static void main(String [] args)throws InterruptedException { JFrame frame = new JFrame(" D1gbitx0"); GameWindow window = new GameWindow(); frame.add(窗口); frame.setSize(1000,1500); frame.setVisible(真); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     while(true){         window.moveBall();         window.repaint();         了Thread.sleep(10);         } }} 我尝试在一个部分做这个:     class MKeyListener实现KeyListener {     @覆盖     public void keyTyped(KeyEvent e){         dp = KeyEvent.getKeyText(e.getKeyCode());            if(dp ==" E"){                 ballX = ballX + pixelSize;         }     }     public void keyPressed(KeyEvent e){         dp = KeyEvent.getKeyText(e.getKeyCode());         String jl =" keyPressed =" + KeyEvent.getKeyText(e.getKeyCode());         的System.out.println("的keyPressed =&#34 + KeyEvent.getKeyText(e.getKeyCode()));         的System.out.println(DP);         的System.out.println(JL);            if(jl ==" keyPressed = E"){                 ballX = ballX + pixelSize;             }     }     public void keyReleased(KeyEvent e){     } } 但正如您所看到的,System.out.println部分可以工作,但if语句中没有任何内容。 你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

使用str1.equals(str2)比较两个字符串。 ==评估对象的相等性。

替换:

if (jl == "keyPressed=E") {
    ballX = ballX + pixelSize;
}

使用:

if ("keyPressed=E".equals(jl)) {    // NOTE: used jl as parameter in case it is null
    ballX = ballX + pixelSize;
}