This site requires JavaScript, please enable it in your browser!
Greenfoot back
ananyagarg
ananyagarg wrote ...

2018/10/11

Need help with switching images when 2 objects intersect

ananyagarg ananyagarg

2018/10/11

#
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:
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);
    }
And this is the code inside my hitBarrier method:
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);
}
}
All help will be appreciated! Thanks!
danpost danpost

2018/10/11

#
Your if conditions are looking at the bullet's image -- not that of the barrier it intersects. Add 'barrier.' before the getImage calls within the if parameters.
ananyagarg ananyagarg

2018/10/14

#
Hi! I changed the code to what you mentioned, but now the bullets just pass the barrier without doing anything.
public void hitBarrier(){
    Actor barrier = getOneIntersectingObject(Barrier.class);
    if (barrier !=null && barrier.getImage() == image1){
            barrier.setImage(image2);
            getWorld().removeObject(this);
    }
    else if (barrier !=null && barrier.getImage() == image2){
        barrier.setImage(image3);
        getWorld().removeObject(this);
    }
    else if (barrier !=null && barrier.getImage() == image3){
        barrier.setImage(image4);
        getWorld().removeObject(this);
    }
    else if (barrier !=null && barrier.getImage() == image4){
        getWorld().removeObject(barrier);
        getWorld().removeObject(this);
    }
    else if (getY() == getWorld().getHeight() - 1){
        getWorld().removeObject(this);
}
}
ananyagarg ananyagarg

2018/10/14

#
danpost wrote...
Your if conditions are looking at the bullet's image -- not that of the barrier it intersects. Add 'barrier.' before the getImage calls within the if parameters.
Hi! I changed the code to what you mentioned, but now the bullets just pass the barrier without doing anything.
public void hitBarrier(){
    Actor barrier = getOneIntersectingObject(Barrier.class);
    if (barrier !=null && barrier.getImage() == image1){
            barrier.setImage(image2);
            getWorld().removeObject(this);
    }
    else if (barrier !=null && barrier.getImage() == image2){
        barrier.setImage(image3);
        getWorld().removeObject(this);
    }
    else if (barrier !=null && barrier.getImage() == image3){
        barrier.setImage(image4);
        getWorld().removeObject(this);
    }
    else if (barrier !=null && barrier.getImage() == image4){
        getWorld().removeObject(barrier);
        getWorld().removeObject(this);
    }
    else if (getY() == getWorld().getHeight() - 1){
        getWorld().removeObject(this);
}
}
danpost danpost

2018/10/14

#
ananyagarg wrote...
I changed the code to what you mentioned, but now the bullets just pass the barrier without doing anything. << Code Omitted >>
You probably are not comparing to the same image you set the barriers to. That is, image1 thru image4 in the Bullet class are probably not the same GreenfootImage instances that you have in the Barrier class.
ananyagarg ananyagarg

2018/10/14

#
danpost wrote...
ananyagarg wrote...
I changed the code to what you mentioned, but now the bullets just pass the barrier without doing anything. << Code Omitted >>
You probably are not comparing to the same image you set the barriers to. That is, image1 thru image4 in the Bullet class are probably not the same GreenfootImage instances that you have in the Barrier class.
I am pretty sure they are the same.
public class invaderBullet extends Bullet
{
    private static final int STEP_SIZE = 1;
    private GreenfootImage image;
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    
    public invaderBullet(){
    image = getImage();
    image.scale(image.getWidth()-495, image.getHeight()-340);
    setImage(image);
    
    image1 = new GreenfootImage("rock.png"); 
    int h1 = image1.getHeight();
    int w1 = image1.getWidth();
    image1.scale(w1*1/8, h1*1/12);
    
    image2 = new GreenfootImage("rock1.png");
    int h2 = image2.getHeight();
    int w2 = image2.getWidth();
    image2.scale(w2*1/8, h2*1/12);
    
    image3 = new GreenfootImage("rock2.png");
    int h3 = image3.getHeight();
    int w3 = image3.getWidth();
    image3.scale(w3*1/8, h3*1/12);
    
    image4 = new GreenfootImage("rock3.png");
    int h4 = image4.getHeight();
    int w4 = image4.getWidth();
    image4.scale(w4*1/8, h4*1/12);
    }
ananyagarg ananyagarg

2018/10/14

#
danpost wrote...
ananyagarg wrote...
I changed the code to what you mentioned, but now the bullets just pass the barrier without doing anything. << Code Omitted >>
You probably are not comparing to the same image you set the barriers to. That is, image1 thru image4 in the Bullet class are probably not the same GreenfootImage instances that you have in the Barrier class.
The thing is, I created these 4 barrier images in the Bullet class, not the Barrier class. Will it make a difference if I move them to the Barrier class?
danpost danpost

2018/10/14

#
ananyagarg wrote...
I created these 4 barrier images in the Bullet class, not the Barrier class. Will it make a difference if I move them to the Barrier class?
The problem is that there is no comparison that a bullet can make on a barrier's image if a previous bullet creates that barrier's image. Best would be to have a method in the Barrier class that a bullet can call on a barrier it hits:
public void hit()
{
    // set next image or remove
}
Try to keep only Bullet stuff in the Bullet class and only Barrier stuff in the Barrier class. With the method, the only field you would need is one to track number of hits. The class would simply be:
public class Barrier extends Actor
{
    private int hitCount = 0;
    
    public void hit()
    {
        if (++hitCount < 4) setImage("rock"+hitCount+".png");
        else getWorld().removeObject(this);
    }
}
In the Bullet class you would have:
private void hitBarrier()
{
    Barrier barrier = (Barrier)getOneIntersectingObject(Barrier.class);
    if (barrier != null) barrier.hit();
}
ananyagarg ananyagarg

2018/10/14

#
danpost wrote...
ananyagarg wrote...
I created these 4 barrier images in the Bullet class, not the Barrier class. Will it make a difference if I move them to the Barrier class?
The problem is that there is no comparison that a bullet can make on a barrier's image if a previous bullet creates that barrier's image. Best would be to have a method in the Barrier class that a bullet can call on a barrier it hits:
public void hit()
{
    // set next image or remove
}
Try to keep only Bullet stuff in the Bullet class and only Barrier stuff in the Barrier class. With the method, the only field you would need is one to track number of hits. The class would simply be:
public class Barrier extends Actor
{
    private int hitCount = 0;
    
    public void hit()
    {
        if (++hitCount < 4) setImage("rock"+hitCount+".png");
        else getWorld().removeObject(this);
    }
}
In the Bullet class you would have:
private void hitBarrier()
{
    Barrier barrier = (Barrier)getOneIntersectingObject(Barrier.class);
    if (barrier != null) barrier.hit();
}
Hi! Thank you so much for the help. Unfortunately, I am still unable to change the photos once the second bullet hits because when the first bullet hits, he barrier disappears. This is my code in the barrier class:
public void hit(){  
        if (++hitcount < 4){
           setImage("rock"+hitcount+".png");
        }
           else{
               getWorld().removeObject(this);
            }
    }
And this is my code in the Bullet class
public void hitBarrier(){
   Barrier barrier = (Barrier)getOneIntersectingObject(Barrier.class);
    if (barrier !=null){
        barrier.hit();
    }
    else if (getY() == getWorld().getHeight() - 1){
        getWorld().removeObject(this);
    }
danpost danpost

2018/10/14

#
ananyagarg wrote...
I am still unable to change the photos once the second bullet hits because when the first bullet hits, he barrier disappears. << Codes Omitted >>
That is because the bullet is not being removed when hitting a barrier. In the Bullet class hitBarrrier method (as last shown), add after line 4:
getWorld().removeObject(this);
You need to login to post a reply.