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

2015/12/3

Can't get Proton Wave code to work!

Swordman27 Swordman27

2015/12/3

#
So I'm working on Asteroids, and I have looked up how to do the proton wave checkCollision method and I have it typed exactly as it has shown but its not working.
private void checkCollision()
    {
        int range = getImage().getWidth()/2;
        List asteroids = getObjectsInRange(range, Asteroid.class);
        for (Asteroid a : asteroids)
        {
            a.hit (DAMAGE);
        }
    }
That's the code, here is the error: incompatible types: java.lang.Object cannot be converted to Asteroid
danpost danpost

2015/12/3

#
The list of object returned by 'getObjectsInRange' are held as Object objects -- not as Asteroid objects. The compiler needs to know that the objects taken from the list are actually Asteroid objects. Change line 5 to:
for (Object a : asteroids)
and line 7 to:
((Asteroid)a).hit(DAMAGE);
Swordman27 Swordman27

2015/12/7

#
Thank you very much, danpost! I was very worried I had a much worse problem than that.
Swordman27 Swordman27

2015/12/7

#
Well, I guess there is a bigger problem. When I activate the Proton Wave and it finishes, an error pops up
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
	at greenfoot.Actor.failIfNotInWorld(Actor.java:681)
	at greenfoot.Actor.getObjectsInRange(Actor.java:900)
	at ProtonWave.checkCollision(ProtonWave.java:88)
	at ProtonWave.act(ProtonWave.java:101)
	at greenfoot.core.Simulation.actActor(Simulation.java:594)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:552)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
I know what the error is, but i don't know how to fix it.
danpost danpost

2015/12/7

#
Swordman27 wrote...
Well, I guess there is a bigger problem. When I activate the Proton Wave and it finishes, an error pops up < Error Omitted > I know what the error is, but i don't know how to fix it.
You will need to provide the act method and any methods called before when 'checkCollision' is called.
Swordman27 Swordman27

2015/12/9

#
Here is the code that I have for the Proton Wave:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;

/**
 * A proton wave that expands and destroys things in its path.
 * 
 * @author Michael Kolling
 * @version 0.1
 */
public class ProtonWave extends Actor
{
    /** The damage this wave will deal */
    private static final int DAMAGE = 30;
    
    /** How many images should be used in the animation of the wave */
    private static final int NUMBER_IMAGES= 30;
    
    /** 
     * The images of the wave. This is static so the images are not
     * recreated for every object (improves performance significantly).
     */
    private static GreenfootImage[] images;
    
    /** Current size of the wave */
    private int imageCount = 0;
    
    /**
     * Create a new proton wave.
     */
    public ProtonWave()
    {
        initializeImages();
        setImage(images[0]);
        Greenfoot.playSound("proton.wav");
    }
    
    /** 
     * Create the images for expanding the wave.
     */
    public static void initializeImages()
    {
        if(images == null)
        {
            GreenfootImage baseImage = new GreenfootImage("wave.png");
            images = new GreenfootImage[NUMBER_IMAGES];
            int i = 0;
            while (i < NUMBER_IMAGES) 
            {
                int size = (i+1) * ( baseImage.getWidth() / NUMBER_IMAGES );
                images[i] = new GreenfootImage(baseImage);
                images[i].scale(size, size);
                i++;
            }
        }
    }
    
    public static void forInitializeImages()
    {
        if(images == null)
        {
            GreenfootImage baseImage = new GreenfootImage("wave.png");
            images = new GreenfootImage[NUMBER_IMAGES];
            for(int i = 0; i < NUMBER_IMAGES; i++)
            {
                int size = (i+1) * ( baseImage.getWidth() / NUMBER_IMAGES );
                images[i] = new GreenfootImage(baseImage);
                images[i].scale(size, size);
            }
        }
    }
    
    private void grow()
    {
        if (imageCount >= NUMBER_IMAGES)
        {
            getWorld().removeObject(this);
        }
        else
        {
            setImage(images[imageCount]);
            imageCount++;
        }
    }
    
    private void checkCollision()
    {
        int range = getImage().getWidth()/2;
        List asteroids = getObjectsInRange(range, Asteroid.class);
        for (Object a : asteroids)
        {
            ((Asteroid)a).hit(DAMAGE);
        }
    }
    
    /**
     * Act for the proton wave is: grow and check whether we hit anything.
     */
    public void act()
    {
        grow();
        checkCollision();
    }
}
danpost danpost

2015/12/9

#
You do not want to be calling the 'checkCollision' method if the 'grow' method removes the actor from the world. Make sure the actor is still in the world before calling the 'checkCollision' method (or, in other words, call the 'checkCollision' method only on the condition that the actor is in a world):
if (getWorld() != null) checkCollision();
Swordman27 Swordman27

2015/12/11

#
Thank you! That solved my problem completely!
You need to login to post a reply.