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

2013/5/1

how to call method from another world

ronzhi ronzhi

2013/5/1

#
Hi, i have 2 world "Island" as the main world and "Info" which i implement danpost's crayon babies as the info world that come out when we click a button. the info is array string from "Information" class. Everything is going well but when level is updated the current information is still displayed. How can i call resetK method from "Island" world, or how to reset this world from "Island" world? this is my Info World
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Info here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Info extends World
{
    World toWorld = null;
    
    private String[] E = Information.I;
    /**
     * Constructor for objects of class Info.
     * 
     */
    public Info(int value, World inWorld)
    //public Info(int value)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        //super(750, 500, 1);
        super(1000, 600, 1);
        setMainImage(); // create a base image to work on
        resetK();
        // add the appropriate information to the base image
        switch (value)
        {
            //case 0: imageZero(); break; // the MainWorld info
            case 1: imageOne(); break; // the MenuWorld info
            //case 2: imageTwo(); break; // the ImageWorld info
        }
        toWorld = inWorld; // save world to return to
    }
    
    /**
     * Method setMainImage: creates the base image that information will be added to
     */
    private void setMainImage()
    {
        // create the image for the background
        GreenfootImage main = new GreenfootImage(getWidth(), getHeight());
        main.setColor(Color.black);
        main.fill(); // set a flat black background
        // create images and draw them on the background
        GreenfootImage img = new GreenfootImage("INFORMATION", 36, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 50);
        img = new GreenfootImage("(you are now in InfoWorld)", 18, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 90);
        // draw separator lines on the background
        main.setColor(Color.yellow);
        main.drawLine(0, 125, 1000, 125);
        main.drawLine(0, 420, 1000, 420);
        setBackground(main); // set the background image
    }
    
    /**
     * Method imageOne: adds the information about MenuWorld to the base image
     */
    //private void imageOne(int x)
    private void imageOne()
    {
        //String[] E = Knowledges.K;
        // retrieve the background image
        GreenfootImage main = getBackground();
        // create text images and draw them on the background
        GreenfootImage img = new GreenfootImage(E[0], 18, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 150);
        img = new GreenfootImage(E[1], 18, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 200);
        img = new GreenfootImage(E[2], 18, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 250);
        img = new GreenfootImage(E[3], 18, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 300);
        img = new GreenfootImage(E[4], 18, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 350);
        img = new GreenfootImage("Click anywhere to proceed to MenuWorld", 18, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 440);
        
    }
        
    /**
     * Method act: returns to calling world when mouse is clicked
     */
    public void act()
    {
        if (Greenfoot.mouseClicked(null)) Greenfoot.setWorld(toWorld); // change to calling world when the mouse is clicked
    }
    
    public void resetK()
    {
        // retrieve the background image
        GreenfootImage main = getBackground();
        // create text images and draw them on the background
        GreenfootImage img = new GreenfootImage("Information 1", 18, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 150);
        img = new GreenfootImage("Information 2", 18, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 200);
        img = new GreenfootImage("Information 3", 18, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 250);
        img = new GreenfootImage("Information 4", 18, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 300);
        img = new GreenfootImage("Information 5", 18, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 350);
        img = new GreenfootImage("Click anywhere to proceed to MenuWorld", 18, Color.yellow, Color.black);
        main.drawImage(img, (main.getWidth() - img.getWidth()) / 2, 440);
    }
    
}
danpost danpost

2013/5/1

#
Just create a new instance of InfoWorld each time you need it (do not recall the same world). You can create a new instance with 'new Info(0, this)' or 'new Info(0,getWorld())' (depending on if the line is in your world class code or in an actor class code). Using '0' for the int parameter will avoid having the 'imageOne' method execute.
ronzhi ronzhi

2013/5/1

#
yes that works danpost,but in my button Greenfoot.setWorld(new Info(1, getWorld()) so it still display the previous info maybe i ask it wrong, how to reset info in imageOne with default when level is updated? as my info is array string from "Information" class. is it in the world or infobutton class should i dig?
danpost danpost

2013/5/1

#
Maybe you need to use a variable instead of the '1' in the button class call to 'new Info(???, getWorld()). Using the level number would be a prime candidate for that variable if you just need to display different text for each level. In the Info world class, you could probably do without the switch and have one method to create the display for any level (depending on the value of the int parameter -- level number).
ronzhi ronzhi

2013/5/1

#
great help danpost as always thanks
You need to login to post a reply.