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

2013/5/25

How To Get Object To Only Detect Intersecting Object Once

JackStieben JackStieben

2013/5/25

#
Hello guys, I am currently working on a home project where you control a spaceship and you have to dodge meteors. At the top, I have 3 circles representing your health. If you hit a meteor one of the circles disappear and when all the circles are gone, you lose .I have an if statement to detect if the rocket ship is intersecting with a meteor, the score will increase (the score is not visible to players) and when the "score == 1", one circle is gone and when "score == 2" another disappears and so on. The problem is, the rocket ship detects the intersection too fast, so all the circles disappear at once. How do you make it so that only 1 circle will disappear for every meteor it hits? Thank you for any help.
JackStieben JackStieben

2013/5/25

#
Here's my code for the rocket.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends Actor
{
    private int score;
       public Rocket(){
        GreenfootImage i = getImage();
        i.scale(55,40);
       score = 0;
    }
       
    public void act() 
    {
        if(Greenfoot.isKeyDown("up")){
            setLocation(getX(),getY()-5);
        }
        if(Greenfoot.isKeyDown("down")){
             setLocation(getX(),getY()+5);
        }
        if(getIntersectingObjects(Meteors.class).size()>0){
            score++;
    }
        if(score==1){
            getWorld().removeObjects(getWorld().getObjects(Life3.class)); 
        }
         if(score==2){
            getWorld().removeObjects(getWorld().getObjects(Life2.class)); 
        }
         if(score==3){
            getWorld().removeObjects(getWorld().getObjects(Life1.class)); 
            getWorld().addObject(new Loser(), 374,421);
        }
    }    
}
JackStieben JackStieben

2013/5/25

#
Loser.class is the message that pops up when you lose, and the Life1,Life2,and Life3 all represent the lives
danpost danpost

2013/5/25

#
You do not want to execute the last three 'if' statements every act; only when the score increases. So move those 'if' statements up inside the previous one right after increasing the score value.
You need to login to post a reply.