2014-10-30 54 views
-1

我收到此错误:Syntax error on token "}", { expected after this token错误与支架放置

我不知道问题是什么在这里,我已经检查了括号匹配,还是让我觉得,所以我并不完全清楚如何解决这个问题

import java.awt.*; 


public class Player extends Entity { 

    private int directionX, directionY; 
    private Main instance; 

    private Rectangle hitbox; 

    private int life = 3; 

    public Player(Main instance, int x, int y){ 
     super(x , y); 
     this.instance = instance; 
     width = 16; height = 16; 

     hitbox = new Rectangle(x, y, width, height); 
    } 

    public void draw(Graphics g){ 
     move(); 

     g.setColor(Color.WHITE); 
     g.fillOval(hitbox.x, hitbox.y, hitbox.width, hitbox.height); 
     g.setColor(Color.WHITE); 
     g.drawString("Lives: " + life, 20, 20); 
    } 

    private void move(){ 
     if(!instance.getStage().isCollided(hitbox)){ 
      directionY = 1; 
     } else { directionY = 0; 
     hitbox.x += directionX; 
     hitbox.y += directionY; 
     } 
    } // <<<<<< Getting error here <<<<<<<<<< 

    if(instance.getEnemyManager().isCollided(hitbox)){ 
     if(life > 0){ 
      life--; 
      hitbox.x = 800/2 - width/2; 
      y = 390; 
     } else { 
      instance.setGameOver(true); 
     } 
     } 


    public void setdirectionX(int value){ 
     directionX = value; 
    } 

    public void setdirectionY(int value){ 
     directionY = value; 
    } 
} 

如果有人能回答这个问题我会很高兴知道。

回答

2

看看在有问题的区域代码:

private void move(){ 
    if(!instance.getStage().isCollided(hitbox)){ 
     directionY = 1; 
    } else { directionY = 0; 
    hitbox.x += directionX; 
    hitbox.y += directionY; 
    } 
} 

if(instance.getEnemyManager().isCollided(hitbox)){ 

右括号是方法的结束......然后你有一个if声明是在类声明的中间,而不是以任何方法。你是否打算成为move()的一部分?如果是这样,你需要在它之前删除右大括号。

请注意,在您的文章中,至少,缩进遍布整个地方。它真的帮助如果你让你的IDE格式化你的代码,所以你可以看到什么代码是什么方法等的一部分。它使它更容易让其他人阅读...

1

你写

if(instance.getEnemyManager().isCollided(hitbox)){ 
    if(life > 0){ 
     life--; 
     hitbox.x = 800/2 - width/2; 
     y = 390; 
    } else { 
     instance.setGameOver(true); 
    } 
    } 

任何方法之外,这就是为什么你的编译器警告您