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

2014/1/3

When inserting code for killing player by bullet and than make the action an error views

DaveFenste DaveFenste

2014/1/3

#
Hi, I am not good at programming and I have problem when I insert code with kill method, then when bullet reach the edge of the map error views. Msg error is here and code too.
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 Mover
{
    

    /**
     * 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() 
    {
        move(10);
        remove();
        kill();
    }

    public void remove() {
        if(getX() >= getWorld().getWidth() -1)
            getWorld().removeObject(this);
        else if(getX() <= 1)
            getWorld().removeObject(this);
        else if(getY() >= getWorld().getWidth() -1)
            getWorld().removeObject(this);
        else if(getY() <= 1)
            getWorld().removeObject(this);

    }
    public void kill()
    {
        Actor p2 = getOneIntersectingObject(Player2.class);
        if (p2 != null) {  
            getWorld().removeObject(p2);
        }  

    }
    
}
danpost danpost

2014/1/3

#
You are calling 'remove' from within the 'act' method before calling 'kill'. If 'remove' does remove the actor from the world then 'kill' will error on 'getOneIntersctingObject' because that method require the actor be in the world. Just reversing the calls should fix the problem:
public void act()
{
    move(10);
    kill();
    remove();
}
DaveFenste DaveFenste

2014/1/3

#
danpost wrote...
You are calling 'remove' from within the 'act' method before calling 'kill'. If 'remove' does remove the actor from the world then 'kill' will error on 'getOneIntersctingObject' because that method require the actor be in the world. Just reversing the calls should fix the problem:
public void act()
{
    move(10);
    kill();
    remove();
}
Oh my god...that was very simply and it works thx. Can I have one more question? How can i set to player to shoot i four directions (up, down, right, left) ?
danpost danpost

2014/1/3

#
First, you need to decide what trigger(s) you will use to shoot (mouse or keyboard event(s)? or, timed or randomly spaced? ). Answers to these questions will help determine what is needed for your code.
You need to login to post a reply.