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

2013/3/12

if intersect method

leeyong0 leeyong0

2013/3/12

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Submarine here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Submarine extends Actor
{
    /**
     * Act - do whatever the Submarine wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(-1);
    {
        //if method
    }    
  
}
}
I have a question how do you write an if intersect method? I have an actor as a 'Ship' and another actor inside that shoots out method that shoots out 'Missile'. How do you make it as if the missile intersects with the ship. The ship dissapears?
danpost danpost

2013/3/12

#
You seem to be showing the wrong code every time you post a question. When asking about trying to shoot one missle at a time, you should have shown the class with the shoot method in it. Here, you ask about the missle intersecting a ship and should have shown the Missle class. Anyway, I saw your code for the Missle class from you other discussion post and cleaned it up a bit to the following:
import greenfoot.*; 

public class Missile extends Actor
{
    public Missle()
    {
        turn(-90);
    }

    public void act() 
    {
        // climb
        move (-8);
        // check hit ship
        Ship ship = (Ship) getOneIntersectingObject(Ship.class);
        if (ship != null)
        {
            getWorld().removeObject(ship);
            // create explosion here (if desired), or use the following two lines
            getWorld().removeObject(this);
            return;
        }
        // check missed 
        if (getY() == 0)
        {
            getWorld().removeObject(this);
        }
    }    
}
You need to login to post a reply.