2016-09-26 96 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

相關問題