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

2014/6/1

Problem for removing at right edge

ShowS7oppeR ShowS7oppeR

2014/6/1

#
Hi :) I've got a little problem with the game I've just made : the penguin throws the balls on me (the crab) and I have to make them bounce at the other side of the map (as if I had to defend my goal). But when the balls reach the other side of the map they don't disappear as it is program in my Ball's code, they just stay here. Does someone know a solution ? :) Thanks :)
public void distroyed(){   
        if(getX()>= getWorld().getWidth()) {
            getWorld().removeObject(this);
        }        
    }
davmac davmac

2014/6/1

#
This:
        if(getX()>= getWorld().getWidth())
Unless your world is unbounded, the X position of an actor can never be >= the world's width. Example: if the world's width is 10, the possible X positions are: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Either make your world unbounded, or change your code to:
public void distroyed(){     
    if(getX() >= getWorld().getWidth() - 1) {
        getWorld().removeObject(this);
    }
}
ShowS7oppeR ShowS7oppeR

2014/6/1

#
Thanks but there's a bug, when a ball arrives at right edge it stops the game and there's a Greenfoot terminal window.. Do you know how to correct it ? Here's my ball's code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Animal
{
    private int dx;
    private int dy;
    private int x;
    private int y;
    public Ball(){
        dx=0;
        dy=0;
        x=509;
        y=280;
    }
    public Ball (int compx, int compy){
        dx = compx;
        dy = compy;
        x=509;
        y=280;
    }
    public void act() {
        //distroyed();
        deplacement();
        changeDx();
        changeDy();
        lose();
    }  
    //public void distroyed(){   
        //if(getX()>= getWorld().getWidth()-1) {
           // getWorld().removeObject(this);
        //}        
    //}
    public void lose() {
        if(getX()<=0){
            Greenfoot.stop();
        }
    }
    public void deplacement()
    {
        if(getX()>= getWorld().getWidth()-1) {
            getWorld().removeObject(this);
        }        
        else if(getX()> 0 && getX() < getWorld().getWidth()-1)
        {
            int newx=getX()+dx;
            int newy=getY()+dy;
            setLocation(newx,newy);
        }
    }
    public void changeDy(){
        if(getY()<=10 || getY()>=getWorld().getHeight()-10) {
        
            dy=-dy ;
    
        }
    }
    public void changeDx(){
        if(canSee(Crab3.class)) {
        
            dx=-dx;
            
        }
    }
}
davmac davmac

2014/6/1

#
it stops the game and there's a Greenfoot terminal window
Can you post the stack trace from the terminal window please?
davmac davmac

2014/6/1

#
Actually, does it say: IllegalStateException: Actor not in world? In that case read this. The problem is that your 'deplacement()' method removes 'this' object from the world, but your act method later calls methods which rely on the object still being in the world.
ShowS7oppeR ShowS7oppeR

2014/6/2

#
Thanks for all your answers, I tried both solutions but no one works :( Here is the whole text message which appears when my ball reaches the right side of the world : java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Ball.deplacement(Ball.java:48) at Ball.act(Ball.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Here is my ball's code :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Animal
{
    private int dx;
    private int dy;
    private int x;
    private int y;
    public Ball(){
        dx=0;
        dy=0;
        x=509;
        y=280;
    }
    public Ball (int compx, int compy){
        dx = compx;
        dy = compy;
        x=509;
        y=280;
    }
    public void act() {
        distroyed();
        deplacement();
        changeDx();
        changeDy();
        lose();
    }  
    public void distroyed(){   
        if(getX()>= getWorld().getWidth()-1) {
            getWorld().removeObject(this);
            return;
        }        
    }
    public void lose() {
        if(getX()<=0){
            getWorld().removeObject(this);
            return;
        }
    }
    public void deplacement()
    {
        if(getX()>= 0 && getX() <= getWorld().getWidth())
        {
            int newx=getX()+dx;
            int newy=getY()+dy;
            setLocation(newx,newy);
        }
    }
    public void changeDy(){
        if(getY()<=10 || getY()>=getWorld().getHeight()-10) {
        
            dy=-dy ;
    
        }
    }
    public void changeDx(){
        if(canSee(Crab3.class)) {
        
            dx=-dx;
            
        }
    }
}
Thanks for helping me.
davmac davmac

2014/6/2

#
Your deplacement method calls getX() and getY() and so on, which happens after 'this' object has been removed from the world (in the distroyed() method). Because the relevant code is not in the act() method, you need to use the second method outlined in the link I gave you - that is, check whether the world is null before you use getX(), getY() etc. eg:
    public void deplacement()  
    {  
        if(getWorld() != null && getX()>= 0 && getX() <= getWorld().getWidth())  
        {  
              // ... and so on
By the way, "destroyed" is spelled with an 'e'.
ShowS7oppeR ShowS7oppeR

2014/6/4

#
It worked, thanks so much! :)
You need to login to post a reply.