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

2015/4/26

getOneIntersectingObject problem

zkiwi zkiwi

2015/4/26

#
I have a fairly lame simulation that works mostly, except that sometimes it isn't picking up that the laser beam is intersecting with its target. It's probably something I am doing wrong, but I can't see it. I've included the code for the laser beam.
import greenfoot.*;
import java.util.*; // For Lists

/**
 * Write a description of class LaserBeam here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LaserBeam extends Actor
{
    /**
     * Constructs the LaserBeam, pointing in the right direction
     */
    public LaserBeam(int direction)
    {
        setRotation(direction);
    }
    
    /**
     * Act - do whatever the LaserBeam wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        move(10);
        
        // Vanish if we reach the edge of the world
        World choirWorld = getWorld();
        if (getX() <= 0 || getY() <= 0 || getX() >= choirWorld.getWidth() || getY() >= choirWorld.getHeight())
        {
            List<LaserCat> cats = choirWorld.getObjects(LaserCat.class);
            if (!cats.isEmpty())
            {
                LaserCat theCat = cats.get(0);
                theCat.resetLaser();
            }

            choirWorld.removeObject(this);
        }
        else 
        {
            // Check to see what we've hit
            // If it's the squeaker, remove them
            ChoirSinger choirSqueaker = (ChoirSinger)getOneIntersectingObject(ChoirSinger.class);  
          
            if (choirSqueaker != null && choirSqueaker.Squeak())
            {
                List<LaserCat> cats = choirWorld.getObjects(LaserCat.class);
                if (!cats.isEmpty())
                {
                    LaserCat theCat = cats.get(0);
                    theCat.resetLaser();
                }
                
                choirSqueaker.unblock();
                choirWorld.removeObject(choirSqueaker);
                choirWorld.removeObject(this);
            }
        }
    }    
}

danpost danpost

2015/4/26

#
zkiwi wrote...
sometimes it isn't picking up that the laser beam is intersecting with its target. It's probably something I am doing wrong, but I can't see it.
First, make sure that the laser is actually intersecting the squeaker that you believe should be removed. Then, make sure that what appears to be an unremoved intersector is actually one that squeaks. Line 57, 'choirSqueaker.unblock();' seems odd since you are removing the 'choirSqueaker' from the world two lines later. Lines 50 though 55 seems a bit awkward. There is, I am sure, much easier ways to regulate laser firing -- especially if only one laser object can be in the world at any time.
danpost danpost

2015/4/29

#
Your issue was due to excessive transparencies in your Red_laser image. The method was picking up one of more than one intersectors (the one that did not sing a bad note); so, the one that was supposed to be removed was passed up. After removing the excess transparency from its image, it seemed to be having no problems with detecting and removing the bad note singer.
You need to login to post a reply.