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

2016/11/17

Reference method from a world in a separate world

Samas Samas

2016/11/17

#
I want to reference a setupLevel() method in my world Iron1. This is the code from my world IronManMode
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class HoneyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class IronManMode extends World
{
    int level;
    Actor Pizza;
    Actor Sean;
    Actor Wolf;
    Actor PackLeader;
   
    /**
     * Constructor for objects of class HoneyWorld.
     * 
     */
    public IronManMode()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
         
        setupLevel(0);
    }
    public void setupLevel(int lvl)
    {
        removeObjects(getObjects(null));
         
        level = lvl;
         if(level==0)
            {
            addObject( new Intro(), 400, 300 );
            addObject( new IronButton1(), 400, 450);
             showText( "Play", 400, 450);
            addObject( new Pizza(), 1,1);
            }
        else if (level == 1)
            {
            addObject( new Sean(), 400, 300 );
            addObject( new Wolf(), ( int ) ( 300*Math.random() ), (int) ( 200*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new IceCube(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            
            }
        else if (level == 2)
            {
            addObject( new LoadingScreen1(), 400, 300 );
            addObject( new IronButton2(), 700, 500);
             showText( "Play", 700, 500);
            addObject( new DumbSheep(), 10, 450 );
            addObject( new Rock(), 400, 450 );
            }
        else if (level == 3)
            {
            addObject( new Sean(), 400, 300 );
            addObject( new Wolf(), ( int ) ( 300*Math.random() ), (int) ( 200*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Rock(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Rock(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Rock(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Rock(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            }
        else if ( level == 4 )
            {
            addObject( new LoadingScreen3(), 400, 300 );
            addObject( new IronButton3(), 400, 450);
             showText( "Play", 400, 450);
            }
        else if ( level == 5 )
            {
            addObject( new Sean(), 400, 300 );
            addObject( new PackLeader(), ( int ) ( 300*Math.random() ), (int) ( 200*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Rock(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Rock(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Rock(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Rock(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            }
        else if ( level == 6 )
        {
         addObject( new LoadingScreen4(), 400, 300 );
            addObject( new IronButton4(), 400, 450);
             showText( "Play", 400, 450);
        }
        else if ( level == 7 )
            {
            addObject( new Sean(), 400, 300 );
            addObject( new PackLeader(), ( int ) ( 300*Math.random() ), (int) ( 200*Math.random() ) );
            addObject( new Wolf(), ( int ) ( 500*Math.random() + 300 ), (int) (200*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Rock(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Rock(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Rock(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Rock(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            }
    }
       
               
    }
This is the code from Iron1
    import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Iron1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Iron1 extends World
{
    
    /**
     * Constructor for objects of class Iron1.
     * 
     */
    public Iron1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super( 800, 600, 1);
        addObject( new Sean(), 400, 300 );
            
            addObject( new Wolf(), ( int ) ( 300*Math.random() ), (int) ( 200*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            addObject( new Pizza(), ( int ) ( 800*Math.random() ), (int) ( 600*Math.random() ) );
            
            addObject( new PizzaCounter(), 725, 50);
        
        act();
        
    }
    public void act()
        {
        World IronManMode;
        
        
        List Sean = getObjects(Sean.class);
        List MenuButton = getObjects(MenuButton.class);
        if (Sean.isEmpty()  & MenuButton.isEmpty())
            {
            Greenfoot.setWorld(new Iron1());
            } 
           List Pizza = getObjects(Pizza.class);
        if (Pizza.isEmpty())
            {
            (IronManMode).setupLevel(0);
            }
        }
    }
I would like to setup a certain level in IronManMode from Iron1
Super_Hippo Super_Hippo

2016/11/17

#
(IronManMode)getWorld().setupLevel(0);
Well, since I am never sure which ( ) are needed, it could also be one of the following... But test the first one first ;)
(IronManMode)(getWorld()).setupLevel(0);
((IronManMode)(getWorld())).setupLevel(0);
Samas Samas

2016/11/18

#
neither worked I get an error that says that it can't find a method getWorld()
Super_Hippo Super_Hippo

2016/11/18

#
Oh sorry, I didn't see that Iron1 is a world class... Maybe this could work then:
IronManMode newWorld = new IronManMode();
newWorld.setupLevel(0); //right now not needed if it is 0
Greenfoot.setWorld(newWorld);
You could also add a new constructor: (or just change it to it if your scenario does not start in the IronManMode class)
public IronManMode(int level) //added parameter
{
    super(800, 600, 1); 
      
    setupLevel(level);
}
And then, you could use the following instead of the first code:
IronManMode newWorld = new IronManMode(0);
Greenfoot.setWorld(newWorld);
You need to login to post a reply.