2016-09-26 3 views
0

J'essaye de limiter la zone qu'une certaine cellule peut aller, donc j'ai ajouté un Point spawn de cette façon que je peux utiliser spawn.distance() pour m'assurer qu'il ne s'écarte pas trop loin de sa réapparition. Le problème est qu'il ne cesse de changer à l'emplacement actuel de la cellule. Autant que je sache, il n'y a rien qui change après l'avoir défini. Est-ce que quelqu'un voit une raison qui change?Pourquoi ce point change-t-il?

Classe Entité:

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; 
    } 

} 

cellulaire Classe:

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

veuillez fournir un [MCVE] (http://stackoverflow.com/help/mcve). Le code donné ne précise pas ce qui se passe réellement en ce moment. – SomeJavaGuy

+0

En fait, il y a du code qui change d'emplacement 'if (coche% 3 == 0) { location.x + = xVelocity; location.y + = yVelocity; } ' –

+0

@MikhailKuchma Pas le' location'the 'spawn' – TheGamerPlayz

Répondre

0
spawn = location; 
    super.location = location; 

Vous avez deux variables qui font référence à un objet . Utilisez une sorte de constructeur de copie ou similaire pour stocker l'emplacement d'origine comme spawn.