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

2017/5/7

Detection Problem

dirtyninja1 dirtyninja1

2017/5/7

#
I have a school project with Greenfoot and i need to have LittleRedCap killed by an explosion, but i cant get it to work. I also have a scoreboard which need to get a negative score so my gameover method will start. I am getting a nulpointer error and an actor not in world error.
public void killLittleRedCap() {
        Actor b = getOneIntersectingObject(LittleRedCap.class);
        if( b!= null) {
            Actor actor = getOneObjectAtOffset(0, 0, LittleRedCap.class);
            if(actor.getWorld() != null) {
                getWorld().removeObject(actor);
            }
            score = score - 100;
            Planet planet = (Planet)getWorld();
            World world = getWorld();
            world.removeObjects(world.getObjects(Scoreboard.class));
            world.addObject (new Scoreboard(score), 75,25);
            if (score<10) {
                world.addObject(new Announcement(),300,300);
                Greenfoot.stop();
            }
        }
    }
LittleRedCap is not in contact with the explosion and still the score goes down by 100 (Before i broke it and still worked).Right now im completely lost on what to do
danpost danpost

2017/5/7

#
Please remove lines 4, 5, 7 and 9. Then, show the entire class for help on the error(s).
dirtyninja1 dirtyninja1

2017/5/7

#
danpost wrote...
Please remove lines 4, 5, 7 and 9. Then, show the entire class for help on the error(s).
Thanks Alot BTW!
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * Write a description of class Rock here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Explosion extends Actor
{
    private static final int NUM_FRAGMENTS = 20;
    GifImage explosionGif = new GifImage("explosion1.gif");
    private int timer = 100;
    protected int score = 0;
    /**
     * Act - do whatever the Rock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage( explosionGif.getCurrentImage() );
        String key = Greenfoot.getKey();
        if (timer>0)
        {
            timer--;
            if(timer == 0) explode();

        }
        killLittleRedCap();
    }

    public void explode()
    {
        placeDebris (getX(), getY(), NUM_FRAGMENTS);
        getWorld().removeObject(this);
    }

    private void placeDebris(int x, int y, int numFragments)
    {
        for (int i=0; i < numFragments; i++) {
            getWorld().addObject ( new ExplosionDebris(), x, y );
        }
    }

    public void killLittleRedCap() {
        score = score - 100;
        World world = getWorld();
        world.removeObjects(world.getObjects(Scoreboard.class));
        world.addObject (new Scoreboard(score), 75,25);

    }

}


i copied alot of things from the internet. I still have the same errors. What i want the method to do is, when i hit the explosion with littleredcap, that littleredcap gets removed and that my score goes down by 100 which activates a gameover announcement.
dirtyninja1 dirtyninja1

2017/5/7

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

/**
 * Write a description of class Rock here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Explosion extends Actor
{
    private static final int NUM_FRAGMENTS = 40;
    GifImage explosionGif = new GifImage("explosion1.gif");
    private int timer = 100;
    protected int score = 0;
    /**
     * Act - do whatever the Rock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage( explosionGif.getCurrentImage() );
        String key = Greenfoot.getKey();
        if (timer>0)
        {
            timer--;
            if(timer == 0) explode();

        }
        killLittleRedCap();
    }

    public void explode()
    {
        placeDebris (getX(), getY(), NUM_FRAGMENTS);
        getWorld().removeObject(this);
    }

    private void placeDebris(int x, int y, int numFragments)
    {
        for (int i=0; i < numFragments; i++) {
            getWorld().addObject ( new ExplosionDebris(), x, y );
        }
    }

    public void killLittleRedCap() {
       if (canSee(LittleRedCap.class)){
            score = score - 100; 
            Planet planet = (Planet)getWorld();
            World world = getWorld();
            world.removeObjects(world.getObjects(Scoreboard.class));
            world.addObject (new Scoreboard(score), 75,25);

            if (score<0){
                world.addObject(new Announcement("Game over \n score= " + score,400,400,100,100,100,120),300,300);
                Greenfoot.stop();
            }
        }
    }
     public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }

}


Changed a few things with help from my classmate. I keep getting an error with getOneObjectAtOffset
dirtyninja1 dirtyninja1

2017/5/7

#
solved it! thanks for the help!
You need to login to post a reply.