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

2013/12/20

Actor sets variable of a world he is not in

1
2
S_Felix S_Felix

2013/12/20

#
I'm making a game where I have a button in a World. If I click this button I want it to set the variable "img" in another world to 2. This is the code of the button:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class S2 extends buttons
{
    public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
            if(//?
            {
                // sets the variable img of World "welt" to 2
            }
            getWorld().removeObject(this);
            Greenfoot.setWorld(new Game());
        }
    }    
}
danpost danpost

2013/12/20

#
(1) What world is this S2 object in (class name) (2) Where is your reference to the 'welt' world (3) It seems line 12 has no purpose
Mimagic Mimagic

2013/12/20

#
if i had an actor class named "globalvariable" and within that actor i added a "public static int changeimage = 0;" then you would just have to refer everything to do with the button to the gloabvariable actor. if(globalvariable.changeimage == 1) { setImage("tosomethingelse.png"); } well thats how i do it anyway :p
S_Felix S_Felix

2013/12/20

#
1: the world he is in is called SScreen 2: I just learnd to work with Greenfoot for a very short time at school and also in another language so I don't know what that means, sorry 3:I got it from a scenario I found online. Line 12 and 13 are meant to change the world. Line 12 removes all objects in the world and Line 13 sets another.
danpost danpost

2013/12/20

#
(1) ok, so this world is not the "welt" world (2) where is your "welt" world (3) you could say it is like cleaning your trash before throwing it out
S_Felix S_Felix

2013/12/20

#
1: right 2: it is a subclass of the World class 3: would it work without that line?
danpost danpost

2013/12/20

#
(1) fine (2) I was asking about your "welt" world object, not about the class; also, how is the 'img' field declared (show the line) (3) absolutely
S_Felix S_Felix

2013/12/20

#
Because of all that information needed I uploaded it to Dropbox: https://www.dropbox.com/sh/cml06kvp4lg1cxz/l8KRE3Bj6B There is a INFO object in this project that explains better what I want to do.
danpost danpost

2013/12/20

#
It is going to take a little work (because of the world changing) to get the required value to the 'welt' world (when it finally does get created). In the Game world class, add an instance int field and set it to one (the default value); call it 'img'. Then, add the following method (constructor) to the class:
public Game(int img)
{
    this();
    this.img = img;
}
Also, add this method to the Game class to make the value of the field accessible to the actors:
public int getImg()
{
    return img;
}
Now, use 'new Game(1)' in the S1 class act method and 'new Game(2)' in the S2 class act method for the worlds to go to. Next, change your current welt class constructor identifier to accept the value of the int field -- 'public welt(int img)'. After the 'super' call in the constructor, add the line 'this.img = img;'. Finally, we can now pass the value from the Play class using 'new welt(((Game)getWorld()).getImg())'.
S_Felix S_Felix

2013/12/20

#
I get 2 errors so far: 1: if I put the "this" command in the Game constructor first, it says the "super" command should be first. if I put the "super" command in the Game constructor first, it says the "this" command should be first. 2: if I just delete the "this();" command there is an error saying that it was not possible to instantiate a new world because it does not have a constructor that doesn't take a parameter.
danpost danpost

2013/12/20

#
danpost wrote...
Then, add the following method (constructor) to the class:
public Game(int img)
{
    this();
    this.img = img;
}
The key word (that was bold and italicized) was 'add' (not 'replace'). That means that in addition to the one you already have (had) in the class, to add this, a second, one.
danpost danpost

2013/12/20

#
The line 'this();' actually calls the constructor 'public Game()' to execute (which should, in your case, start with 'super').
S_Felix S_Felix

2013/12/20

#
Oh, thanks, but the error of the 2nd case still appears
danpost danpost

2013/12/20

#
S_Felix wrote...
2: if I just delete the "this();" command there is an error saying that it was not possible to instantiate a new world because it does not have a constructor that doesn't take a parameter.
Do not delete the 'this();' command. Anyway, if you have both constructors (like you should), that error should not be occurring. If it is, post both constructors here.
S_Felix S_Felix

2013/12/20

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

public class Game extends World
{
    public int img=1;
    
    public Game()
    {             
        super(1000, 800, 1);       
        //buttons
        addObject(new Play(),100,50);
        addObject(new Ships(),275,50);
    }
    public Game(int img)  
    {  
        this();  
        this.img = img;  
    } 
    public int getImg()  
    {  
        return img;  
    }
}
this is the whole Game.class
There are more replies on the next page.
1
2