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

2013/5/26

Everytime it changes the world, but why?!

1
2
3
JesusJesus JesusJesus

2013/5/26

#
Hey, i made an Ammo Limit in my Zombiegame. Now i want to Show a new class on the map with the Image "RELOAD". When the ammo is == 0 it Shows this class, but directly it changes also the world to the gameover world?! But why?! In the same Actorclass i made a addObject shot, and it works with the same code as the Image, but there it doesn´t changes the world.
if (Greenfoot.isKeyDown("r") && (firecounter == 0) && (ammocounter > 0))
        {
             Shoot2 rObj = new Shoot2();
             getWorld().addObject( rObj, getX(), getY() );
             rObj.setRotation(getRotation());
             firecounter = 5;
             ammocounter--;
        }
        
        if(ammocounter < 1)
        {
            Reload2 reload2 = new Reload2();
            getWorld().addObject(reload2, 700, 80);
        }
can you help me??? Thank you
danpost danpost

2013/5/26

#
I do not know if this will fix your problem; but it appears that the second 'if' block should be moved up inside the first one (move line 8 to line 15). You only need to check for zero when decrementing the value.
JesusJesus JesusJesus

2013/5/26

#
Okay i will test it now, but in the first one there is the Statement to follow it only if the ammocounter is bigger then 0 and in the second it is smaller then 1 can it work then when it is in the first if?! But okay wait a min i will test it :)
JesusJesus JesusJesus

2013/5/26

#
it is the same Problem... but thanks... maybe i post the code in another class which changes the world:
Actor one;
      one = getOneObjectAtOffset(0,0, One.class);
      if (one != null)
      {
            World world;
            world = getWorld();
            world.removeObject(one);
            Greenfoot.setWorld(new Over());
      }    
danpost danpost

2013/5/26

#
JesusJesus wrote...
Okay i will test it now, but in the first one there is the Statement to follow it only if the ammocounter is bigger then 0 and in the second it is smaller then 1 can it work then when it is in the first if?! But okay wait a min i will test it :)
Even though you may still have the same problem, yes. It should work when it is in the first 'if'. Again, if the value was 1 going into the first 'if, and it is then decremented to zero, the second 'if' should catch it. The problem does not appear to be in the latest code post (the one changing worlds). What is a One object? give its states and behaviours.
JesusJesus JesusJesus

2013/5/26

#
Okay: The One class is only the first Player (is has a coop mod) and therefore every Player has its own reload and shot class (Reload1, Shot1||Reload2, Shot2) any other Problems in understanding the classes? (And thank you that your trying to help me ;) )
danpost danpost

2013/5/26

#
What class is the world-changing code you posted in? again.be specific.
JesusJesus JesusJesus

2013/5/26

#
this is the class of the Zombies. So if the can see the playerclass the Player class will be removed and the GameOver world will come
danpost danpost

2013/5/26

#
What other code do you have related to the Reload2 class?
JesusJesus JesusJesus

2013/5/26

#
None. that in the Player Two class is the only one. The Class Reload2 has None codes, because it only has to Show the Picture RELOAD
danpost danpost

2013/5/26

#
How are your classes arranged (which are subclasses of which, including those subclassing Actor and World)?
JesusJesus JesusJesus

2013/5/26

#
I think it is easier for you to see all codes and classes. Here you can open it in Greenfoot: Zombie Invasion Demo 1.6
JesusJesus JesusJesus

2013/5/26

#
Okay thank you but i have to go now, if you write sth i can answer you tomorrow.
danpost danpost

2013/5/26

#
I thought so. That does explain the behavior you are getting. Because your Shoot1 and Reload1 classes extend One and your Shoot2 and Reload2 classes extend Two, any object created from those classes are still considered to be instances of One or Two. When you add a Reload2 object to the world, you are also adding a One object to the world. If a zombie hits a Reload# object, it is hitting a One object and will cause change to game over world. Change what the Shoot# and Reload# extend back to Actor and copy/paste the 'canSee' and 'eat' methods into the 'Shoot#' classes
JesusJesus JesusJesus

2013/5/27

#
Okay thank you i will try it after school. But it sounds very good :)
There are more replies on the next page.
1
2
3