java - libgdx remove bullet from bullet array -
im creating 2d game. have array of bullet class use create bullets. have array consisting sprites move around screen being rendered every 3 4 seconds.
problem:
when bullet , other sprite meet @ same location bullet should removed or deleted , never comeback.
please me...
code rendering bullet:
bullet.java:
import com.badlogic.gdx.math.vector2; public class bullet{ public vector2 bulletlocation = new vector2(0, 0); public vector2 bulletvelocity = new vector2(0, 0); public bullet(vector2 sentlocation, vector2 sentvelocity) { bulletlocation = new vector2(sentlocation.x, sentlocation.y); bulletvelocity = new vector2(sentvelocity.x, sentvelocity.y); } public void update() { bulletlocation.x += bulletvelocity.x; bulletlocation.y += bulletvelocity.y; } }
main class:
arraylist<bullet> bulletmanager = new arraylist<bullet>(); array<other> othersmanager = new array<other>(); bullet currentbullet; other currentothers;
render();
int other = 0; while (other < othersmanager.size) { currentothers = othersmanager.get(other); if (currentothers.otherlocation.x > guy.getx()) { currentothers.othervelocity.x = -2f; } if (currentothers.otherlocation.x < guy.getx()) { currentothers.othervelocity.x = 2f; } currentothers.update(); others.setposition(currentothers.otherlocation.x,currentothers.otherlocation.y); others.draw(batch); other++; } int counter = 0; while (counter < bulletmanager.size()) { currentbullet = bulletmanager.get(counter); currentbullet.update(); shoot.setposition(currentbullet.bulletlocation.x,currentbullet.bulletlocation.y); if(currentothers.otherlocation.x == currentbullet.bulletlocation.x){ bulletmanager.remove(currentbullet); } shoot.draw(batch); counter++; }
it seems problem in block of code:
if(currentothers.otherlocation.x == currentbullet.bulletlocation.x){ bulletmanager.remove(currentbullet); }
i suggest use epsilonequals method vector2 class check whether currentothers.otherlocation matches currentbullet.bulletlocation:
if(currentothers.otherlocation.epsilonequals(currentbullet.bulletlocation)){ bulletmanager.remove(currentbullet); }
moreover can use epsilonequals float epsilon: documentation
Comments
Post a Comment