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

2020/11/29

Creating a game but counter keeps breaking

OpticalFlow OpticalFlow

2020/11/29

#
I'm making a small game in Greenfoot to see what I can do again as I haven't done coding in a while due to isolation, I got a system which lets me travel from one world to another (thank you danpost) and I implemented it into my code, whenever you pick up an apple, a counter in the corner is increased by 1, but when the counter went up it fails because it tries to send it to both worlds. Is there any way of fixing this? Code for Apple: Actor Apple; Apple = getOneObjectAtOffset(0,0,Apple.class); if (Apple!=null) { Indoors indoors = (Indoors)getWorld(); indoors.removeObject(Apple); Counter1 counter = indoors.getCounter1(); counter.bumpCount(1); } if (Apple!=null) { Outside outside = (Outside)getWorld(); outside.removeObject(Apple); Counter2 counter = outside.getCounter2(); counter.bumpCount(1); } Code for Counter 1 and 2 is the same just 1 and 2 is swapped:] public class Counter1 extends Actor { private int totalCount = 0; public Counter1() { setImage(new GreenfootImage("0",20,Color.WHITE,Color.BLACK)); } public void bumpCount(int amount) { totalCount += amount; setImage(new GreenfootImage(""+ totalCount,20,Color.WHITE,Color.BLACK)); } }
danpost danpost

2020/11/29

#
OpticalFlow wrote...
I implemented it into my code, whenever you pick up an apple, a counter in the corner is increased by 1, but when the counter went up it fails because it tries to send it to both worlds. Is there any way of fixing this?
"getWorld()" will always fail in one of the two if blocks. If actor is in Indoors world, you will not be able to assign it to outside; and, if in Outside world, you will not be able to assign it to indoors. You must verify which sub-type of World the actor is in before assigning it to a variable that holds a specific sub-type of World. For example, the first if condition should be:
if (Apple != null && (getWorld() instanceof Indoors))
The other if condition will be done similarly.
OpticalFlow OpticalFlow

2020/11/29

#
Right I got that but now how do I prevent the worlds from changing when I go in between them? This is the code for going in between worlds and I made special actors shaped like arrows which you walk into. if(isTouching(Exit_Sign_1.class)) { Greenfoot.setWorld(new Outside()); } if(isTouching(Enter_Sign_1.class)) { Greenfoot.setWorld(new Indoors()); } Is there any way I can travel in between them without losing the counters data from that world and the actors which I have removed?
danpost danpost

2020/11/29

#
OpticalFlow wrote...
Is there any way I can travel in between them without losing the counters data from that world and the actors which I have removed?
Apply the same extra conditions to these ifs.
OpticalFlow OpticalFlow

2020/11/30

#
How do I do that? Can you put an example please?
OpticalFlow OpticalFlow

2020/11/30

#
I changed the ifs so they were the same and their hitbox appears to be smaller. But the worlds do not save when I go in between them does this have something to do with Greenfoot.setWorld(new Outside());?
danpost danpost

2020/11/30

#
OpticalFlow wrote...
I changed the ifs so they were the same and their hitbox appears to be smaller. But the worlds do not save when I go in between them does this have something to do with Greenfoot.setWorld(new Outside());?
Correct. You will need to keep a valid reference to any world that you wish to return to if you want it to be exactly as you left it. You could adjust the score in the world proceeding to when changing worlds. Sidenote: I would not have thought the hit box size would be changed.
You need to login to post a reply.