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

2013/12/12

Small Problem

csr1010 csr1010

2013/12/12

#
This is a minor problem, when I shoot and hit a tree it stops the world. I made a couple changes to my code that could have caused this. It worked fine before. Here is my code for the bullet, (where i changed things)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public boolean exsists = true;
    public static int kills;
    private int gunCoolDown;
    private int speed = -15;
    public Survivor ethan;
    public Bullet(int direction)
    {
        setRotation(direction); 

    }

    public void act() 
    {
        move(speed);
        bulletTree();
        if ((this) !=null)
        {

            if (canSee (Zombie.class)) 
            {
                kill(Zombie.class);
                exsists = false;
            }
            if (exsists)
            {
                checkEdges();
            }
        }
    }

    public void kill(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        if(actor != null) 
        {
            getWorld().removeObject(actor);
            getWorld().removeObject(this);
            Survivor.score += 1;
        }
    }

    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        return actor != null;
    }

    public void checkEdges()
    {
        if (atWorldEdge())
        {
            getWorld().removeObject(this);
        }
    }

    public boolean atWorldEdge()  
    {  
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)  
            return true;  
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)  
            return true;  
        else  
            return false;  
    } 

    public void bulletTree()
    {
        if (canSee (Tree.class)) 
        {
            getWorld().removeObject(this);
        }
    }

}   
csr1010 csr1010

2013/12/12

#
I think the problem is with lines 27-41 or 57-61
GreenHouse GreenHouse

2013/12/12

#
if ((this) !=null)
is useless; because 'this' can not be null. You want to check whether the object still exists in the world, right? So use instead:
if(getWorld() != null)
You need to login to post a reply.