Hello I'm trying to add a tail behind player just like the snake game. But for some reason this keeps lagging out my game and running it out of memory. Why is this happening and how do I fix this?
I create the list like this:
List<Snake> snake = new CopyOnWriteArrayList<Snake>();
This is where I create new objects and remove them in a forloop:
public void snake() {
snake.add(new Snake(ball.getX(), ball.getY()));
currentTime++;
for(Snake currentSnake: snake) {
if(currentSnake.creationDate < SnakeLength){
currentSnake.Update();
} else {
Gdx.app.log("SnakeLength" + SnakeLength, "CreationDate" + currentSnake.creationDate);
snake.remove(currentSnake);
}
}
}
This is how my snake class looks like:
public class Snake
{
float width = Gdx.graphics.getWidth();
float height = Gdx.graphics.getHeight();
float screenwidth = width/270;
float screenheight = height/480;
public float x;
public float y;
public int creationDate;
ShapeRenderer shapeRenderer;
SpriteBatch batch;
public boolean active = false;
public Snake(float x, float y) {
shapeRenderer = new ShapeRenderer();
batch = new SpriteBatch();
this.x = x;
this.y = y;
}
public void Update() {
batch.begin();
shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(new Color(1, 1, 1, 0.2f));
shapeRenderer.circle(x + 8*screenwidth, y + 8*screenheight, 6*screenheight);
shapeRenderer.end();
batch.end();
creationDate++;
}
}