It depends. In my experience, it is probably best to set it to null just to be certain.
Or add it to secondary ArrayList and use removeAll(). Bit slow but works fine.
EDIT: Problem solved, I’ve done it without Rects…
Hey! The player can shoot now projectiles and they will be drawn correctly.
Now I create for every bullet a rectangle for collision with the enemy.
But somehow for 1 bullet there will be 60 Rectangles created?!?!
I checked the for loop and it loop the corect time(how many bullets have been created)
I make a bullet like this:
projectile = new Projectile(player.getX() + 55, player.getY() + 22);
I created a ArrayList
ArrayList <Projectile> bullets = new ArrayList<Projectile>();
Rectangle[] projectileRect = new Rectangle[1000];
ArrayList <Rectangle> shootingCollision = new ArrayList<Rectangle>();
So when the player shoot I add the bullet the Container:
bullets.add(projectile);
This is fine, but now I have to set every RectangleBullet new in each loop so in the Update method Id make:
for(int z=0 ; z<bullets.size(); z++){
projectileRect[z].setX(bullets.get(z).x);
projectileRect[z].setY(bullets.get(z).y);
projectileRect[z].setWidth(12);
projectileRect[z].setHeight(12);
shootingCollision.add(projectileRect[z]);
System.out.println("Looped: " +z);
}
shootingCollision.add(projectileRect[z]);
Here it adds like 60 Rectangles?!?! But why? It only go throught the Loop as how many bullets there are.
If I shoot 1 Bullet there should be only one Rectangle.
Collision detection.
Vector3f has x,y,z values the z is not used.
public boolean collision( Vector3f other, double dist )
{
if(this.loc.dist(other) > dist )
return false;
else
return true;
}
public double dist( Vector3f other )
{
return Math.sqrt(Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2));
}
The Vector3f is just an object with an x,y,z coordinates. In this case the z is not used.
The dist( Vector3f other ) finds the distance between the center of one location and the center of another.
You can then add the size (image length/width) to the collision method to get the collision without overlapping images.
It is not perfect but it works well for me.
The rect.intersects stuff is more accurate but also more cumbersome to use for me.
It will help you to google the distance formula.
Note: It’s somewhat undesirable to call pow for small integer exponents. Likewise the sqrt in this instance is not needed since: if a>b, then a2 > b2.
Ive done it this way:
for(int x=0;x<bullets.size();x++)
{
for(int z=0;z<enemyContainer.size();z++)
{
if(bullets.get(x).x > enemyContainer.get(z).x && bullets.get(x).x < enemyContainer.get(z).x + 64
&& bullets.get(x).y > enemyContainer.get(z).y && bullets.get(x).y < enemyContainer.get(z).y + 32 )
{
enemyContainer.get(z).hp=enemyContainer.get(z).hp-1;
}
}
}
It work but do you think were will be problems with more enemys?
I’ve actually made a game that has bullets and enemies.
Try it out and tell me if this is something that you’d want to see the source code for, because I have no hesitations to post the source code to help.
The way I learned is with interfaces. Basically you have an interface called ShotListener that has one method.
public interface ShotListener {
public boolean shotPerformed(ShotEvent event);
}
ShotEvent takes a Bullet when it is made.
public class ShotEvent {
private Bullet bullet;
public ShotEvent(Bullet bullet) {
this.bullet = bullet;
}
public int getX() { return bullet.getX(); }
public int getY() { return bullet.getY(); }
}
Then, have like 5 bullets in Player that you recycle, and give Bullet an ArrayList of ShotListeners and an
addShotListener(ShotListener listener) method that adds it to the array list.
Player and the enemy tanks should implement ShotListener and be added with the method.
Now, every update() for Bullet it calls shotPerformed for everything in the list.
There is probably a much more organized way to do it, but that’s what I do…
How do you handled the explosion animation after the enemy died?
If the enemy died did you active a switch for the animation?
In my games, I have an Explosion class that extends Entity and I add it to the GameWorld once the enemy/player is dead.