I am making a version of Space Invaders, and am having trouble with changing the image of the barrier when an Invader Bullet intersects it. I have figured out how to change the image when the first bullet hits, but I don't know how to make the barrier deteriorate further when more bullets hit. This is the code in my Invader Bullet constructor class:
And this is the code inside my hitBarrier method:
All help will be appreciated!
Thanks!
public class invaderBullet extends Bullet { private static final int STEP_SIZE = 2; private GreenfootImage image; private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image3; public invaderBullet(){ image = getImage(); image.scale(image.getWidth()-495, image.getHeight()-340); setImage(image); image1 = new GreenfootImage("rock1.png"); int h1 = image1.getHeight(); int w1 = image1.getWidth(); image1.scale(w1*1/8, h1*1/12); image2 = new GreenfootImage("rock2.png"); int h2 = image2.getHeight(); int w2 = image2.getWidth(); image2.scale(w2*1/8, h2*1/12); image3 = new GreenfootImage("rock3.png"); int h3 = image3.getHeight(); int w3 = image3.getWidth(); image3.scale(w3*1/8, h3*1/12); }
public void hitBarrier(){ Actor barrier = getOneIntersectingObject(Barrier.class); if (barrier !=null && getImage() == image){ barrier.setImage(image1); getWorld().removeObject(this); } else if (barrier !=null && getImage() == image1){ barrier.setImage(image2); getWorld().removeObject(this); } else if (barrier !=null && getImage() == image2){ barrier.setImage(image3); getWorld().removeObject(this); } else if (barrier !=null && getImage() == image3){ getWorld().removeObject(barrier); getWorld().removeObject(this); } else if (getY() == getWorld().getHeight() - 1){ getWorld().removeObject(this); } }