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

2014/5/1

Enemies code not working

GamesGrinder1998 GamesGrinder1998

2014/5/1

#
For some reason my enemies bullet is not being removed at the world edge. Can someone please help, here is the code for it...
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class DarkPower here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DarkPower extends Actor
{
    public DarkPower(int rotation)
    {
        setRotation(rotation);
    }

    /**
     * Act - do whatever the DarkPower wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(2);
        tryToEat(); 
    }

    public void tryToEat()
    {
        if (canSee(Starfighter.class) )
        {
            eat(Starfighter.class);
            gameOver();
        }
        if (canSee(Starfighter2.class) )
        {
            eat(Starfighter2.class);
            gameOver();
            Label1 label1 = new Label1();
            getWorld().addObject(label1, 450, 350);
        } 
        if (canSee(Starfighter3.class) )
        {
            eat(Starfighter3.class);
            gameOver();
            Label2 label2 = new Label2();
            getWorld().addObject(label2, 450, 350);
        } else {
            if(atWorldEdge())
            {
                getWorld().removeObject(this);
            }   
        }
    }

    public void gameOver()
    {
        Greenfoot.stop();
        GameOver gameover = new GameOver();
        getWorld().addObject(gameover, getWorld().getWidth()/2, getWorld().getHeight()/2);
        Greenfoot.playSound("gameover.wav");
        Greenfoot.stop();
        GameOver2 gameover2 = new GameOver2();
        getWorld().addObject(gameover2, getWorld().getWidth()/2, getWorld().getHeight()/2);
        Greenfoot.playSound("gameover.wav");
    }

    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 boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }

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

2014/5/1

#
This:
        if(getX() < 10 || getX() > getWorld().getWidth() + 10) 
How would it be possible for the position to be the width of the world plus 10? ;)
erdelf erdelf

2014/5/1

#
I am sure he wanted
    if(getX() < 10 || getX() > getWorld().getWidth() - 10)   
but, isnt it possible in an unbounded world to be at a greater position than the world's size `?
davmac davmac

2014/5/1

#
isnt it possible in an unbounded world to be at a greater position than the world's size
It is, but I am assuming the world is bounded. The 'getX() < 10' part of the condition expression suggests that the object should be removed as it nears the edge of the world, not after it has moved past the edge of the world.
erdelf erdelf

2014/5/1

#
ok good i was just confused for a moment
GamesGrinder1998 GamesGrinder1998

2014/5/1

#
davmac wrote...
This:
        if(getX() < 10 || getX() > getWorld().getWidth() + 10) 
How would it be possible for the position to be the width of the world plus 10? ;)
i tried that but it still doesn't work, here is the code...
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class DarkPower here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DarkPower extends Actor
{
    public DarkPower(int rotation)
    {
        setRotation(rotation);
    }

    /**
     * Act - do whatever the DarkPower wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(2);
        tryToEat(); 
    }

    public void tryToEat()
    {
        if (canSee(Starfighter.class) )
        {
            eat(Starfighter.class);
            gameOver();
        }
        if (canSee(Starfighter2.class) )
        {
            eat(Starfighter2.class);
            gameOver();
            Label1 label1 = new Label1();
            getWorld().addObject(label1, 450, 350);
        } 
        if (canSee(Starfighter3.class) )
        {
            eat(Starfighter3.class);
            gameOver();
            Label2 label2 = new Label2();
            getWorld().addObject(label2, 450, 350);
        } else {
            if(atWorldEdge())
            {
                getWorld().removeObject(this);
            }   
        }
    }

    public void gameOver()
    {
        Greenfoot.stop();
        GameOver gameover = new GameOver();
        getWorld().addObject(gameover, getWorld().getWidth()/2, getWorld().getHeight()/2);
        Greenfoot.playSound("gameover.wav");
        Greenfoot.stop();
        GameOver2 gameover2 = new GameOver2();
        getWorld().addObject(gameover2, getWorld().getWidth()/2, getWorld().getHeight()/2);
        Greenfoot.playSound("gameover.wav");
    }

    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 boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }

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

2014/5/1

#
did u even change something `? this should work line 68-71
if(getX() < 10 || getX() > getWorld().getWidth() - 10)   
            return true;  
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)  
            return true;  
You need to login to post a reply.