Hi guys.
I’m back working on my tower defence game today and I’ve noticed a very strange issue. If I set the speed variable to anything other than 1, the movement breaks. If the value is lower than 1, then when it gets to the next tile, the movement stops. If the speed is larger than 1, the entity flickers on the screen very fast, while stuck on the first tile.
I feel like I’ve made a stupid mistake somewhere but I can’t seem to find it. I’ve posted the movement code below, along with an explanation
for(Entity entity : entityList){
if(entity.getPathIndex() < path.getLength()){
Vector2 direction = new Vector2(path.getPath().get(entity.getPathIndex()).getX() - entity.getX(), path.getPath().get(entity.getPathIndex()).getY() - entity.getY());
direction.nor();
entity.setX(entity.getX() + direction.x * 1);
entity.setY(entity.getY() + direction.y * 1);
if(entity.getX() == path.getPath().get(entity.getPathIndex()).getX() && entity.getY() == path.getPath().get(entity.getPathIndex()).getY()){
entity.setPathIndex(entity.getPathIndex() + 1);
}
}
}
The code checks if the current entity’s path index is less than the path, and then creates a new vector that points to the direction of the next tile (which is then normalized). The movement code then takes place and the direction is multiplied by the speed. After the movement code, I check if the entity has got to the next tile, if it has, then it simply increments the path index for said entity and repeats the whole process.
It just makes sense in my head, but I can’t see what is going wrong ???