在java,applet中找不到符号

时间:2014-05-18 06:35:11

标签: java applet pong

我正在开发一个java applet游戏(乒乓游戏),但我遇到了一个问题。请帮助:/ 在KeyHandler.java中,编译器找不到PlayerPaddle?

CMD(编译器错误):

    .KeyHandler.java:28: error: cannot find symbol
                    PlayerPaddle = stopYvelo();

   symbol:     variable PlayerPaddle
   location: class KeyHandler
  .\KeyHandler.java:28: error: cannot find symbol
                    PlayerPaddle = stopYvelo();

  symbol:     method stopYvelo()
  location: class KeyHandler
 .\KeyHandler.java:31: error: cannot find symbol
                    PlayerPaddle = stopYvelo();

  symbol:   variable PlayerPaddle
 location: class KeyHandler
 .\KeyHandler.java:31: error: cannot find symbol
                    PlayerPaddle = stopYvelo();

 symbol: method stopYvelo()
 location: class KeyHandler
 4 errors

代码:

Runtime.java

 import javax.swing.Timer;
 import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;

public class Runtime extends JApplet implements ActionListener {

private static final long serialVersionUID=1L;

 //Velikost appleta

 public static final int APPLET_WIDTH = 500;
 public static final int APPLET_HEIGHT = 400;

 //Key event handler

 public KeyHandler handler = new KeyHandler();

 private int x=20;
 private int y=20;

  //Player object 

  public PlayerPaddle player = new PlayerPaddle();

   //Ball object

   public Ball ball = new Ball();

   //Computer object

   public ComputerPaddle computer = new ComputerPaddle();

   //Score keeping

   public static int playerScore = 0;
   public static int computerScore = 0;

   public void init(){

setSize(APPLET_WIDTH,APPLET_HEIGHT);
addKeyListener(handler);
setFocusable(true);

Timer timer = new Timer(10, this);
timer.start();
  }

  public void update(){

ball.update();
player.update();
computer.update();
    }
   public void paint (Graphics g) {

g.setColor(new Color(40, 132, 150));
g.fillRect(0, 0, APPLET_WIDTH, APPLET_HEIGHT);

g.setColor(Color.WHITE);    
g.drawString(String.format("%d  |   %d", playerScore, computerScore), 225, 20);

ball.paint(g);
player.paint(g);
computer.paint(g);

    }
    public void actionPerformed (ActionEvent e) {

update();
repaint();

    }
    }

KeyHandler.java

   import java.awt.event.KeyAdapter;
   import java.awt.event.KeyEvent;

   public class KeyHandler extends KeyAdapter {

    private int playerSpeed = 3;

public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_UP){

            PlayerPaddle.setYvelo(-playerSpeed);
    }
    if(e.getKeyCode() == KeyEvent.VK_DOWN){

            PlayerPaddle.setYvelo(playerSpeed);
    }
    if(e.getKeyCode() == KeyEvent.VK_SPACE){

        if(!Ball.ballServed){

            Ball.ballServed = true;
            Ball.xVelo = 3;
            Ball.yVelo = 3;
        }
    }
}
public void KeyReleased (KeyEvent e){

    if(e.getKeyCode() == KeyEvent.VK_UP){

        PlayerPaddle = stopYvelo();

    }
    if(e.getKeyCode() == KeyEvent.VK_DOWN){

        PlayerPaddle = stopYvelo();

    }
}
    }

PlayerPaddle.java

   import java.awt.Graphics;
   import java.awt.Color;

   public class PlayerPaddle {

public static int x = 10;
public static int y = Ball.y;

public static int yVelo = 0;

public static int width = 10;
public static int height = 40;

public void update() {
    y += yVelo;

    if(y <= 0) {

        y = 0;

    }
    if(y >= Runtime.APPLET_HEIGHT - height){

        y = Runtime.APPLET_HEIGHT - height;

    }

}

public void paint (Graphics g){

    g.setColor(Color.BLACK);
    g.fillRect(x, y, width, height);


}
public static int setYvelo( int playerSpeed) {

    return yVelo = playerSpeed;

}
public static int stopYvelo(){

    return yVelo = 0;

}
     }

Ball.java

    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.Random;


    public class Ball {
public static int x = 248;
public static int y = 200;

public static int xVelo = 0;
public static int yVelo = 0;

public int size = 10;

private boolean pattern;
private Random r = new Random();

public static boolean ballServed = false;

public void update () {

    if(ballServed){
        x += xVelo;
        y += yVelo;

        if(x <= 0){
            Runtime.computerScore++;

            x = 225;
            y = 200;

            ballServed = false;
        }

        if(x >= Runtime.APPLET_WIDTH-size){

            Runtime.playerScore++;
            x = 225;
            y = 200;

            ballServed = false;
        }

        if(y <= 0){
            yVelo= 3;
        }

        if(y >= Runtime.APPLET_HEIGHT-size){
            yVelo= -3;
        }
        if((x + size >= PlayerPaddle.x) && (x <= PlayerPaddle.x + PlayerPaddle.width) &&
            (y + size >= PlayerPaddle.y) && (y <= PlayerPaddle.y + PlayerPaddle.height)){
                pattern = r.nextBoolean();
                if(!pattern){
                    xVelo = 3;
                }
                if(pattern){
                    xVelo = -3;
                }

        }
        if((x + size >= ComputerPaddle.x) && (x <= ComputerPaddle.x + ComputerPaddle.width) &&
            (y + size >= ComputerPaddle.y) && (y <= ComputerPaddle.y + Computerpaddle.height)){

                pattern = r.nextBoolean();
                if(!pattern){
                    xVelo = 3;
                }
                if(pattern){
                    xVelo = -3;
                }
        }
    }
    if(!ballServed){
        xVelo = 0;
        yVelo = 0;
    }   
}

public void paint (Graphics g){
    g.setColor(Color.WHITE);
    g.fillOval(x, y, size, size);
}

}

1 个答案:

答案 0 :(得分:0)

这意味着你错过了什么......

应该是

PlayerPaddle.stopYvelo();