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

2015/1/11

get counter from several levels to one next level

Steven.Z. Steven.Z.

2015/1/11

#
i decided to create one new last fourth level for my game the third level are actually 4 third levels (Level 3A, 3B, 3C & 3D) so you could always choose a certain path to get to one level and you could always get your score from the previous level to the next one as i said will be the last level just one level (4); no matter if you were in 3A or 3C, you always get to level 4 so i have this code but it doesn't work to get the score to level 4 from any of these level 3
if(Greenfoot.isKeyDown("space"))
            {
               Counter counter = ((level3A) getWorld()).getCounter();
               Greenfoot.setWorld(new Level4(counter));
            }
this codes works if im getting from 3A to 4 but i want the code to change, that it works from all 3s unfortunately i don't have the Level 3 Worlds not in a subclass and it doesn't work how can i change just this code, so that it works the way i want? thanks
danpost danpost

2015/1/11

#
Replace line 3 with this:
Counter counter = null;
if (getWorld() instanceof level3A)  counter = ((level3A) getWorld()).getCounter();
else counter = ((level3C) getWorld()).getCounter()
Steven.Z. Steven.Z.

2015/1/11

#
thanks, it works, but only for level 3a and 3c i tried to copy it for 3b and 3c or just add 3b and 3c but it didn't work
danpost danpost

2015/1/11

#
It would expand to this for all 4:
Counter counter = null;
if (getWorld() instanceof level3A)  counter = ((level3A) getWorld()).getCounter();
else if (getWorld() instanceof level3B)  counter = ((level3B) getWorld()).getCounter();
else if (getWorld() instanceof level3C)  counter = ((level3C) getWorld()).getCounter();
else if (getWorld() instanceof level3D)  counter = ((level3D) getWorld()).getCounter();
Steven.Z. Steven.Z.

2015/1/11

#
thanks
You need to login to post a reply.