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

2021/3/26

Error object is hit

SanderNeele SanderNeele

2021/3/26

#
Hello, When I shoot an animal with a spear I want the spear to stop when it hits the animal. I get an error when I do that when I shoot an animal. Except for the last animal in the code (cheetah) Raakt (is a dutch word) = Hits
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Botermes here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Speer extends Objecten
{
        /**
     * Move forward, not to slowly
     */
    
    public void act() {
        move(10);
        raaktZebra();
        raaktLeeuw();
        raaktTijger();
        raaktStruisvogel();
        raaktNeushoorn();
        raaktZeehond();
        raaktWolf();
        raaktCheetah();
    }   
    
    /**
     * Set the objectreference to the gardener
     * @param g The objectreference to the gardener
     */
    public void raaktZebra()
    {
        if (isTouching (Zebra.class))
        {
            World world = getWorld();
            world.removeObject  (this);
        } 
    }
        public void raaktLeeuw()
    {
        if (isTouching (Leeuw.class))
        {
            World world = getWorld();
            world.removeObject  (this);
        } 
    }
        public void raaktTijger()
    {
        if (isTouching (Tijger.class))
        {
            World world = getWorld();
            world.removeObject  (this);
        } 
    }
        public void raaktStruisvogel()
    {
        if (isTouching (Struisvogel.class))
        {
            World world = getWorld();
            world.removeObject  (this);
        } 
    }
            public void raaktNeushoorn()
    {
        if (isTouching (Neushoorn.class))
        {
            World world = getWorld();
            world.removeObject  (this);
        } 
    }
            public void raaktZeehond()
    {
        if (isTouching (Zeehond.class))
        {
            World world = getWorld();
            world.removeObject  (this);
        } 
    }
            public void raaktWolf()
    {
        if (isTouching (Wolf.class))
        {
            World world = getWorld();
            world.removeObject  (this);
        } 
    }
            public void raaktCheetah()
    {
        if (isTouching (Cheetah.class))
        {
            World world = getWorld();
            world.removeObject  (this);
        } 
    }
    public void removeCarIfHit()
    {
    if (isTouching(Zeehond.class)) {
        //setImage("bullet-gone.fw.png");
        Actor actor = getOneIntersectingObject(Zeehond.class);  
        getWorld().removeObject(this);
    }
    
}
}


        


danpost danpost

2021/3/26

#
Use:
if (getWorld() != null)
for each raakt...() call in act.
You need to login to post a reply.