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

2016/2/16

Keep updating a counter while transferring worlds?

Shurp Shurp

2016/2/16

#
Hello! I just started coding recently and am working on a trivia sort of game where you control the player and move it towards objects that then trigger a world change. The new world has the question on it and when the correct answer is clicked, it adds a point to the counter and returns the player to the overworld. However, I've run into a problem where I am creating two separate counters in the two different worlds. How would I transfer this counter objects value from world to world? I'm assuming I have to reference the counter in both of the worlds, but I am having difficulty wrapping my head around how I can go about doing that. If someone could point me in the right direction it would be greatly appreciated!
danpost danpost

2016/2/16

#
You will need to show the code you are using for the world transferring -- particularly from the game world to the question world.
Shurp Shurp

2016/2/16

#
Overworld/Main game world hub
public class Overworld extends World
{
    public Overworld()
    {    
        super(1200, 800, 1); 
        construct();
    }
    public void construct()
    {  
       Counter counter = new Counter();
       addObject(counter, 600, 40);
        
       Player player = new Player();
       addObject(player, 600,750);
       
       Warpto1 warpto1 = new Warpto1();
       addObject(thumbius, 1025, 175);
    }
}
Question world:
public class Question1 extends World
{
    public Question1()
    {    
        super(1200, 800, 1);     
        construct();
    }
    public void construct()
    {
        Counter counter = new Counter();
        addObject(counter, 600, 40);
        
        Answer answer = new Answer(counter);
        addObject(lucius, 300, 350);
    }
}
And here is the correct answer's class
{
    private Counter counter;
    public void act() 
    {
        whenClicked();
    }
    public Answer(Counter pointCounter)
    {
        counter = pointCounter;
    }
    public void whenClicked()
    {
       if(Greenfoot.mouseClicked(this))
        {
            counter.add(1);
            Greenfoot.setWorld(new Overworld());
        } 
    }
}
Basically, I am aware that I'm making new world's/counters when I don't want to, however I am not sure how to keep the same world or counter and simply transfer between worlds. Thanks for the help, I appreciate it!
danpost danpost

2016/2/16

#
Okay -- two of these three set of code should not compile. You should be getting 'cannot find symbol variable thumbius' in your OverWorld class and you should be getting 'cannot find symbol variable lucius' in your Question1 class. You cannot use variable names in your classes that are not previously declared. As far as the counters/worlds are concerned -- I will presume that your initial active World is of type OverWorld, where you player is located. This is the world that your ONE counter should be in. Also, you should not programmatically create another instance of this type world (like you are doing at line 16 of the Answer class). You can pass and keep references to the OverWorld class in your other worlds so you can access its counter (or, at least, the value off the counter) and then return to the ONE OverWorld class (instead of going to a NEW one). You may want to review the java tutorial pages on Passing information to methods or constructors and Declaring member variables.
You need to login to post a reply.