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

2014/1/24

Need help with a problem! World stops..

Noam001 Noam001

2014/1/24

#
Hello.. I am fairly new with Greenfoot, and after some hours of trying to make a zombie survival game two problems occured. First my world stops when a bullet leaves the edge of the world. Second problem is that nothing seems to happen, when my bullet hits zombie? I hope someone can help me out there.. Here's my coding:
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
{
    private boolean getAngle = true;
    private int shootingCounter = 150;

    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        angleSelect();
        move(10);
        shootingCounter--;
        remove();
        killZombie();
    }

    public void remove()
    {
        if(getX() >= getWorld().getWidth() -10)
            getWorld().removeObject(this);
        else if(getX() <= 5)
            getWorld().removeObject(this);
        else if(getX() >= getWorld().getWidth() -10)
            getWorld().removeObject(this);
        else if(getX() <= 10)
            getWorld().removeObject(this);
    }  

    public void angleSelect()
    {
        if(getAngle == true)
        {
            setRotation(Sniperguy.currentRot);
            getAngle = false;
        }
    }
    
        public boolean amIshot(Class clss)
        {
            Actor actor = getOneObjectAtOffset(0,0, clss);
            return actor !=null;   
        }

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

        }

        public void killZombie()
        {   
            if(amIshot(zombie.class))
            {
                kill(zombie.class);

                getWorld().removeObject(this);
            }
            else
            {
                remove();
            }
        }
    }
davmac davmac

2014/1/24

#
First my world stops when a bullet leaves the edge of the world
Are you getting a stack trace in the terminal window when this happens? I think your code has the problem described here
danpost danpost

2014/1/24

#
You are probably getting an IllegalStateException error during the 'amIshot' method execution at the 'getOneObjectAtOffset' call (line 50) when a bullet reaches an edge of the world. This is because you call 'remove' in the act method before calling 'killZombie' which calls 'amIshot'. To use 'getOneObjectAtOffset' without getting that error, the actor must be in the world, but the 'remove' method does remove it when the bullet reaches an edge. I also noticed that you have another call to 'remove' within the 'killZombie' method. So, you could just remove the 'remove' call from the act method (as one possible solution). An alternative would be to remove the 'else' part from the 'killZombie' method and change the call to it in the act method to 'if (getWorld() != null) killZombie();'. This ensures the actor is still in the world before calling the 'killZombie' method.
You need to login to post a reply.