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

2013/6/11

Can't remove object

wslade wslade

2013/6/11

#
I am trying to remove a GameOver object but I can't get it to work. Also my fell() method isn't working. What am I doing wrong? import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bob here. * * @author (your name) * @version (a version number or a date) */ public class Bob extends Actor { private int speed = 6; //declares the speed variable private int vspeed = 0; // declares the vspeed variable private int acceleration = 1; // declares the acceleration variable private static final int jumpStrength = 12; private GameOver GameOver; /** * Act - do whatever the Bob wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeys(); // runs the check key method fall(); // runs the fall method CheckFall();// runs the CheckFall method dead();//runs the dead method fell();//runs the fell method win(); //runs the win method } private void checkKeys()// declares the checkKeys method { if (Greenfoot.isKeyDown("d"))// checks for the d key to be pressed { setImage("polar-bear.png"); moveRight(); // moves right according ot the public void } if (Greenfoot.isKeyDown("a"))// checks for the a key to be pressed { setImage("polar-bearCopy.png"); moveLeft(); // moves left according to the public void } if (Greenfoot.isKeyDown("space"))//checks for the space key to be pressed { if (onGround()|| onMovingGround()) jump(); } if (Greenfoot.isKeyDown("w"))//checks for the space key to be pressed { if (onGround()|| onMovingGround()) jump(); } } public void moveLeft()// declares moveLeft method { setLocation(getX()- speed,getY());// moves Bob left at the rate of the speed integer earlier declared } public void moveRight()// declares the moveRight method { setLocation(getX() + speed,getY()); //moves Bob right at the rate of the speed integer earlier declared } public void fall()// declares the fall method { setLocation(getX(),getY()+vspeed); // creates the falling action vspeed = vspeed + acceleration; // makes the fall look more natural } public boolean onGround()// declares the on ground boolean { Object under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);// checks for ground underneath the bear return under!= null; } public void CheckFall()// declares the check fall method { { if (onGround() || onMovingGround()) { vspeed=(0); // sets the vspeed to 0 if the onGround boolean returns true } else { fall(); // falls if he is not on ground } Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class); if((under != null)) setLocation(getX(),under.getY() - under.getImage().getHeight()-5); } } private void jump()// declares jump method { vspeed= (-jumpStrength); Greenfoot.playSound("jump1.wav"); fall(); } public boolean onMovingGround() { Object under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, MovingGround.class);// checks for ground underneath the bear return under!= null; } public void dead() { if (getOneIntersectingObject(Spear.class) !=null)// says what to do if the Spear hits Bob { getWorld().addObject(new Death(),getX(),getY()); // adds the death picture effect Actor Spear = getOneIntersectingObject(Spear.class); // tells when the bear and spear intersect getWorld().removeObject(Spear); // removes Spear from the world Actor Death = getOneIntersectingObject(Death.class); Greenfoot.playSound("death1.wav");// plays the death sound Greenfoot.delay(30); // delays whats happening so that the death shows getWorld().addObject(new GameOver(),400,300); //adds GameOver Greenfoot.delay(100); //Delays Greenfoot Actor GameOver = getOneIntersectingObject(GameOver.class); getWorld().removeObject(GameOver);//removes game over getWorld().removeObject(Death); // removes the death symbol setLocation(44,546); // puts the bear back at the beggining Greenfoot.start(); //automaticly starts the game } } public void fell() { if(getY()>=getWorld().getHeight()+1) { dead(); } } public void win() { if (getOneIntersectingObject(SealHole.class) !=null) { getWorld().addObject(new YouWin(),400,300); //adds the YouWin to the center of the screen //play sound getWorld().addObject(new Winner(),400,300); //greenfoot stop } } }
Zamoht Zamoht

2013/6/11

#
The GameOver is not an intersecting object so change these lines: getWorld().addObject(new GameOver(),400,300); //adds GameOver Greenfoot.delay(100); //Delays Greenfoot Actor GameOver = getOneIntersectingObject(GameOver.class); getWorld().removeObject(GameOver);//removes game over To: GameOver gameOver = new GameOver(); getWorld().addObject(gameOver,400,300); //adds GameOver Greenfoot.delay(100); //Delays Greenfoot getWorld().removeObject(gameOver);//removes game over Also I noticed that your fall method gets called twice in the act method. First you call fall() and then you call CheckFall() which calls fall if the player is not on the ground. Though I don't get why fell() doesn't work. And next time, please send your code within a code block (when your write a post you can click in the button labeled "code" and add your code in there).
danpost danpost

2013/6/11

#
Unless you have an unbounded world, one with 'super(int, int, int, false)', the y-coordinate of your actor will never reach 'getWorld().getHeight()+1'. The best it can be is 'getWorld().getHeight()-1'.
wslade wslade

2013/6/11

#
Thanks! It works perfectly now :)
wslade wslade

2013/6/13

#
Okay. I have another issue. I have two platforms that I have which I want to be able to move up and down when I place them into the program. Right now I have 2 platforms that appear in the beginning (in the world constructor) at the lower right so these move up and down but if I add platforms in a different area after the world is constructed then don't move. How can I change the program so they will also move up and down?
public class MovingGround extends Actor
{
    /**
     * Act - do whatever the MovingGround wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int Mspeed = 1;
    private int up = 572;
    private int down = 470;
    public void act() 
    {
       setLocation (getX(),getY()+Mspeed);
       Actor actor = getOneIntersectingObject(null);
       if(actor!=null)
       {
           actor.setLocation(actor.getX(),actor.getY() + Mspeed);
        }
        if (TurningPoint())
        {
            Mspeed = -Mspeed;
        }
        
    }
    public boolean TurningPoint()
    {
        return (getY()>= up||getY()<=down);
    }
}
danpost danpost

2013/6/13

#
One problem is that you are missing a check within the 'TurningPoint' method. It should be:
public boolean TurningPoint()
{
    return (Mspeed > 0 && getY() >= up) || (Mspeed < 0 && getY() <= down);
}
wslade wslade

2013/6/13

#
Thanks. That did the trick :)
You need to login to post a reply.