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

2017/3/22

Passing a variable from one class of a world to another class in another World.

py-cod py-cod

2017/3/22

#
My Scenario I have two worlds: Screen and Myword. Screen has three three actor classes: Basic, advance and Intermediate. When I click on Basic, variable 'press' becomes one and changes the world to Myworld. I need the press value in ball class of Myworld so that i could use if statement to execute some code according to the selection.i.e if advance was pressed i would assign different value for press to execute different line of code. P.S Myworld Contains two actor classes: Man and Ball.
Super_Hippo Super_Hippo

2017/3/22

#
I think you should simply pass the variable to MyWorld. You also won't need three actor classes if they are only buttons.
//in your menu Screen

//outside methods
private Actor[] difficulties = {new Actor() {}, new Actor() {}, new Actor() {}};


//in constructor - create them
addObject(difficulties[0], 10, 20);
addObject(difficulties[1], 20, 20);
addObject(difficulties[2], 30, 20);
difficulties[0].setImage(new GreenfootImage("Basic", 20, Color.RED, new Color(0,0,0,0))); //maybe you have your own images
difficulties[1].setImage(new GreenfootImage("Intermediate", 20, Color.RED, new Color(0,0,0,0)));
difficulties[2].setImage(new GreenfootImage("Advanced", 20, Color.RED, new Color(0,0,0,0)));


//checking for clicks on them
public void act()
{
    for (int i=0; i<3; i++)
    {
        if (Greenfoot.mousePressed(difficulties[i])
        {
            Greenfoot.setWorld(new MyWorld(i));
        }
    }
}
//in MyWorld

//outside methods
private int difficulty;

//saving the difficulty variable which comes from the Screen World
public MyWorld(int diff)
{
    super(.....);
    difficulty = diff;
}

public int getDifficulty() {return difficulty;}
Then, in your Ball class, you can use this to get the difficulty:
((MyWorld) getWorld()).getDifficulty()
py-cod py-cod

2017/3/22

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Screen here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Screen extends World
{

    /**
     * Constructor for objects of class Screen.
     * 
     */
    private Actor[] difficulties = {new Actor() {}, new Actor() {}, new Actor() {}};
    public Screen()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(difficulties[0], 10, 20);
        addObject(difficulties[1], 20, 20);
        addObject(difficulties[2], 30, 20);
        difficulties[0].setImage(new GreenfootImage("Basic", 20, Color.RED, new Color(0,0,0,0))); //maybe you have your own images
        difficulties[1].setImage(new GreenfootImage("Intermediate", 20, Color.RED, new Color(0,0,0,0)));
        difficulties[2].setImage(new GreenfootImage("Advanced", 20, Color.RED, new Color(0,0,0,0)));
    }
    public void act()
    {
    for (int i=0; i<3; i++)
    {
        if (Greenfoot.mousePressed(difficulties[i]))
        {
            Greenfoot.setWorld(new MyWorld(i));
        }
    }
}

}
I get this error: Constructor MyWorld in class MyWorld cannot be applied to given types; required: no argument; found: int; Reason: Actual and formal argument lists differ in length
Super_Hippo Super_Hippo

2017/3/22

#
You didn't add the constructor to the MyWorld class.
You need to login to post a reply.