I make remake of old retro game "Space Shooter" I want to display bullet and check if there is hit with enemy
I added all of that but still got this error. I am sure its about bullet
Here code:
trash = new ArrayList<>();
for (Bullet bullet: bullets){
if(bullet.y<0){
trash.add(bullet);}
bullet.y = bullet.y+ (int)( -70 * screenRatioY);
for(Enemy enemy: enemies){
if(Rect.intersects(enemy.getRectangle(), bullet.getRectangle())){
score++;
enemyGetShot.start();
randomShot = random.nextInt(60-30)+30;
System.out.println("Nowy random shot: "+randomShot);
bullet.y=-500;
enemy.y=-500;
}
}
}
for(Bullet bullet : trash){
bullets.remove(bullet);}
and in drawing part:
for (Bullet bullet : bullets) {
canvas.drawBitmap(bullet.bullet, bullet.x, bullet.y, paint);
}
If someone wants a bullet class :
package com.example.space_shooter;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import static com.example.space_shooter.GameView.screenRatioX;
import static com.example.space_shooter.GameView.screenRatioY;
public class Bullet {
int x,y,width,height;
Bitmap bullet;
Bullet(Resources res){
bullet= BitmapFactory.decodeResource(res, R.drawable.bullet4);
width= bullet.getWidth();
height= bullet.getHeight();
width = (int) (width*1.7);
height = (int) (height*1.7);
width= (int) (width*screenRatioX);
height= (int) (height* screenRatioY);
bullet= Bitmap.createScaledBitmap(bullet, width,height, true);
}
Rect getRectangle(){
return new Rect(x,y, x+width, y+height);
}
}
Full project : https://github.com/polonez-byte-112/SpaceShooter