What is the most efficient way to add and removed Slick2D sprites?
Posted
by
kirchhoff
on Game Development
See other posts from Game Development
or by kirchhoff
Published on 2012-04-03T11:07:49Z
Indexed on
2012/04/03
17:44 UTC
Read the original article
Hit count: 282
I'm making a game in Java with Slick2D and I want to create planes which shoots:
int maxBullets = 40;
static int bullet = 0;
Missile missile[] = new Missile[maxBullets];
I want to create/move my missiles in the most efficient way, I would appreciate your advise:
public void shoot() throws SlickException{
if(bullet<maxBullets){
if(missile[bullet] != null){
missile[bullet].resetLocation(plane.getCentreX(), plane.getCentreY(), plane.image.getRotation());
}else{
missile[bullet] =
new Missile("resources/missile.png", plane.getCentreX(), plane.getCentreY(), plane.image.getRotation());
}
}else{
bullet = 0;
missile[bullet].resetLocation(plane.getCentreX(), plane.getCentreY(), plane.image.getRotation());
}
bullet++;
}
I created the method resetLocation
in my Missile
class in order to avoid loading again the resource. Is it correct?
In the update method I've got this to move all the missiles:
if(bullet > 0 && bullet < maxBullets){
float hyp = 0.4f * delta;
if(bullet == 1){
missile[0].move(hyp);
}else{
for(int x = 0; x<bullet; x++){
missile[x].move(hyp);
}
}
}
© Game Development or respective owner