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

2018/5/21

How would i swtich worlds if I want two actors touching each other?

applecider124 applecider124

2018/5/21

#
Sorry if I am getting annoying but im really stuck here. I wanted to switch worldsif two actors are touching each other. Heres what I have.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
     private Counter scoreCounter; 
     public static boolean finish= false; 
   
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1200, 700, 1); 
        popWorld();
        gameOver();
    }
    public void popWorld()
    {
        //Shark
        addObject( new Shark(), 50, 40);
        addObject( new Shark2(), 51, 673);
        //Minnow
        addObject( new Minnow(), 573, 60);
        //Checkpoint
        addObject( new Checkpoint(), 1173,338);
        //TitleScreen
        addObject( new TitleScreen2(), 646,383);
        //Counter
        addObject( new Counter(), 1161,26);
        //Wall
        addObject( new Wall(), 228,339);
        addObject( new Wall(), 908,339);
    }
    public void gameOver()
    {
      Actor a = (Actor) getObjects(Shark.class).get(0);
       if (a != null)
       {
       if (finish==true)
       {
        Greenfoot.setWorld(new NewWorld());
       }
      }
      //Super_Hippo 
      //Sent the new world 
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class NewWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class NewWorld extends World
{

    /**
     * Constructor for objects of class NewWorld.
     * 
     */
    public NewWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        Worldpop();
    }
    public void Worldpop()
    {
         //Obstacle 
        addObject( new Obstacle(), 730,486);
        addObject( new Obstacle(), 857,593);
        addObject( new Obstacle(), 990,484); 
        addObject( new Obstacle(), 749,143);
        addObject( new Obstacle(), 881,273);
        addObject( new Obstacle(), 1010,140); 
    }
}
Thats the new world that I want my world to set to. Thank you!!
danpost danpost

2018/5/21

#
Line 44 is troublesome. You cannot assume that there is a first element in a list you do not create yourself (it is created through code you yourself did not create). If no Shark object is in the world, you will get an IndexOutOfBoundsException error. As long as a Shark object is in the world (the error is not thrown), line 45 will always be true. That is, you will never have null as the value of the actor, a. A more precise gameOver method might be this:
public void gameOver()
{
    if (!getObjects(Shark.class).isEmpty() && finish) Greenfoot.setWorld(new NewWorld());
}
As far as having two actors touching, no Actor subclass codes have been supplied and knowing what actors would touch might help.
You need to login to post a reply.