2016-09-26 97 views
0

我想限制某个单元格可以去的区域,所以我加了一个Point spawn,这样我就可以使用spawn.distance()来确保它不会离开它的产卵区太远。问题在于它不断更改到单元的当前位置。据我所知,没有什么能够在它被设置后改变它。有没有人看到它改变的原因?这个Point为什么会改变?

实体类:

public abstract class Entity { 

    protected int width, height; 

    protected Point location; 
    protected CellType cellType; 

    abstract void tick(); 
    abstract void render(Graphics g); 

    public int getWidth() { 
     return width; 
    } 
    public int getHeight() { 
     return height; 
    } 
    public Point getLocation() { 
     return location; 
    } 
    public CellType getCellType() { 
     return cellType; 
    } 

} 

Cell类:

public class Cell extends Entity{ 

    private Random random; 

    private CellType cellType; 
    private Point spawn; 

    private int angle; 
    private float xVelocity, yVelocity; 
    private float maxVelocity = .2f; 

    public Cell(Point location) { 
     random = new Random(); 

     cellType = MasterGame.cellTypes.get(random.nextInt(MasterGame.cellTypes.size())); 
     width = MasterGame.cellSizes.get(cellType); 
     height = width; 
     spawn = location; 
     super.location = location; 
    } 

    int ticks = 0; 
    public void tick() { 
     if(ticks == 15) { 
      System.out.println(spawn); 
      angle = random.nextInt(360); 
      xVelocity = (float) (maxVelocity * Math.cos(angle)); 
      yVelocity = (float) (maxVelocity * Math.sin(angle)); 
      ticks = 0; 
     } 
     if(ticks % 3 == 0){ 
      location.x += xVelocity; 
      location.y += yVelocity; 
     } 
     ticks++; 
    } 

    public void render(Graphics g) { 
     g.setColor(Color.DARK_GRAY); 
     g.fillOval(location.x, location.y, width, height); 
     g.setColor(Color.GREEN); 
     g.fillOval((int)(location.x+(width*.125)), (int)(location.y+(height*.125)), (int)(width*.75), (int)(height*.75)); 
    } 

} 
+0

请提供[MCVE](http://stackoverflow.com/help/mcve)。给定的代码没有说清楚目前实际发生了什么。 – SomeJavaGuy

+0

实际上有更改位置的代码if(ticks%3 == 0)location.x + = xVelocity; location.y + = yVelocity; }' –

+0

@MikhailKuchma不是'位置'产卵' – TheGamerPlayz

回答

0
spawn = location; 
    super.location = location; 

你有参考一个对象的两个变量。使用某种复制构造函数或类似的方法将原始位置存储为spawn

相关问题