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

2012/12/11

Removing all objects

1
2
bbwf bbwf

2012/12/11

#
Hey guys, I was wondering how would I go about removing all objects if something equals a null? Right now in my game I have an aligator chasing frogs and trying to kill the frogs. If the camel hits the frog than the camel changes the image to "Player 2 wins" and goes to the center of the screen. The only problem I'm having is I would like to remove all of the 30 or so frogs that are on the screen also. Please help? Here is my code. import greenfoot.*; import java.util.List; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Camel here. * * @author (your name) * @version (a version number or a date) */ public class Camel extends Mover { /** * Act - do whatever the Camel wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkForArrowKeys(6); checkEdge(); Actor Camel=getOneIntersectingObject(Aligator.class); if(Camel!=null) { getWorld().removeObject(Camel); setImage(new GreenfootImage("Player2.jpg")); setLocation(425, 275); GreenfootImage img = getImage(); img.scale(850,550); removeObjects(getObjects(Frog.class)); //List objects =getObjects(null); //removeObjects(Frog); //Greenfoot.stop(); } } public void checkForArrowKeys (int adjust) { if ( Greenfoot.isKeyDown("a") ) { setLocation(getX() - adjust, getY()); } if ( Greenfoot.isKeyDown("d") ) { setLocation(getX() + adjust, getY() ); } if ( Greenfoot.isKeyDown("w") ) { setLocation(getX() , getY() - adjust ); } if ( Greenfoot.isKeyDown("s") ) { setLocation(getX() , getY() + adjust ); } } public void checkEdge() { int w=getWorld().getWidth(); int h=getWorld().getHeight(); setLocation((getX()+w)%w, ((getY()+h)%h)); } }
Gevater_Tod4711 Gevater_Tod4711

2012/12/11

#
if you want to remove all objects do this:
//in world;
removeObjects(getObjects(null));
//or from an actor;
getWorld().removeObjects(getWorld().getObjects(null));
If you just want to remove the frogs (or any other class) write the classname with .class like this:
removeObjects(getObjects(aClass.class));
vonmeth vonmeth

2012/12/11

#
getWorld().removeObjects(getWorld().getObjects(Frog.class));
Should work.
bbwf bbwf

2012/12/11

#
Thank you so much! Now how would I make it so ifevery frog equals a null it ends the game?
danpost danpost

2012/12/11

#
References to Frog objects can equal 'null', but Frog objects themselves cannot. What you are asking is: if there are not any Frog objects in the world, the game ends.
if(getWorld().getObjects(Frog.class).isEmpty()) // end game
bbwf bbwf

2012/12/11

#
If I do this I get an error: java.lang.NullPointerException at Frog.act(Frog.java:55) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) java.lang.NullPointerException at Frog.act(Frog.java:55) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) java.lang.NullPointerException at Frog.act(Frog.java:55) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194)
vonmeth vonmeth

2012/12/11

#
Erm, that line danpost provided you should work fine. What else did you add?
Zamoht Zamoht

2012/12/11

#
What is on the line 55 in your frog class?
danpost danpost

2012/12/11

#
Please re-post your code using the 'code' tab below the 'Post a reply' input box. If you cannot use the tab, then tag the code by prefixing it with and postfixing it with Do not include the spaces in between the characters. Also, press Control-Shift-I before inserting (or copy/pasting) the code.
bbwf bbwf

2012/12/12

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Frog here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Frog extends Mover
{
    /**
     * Act - do whatever the GoldBall wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
int shotstaken=0;
int count = 0; 
int  health=100;
public Frog()
{
  setRotation(Greenfoot.getRandomNumber(360));

}
    public void act()
        {
        GreenfootImage img = getImage();
        img.scale(60,60);
        setImage(img);

            if(atWorldEdge() || atWorldEdge2() || atWorldEdge3() || atWorldEdge4())
            {
                setRotation(Greenfoot.getRandomNumber(360));
                move(10);
            }  
            else{
                       
                move(5.0);
            }
            
            //Detects when the bullet hits the ball. When it does, the action follows.
            Actor bullet=getOneIntersectingObject(Bullet.class);
        if(bullet!=null)
      {
          
          
          getWorld().removeObject(bullet);
          shotstaken++;
        }
        
        //Detects when the ball hits the rocket. When it does, the action follows
        Actor Aligator=getOneIntersectingObject(Aligator.class);
        if(Aligator!=null)
        {   
            getWorld().removeObject(this);
        }
        if(getWorld().getObjects(Frog.class).isEmpty()) // end game
        { 
            setImage(new GreenfootImage("player1.jpg"));
          setLocation(425, 275);
          //GreenfootImage img = getImage();
           img.scale(850,550);
            Greenfoot.stop();
        }

       
          //Determines when the rocket explodes.
        if(shotstaken==1)
        {
            //getWorld().addObject(new Explosion(), getX(), getY());
            getWorld().addObject(new Debris(),getX()-20, getY()+10);
            getWorld().addObject(new Debris(),getX()+20,getY()+10);
            getWorld().addObject(new Debris(),getX(),getY()-10);
           getWorld().removeObject(this);
        }
        
        if(health<=0)
        {
           // getWorld().addObject(new Explosion(),getX(),getY());
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2012/12/12

#
Shouldn't you be looking for Frog objects (lines 55 to 62) from the Aligator class?
bbwf bbwf

2012/12/12

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Aligator here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Aligator extends Mover
{
    int shootdelay=0;
    int mindelay=5;
     
    public void act() 
    {
        //Sets the rocket as a smaller image.
       GreenfootImage img = getImage();
      setImage(img);
      img.scale(120,32);
        shootdelay++;
        if(Greenfoot.isKeyDown("up"))
        {
            move(5);
             int w=getWorld().getWidth();
             int h=getWorld().getHeight();
             setLocation((getX()+w)%w, ((getY()+h)%h));
        }
              if(getWorld().getObjects(Frog.class).isEmpty()) // end game
        { 
            setImage(new GreenfootImage("player1.jpg"));
          setLocation(425, 275);
         // GreenfootImage img = getImage();
           img.scale(850,550);
            Greenfoot.stop();
        }
          if(getWorld().getObjects(Debris.class).isEmpty()) // end game
        { 
            setImage(new GreenfootImage("player1.jpg"));
          setLocation(425, 275);
         // GreenfootImage img = getImage();
           img.scale(850,550);
            Greenfoot.stop();
        }
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-4);
        }
        
        if(Greenfoot.isKeyDown("right"))
        {
            turn(4);
        }
        if(Greenfoot.isKeyDown("space"))
        {  
            shoot();
        }
      
    }
//Shoots the bullet
    public void shoot()
    {
        if(shootdelay >= mindelay)
        {
            int rot = getRotation()-10;
            int xOffset = (int)(40 *Math.cos(Math.toRadians(rot)));
            int yOffset = (int)(40 *Math.sin(Math.toRadians(rot)));
            Bullet b = new Bullet(getRotation());

            getWorld().addObject(b, getX()+xOffset, getY()+yOffset);
            shootdelay = 0;
 
        }       
    }
}
Nothing happens.
bbwf bbwf

2012/12/12

#
Now all it does is turn my Alligator into a picture and then centers it and stops the world, only if one frog dies to.
danpost danpost

2012/12/12

#
I guess I am a little confused: first you ask how to remove all the frogs; then you ask how check to see if no frogs are in the world so you could end the game. Are these two different situations (one where you remove them all and the other where they are being removed some other way)?
bbwf bbwf

2012/12/12

#
Well sorry for the confusion. In my game if the Alligator hits the Camel than the Alligator dies and the Camel turns into a picture (that fills the whole screen) and the picture says Player two wins( because player 2 is controlling the Camel and is trying to take out the Alligator before it gets all of the frogs). Now I would like it to happen if the Alligator hits all of the Frogs on the screen it will do something like Camel and turn into a picture that says "Player 1 wins". The only problem with this is this is not working for me.
There are more replies on the next page.
1
2