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

2012/5/23

helping with a bug in my code

martijn13039 martijn13039

2012/5/23

#
hey all, I have a bug in my code and really need help. In the code that i´ve post here you can see if I eat a student that the student wil come back after a few seconds, but there is the problem if I eat a second student befor the fist student is back there wil only come one back and not two. I hope you guys understand what I mean and I hope you could help me to fix this problem/bug
privtate int studentDelayCount;

 public concierge(Counter pointCounter)
    {
        counter = pointCounter;
        studentDelayCount = 0;

    }

 public void act()

      {

        checkNewstudent();
       }

 if(canSee(student.class))
        {
            eat(student.class);
                    newstudent();
        }

 private void newstudent()
        {
            studentDelayCount = 250;
        }
    
    private void checkNewstudent()
    {
        if (studentDelayCount > 0) {
            studentDelayCount--;
         if (studentDelayCount == 0) {
             createNewstudent();
            }
        
davmac davmac

2012/5/23

#
Please correct the indentation! You can use ctrl+shift+I in the editor. Also, this can't be all the code; there's no method called createNewstudent, yet you're calling it.
martijn13039 martijn13039

2012/5/23

#
oow srry, I thaw maby guys wil be irrated if I posted an to big code, (but here is the whole code I use on that actor so I don't forget something
import greenfoot.*; 

public class concierge extends school
{
    

    private Counter counter;
    private int studentDelayCount;
   
    
    public concierge(Counter pointCounter)
    {
        counter = pointCounter;
        studentDelayCount = 0;

    }

    public void act()

    
    {

        processKeys();
        checkNewstudent();

        

        if(canSee(afval.class))
        {
            eat(afval.class);
            counter.add(8);
        }


        if(canSee(afval2.class))
        {
            eat(afval2.class);
            counter.add(4);
        }

       

        if(canSee(student.class))
        {
            eat(student.class);
            counter.add(20);
            newstudent();
        }
        
        

        
        if (canSee(student2.class))
        {
            eat(student2.class);
            counter.add(-25);
            createNewstudent2();
        }

    }
    public void processKeys()
    {

        if(Greenfoot.isKeyDown ("left")) {
            setLocation(getX() - 2, getY());
           setImage("concierge klein.png");
         
        } 
        else {
            setLocation(getX(),getY());
        }

        if(Greenfoot.isKeyDown ("down")) {
            setLocation(getX(),getY() + 2);
        }
        else {
            setLocation(getX(),getY());
        }

        if(Greenfoot.isKeyDown ("right")) {
            setLocation(getX() + 2,getY());
             setImage("concierge rechts.png");
        }
        else {
            setLocation(getX(),getY());
        }

        if(Greenfoot.isKeyDown ("up")) {
            setLocation(getX(),getY() - 2);
        }
        else {
            setLocation(getX(),getY());
        }
    }
    
     private void newstudent()
        {
            studentDelayCount = 250;
        }
    
    private void checkNewstudent()
    {
        if (studentDelayCount > 0) {
            studentDelayCount--;
         if (studentDelayCount == 0) {
             createNewstudent();
            }
        }
    }
    
   

    private void createNewstudent()
    {
        

        student newstudent;

        newstudent = new student();

        World world;
        world = getWorld();
        
        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);

        world.addObject(newstudent, x, y);
    }

    private void createNewstudent2()
    {
        

        student2 newstudent2;

        newstudent2 = new student2();

        World world;
        world = getWorld();
        
        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);

        world.addObject(newstudent2, x, y);

       
    }
}
danpost danpost

2012/5/23

#
The first thing that strikes me is that the only key that may function property is the 'up' key. Think about what 'processKeys' is doing! Nothing else really caught my attention as being a problem; though, I did see a way to improve the look (and readability) of the code.
davmac davmac

2012/5/23

#
danpost, I think all the keys would function properly, even though the code looks weird; e.g. if you press "left", it sets the location as slightly to the left of the current location, and then the later "setLocation(getX(),getY());" calls have no effect. martijn13039: With this code (and the other similar code):
        if(Greenfoot.isKeyDown ("down")) {  
            setLocation(getX(),getY() + 2);  
        }  
        else {  
            setLocation(getX(),getY());  
        }  
... the 'else' clause is pointless. Just write:
        if(Greenfoot.isKeyDown ("down")) {  
            setLocation(getX(),getY() + 2);  
        }  
As to your problem with eating multiple students, the reason is that you only have one 'studentDelayCount' variable; if you eat two students, you add to it each time, but it still will only add one student back once it reaches 0. An easy solution would be to just make students go invisible (setImage(null)) when they are 'eaten' and have the counter in the student, rather than the concierge class; when the counter reaches 0, the student should become visible again. Some general comments: you still have some bad indentation. It makes your code so much harder to read. That means more effort for us, and less help for you! You could also collapse multiple blank lines into one blank line. ctrl+shift+I does this all for you. Please don't make it harder for us to help you. Use ctrl+shift+I to fix your indentation! Also, class names should start with a capital. "concierge" should be "Concierge", "student" should be "Student", etc. It's just a convention, but it's confusing for experienced Java programmers who try to read your code.
martijn13039 martijn13039

2012/5/24

#
davmac srry for the confusion code, but wil try to make it better You say make (setImage(null)) but need I make that by if see student class of an other class This is the code how I think you mean it,
if(canSee(student.class))       
 {  
           (setImage(null))
            counter.add(20);  
           newstudent();  
            
         }  
private void newstudent()        {     
       studentDelayCount = 250;   
    }          private void checkNewstudent()    
  {      
    
if (studentDelayCount > 0) {       
     studentDelayCount--;         
  if (studentDelayCount == 0) {          
   (setImage(student))          }        
}  
Have I done it correct now, or have I make mistake
davmac davmac

2012/5/24

#
Sorry, but I'm not even going to read that code until you correct the indentation.
martijn13039 martijn13039

2012/5/24

#
i hope this is what you mean because the whole time there stay in intedion was already correct if it isn´t fine i really have no idea what you mean
[code]import greenfoot.*; 

public class concierge extends school
{

    private Counter counter;
    private int studentDelayCount;

    public concierge(Counter pointCounter)
    {
        counter = pointCounter;
        studentDelayCount = 0;

    }

    public void act()

    {
        processKeys();
        checkNewstudent();

        if(canSee(afval.class))
        {
            eat(afval.class);
            counter.add(8);
        }

        if(canSee(afval2.class))
        {
            eat(afval2.class);
            counter.add(4);
        }

        if(canSee(student.class))
        {
            eat(student.class);
            counter.add(20);
            newstudent();
        }

        if (canSee(student2.class))
        {
            eat(student2.class);
            counter.add(-25);
            createNewstudent2();
        }

    }

    public void processKeys()
    {

        if(Greenfoot.isKeyDown ("left")) {
            setLocation(getX() - 2, getY());
            setImage("concierge klein.png");

               }

        if(Greenfoot.isKeyDown ("down")) {
            setLocation(getX(),getY() + 2);
              }

        if(Greenfoot.isKeyDown ("right")) {
            setLocation(getX() + 2,getY());
            setImage("concierge rechts.png");
        
              }

        if(Greenfoot.isKeyDown ("up")) {
            setLocation(getX(),getY() - 2);
        }
                
    }

    private void newstudent()
    {
        studentDelayCount = 250;
    }

    private void checkNewstudent()
    {
        if (studentDelayCount > 0) {
            studentDelayCount--;
            if (studentDelayCount == 0) {
                createNewstudent();
            }
        }
    }

    private void createNewstudent()
    {

        student newstudent;
        newstudent = new student();
        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);

        world.addObject(newstudent, x, y);
    }

    private void createNewstudent2()
    {

        student2 newstudent2;
        newstudent2 = new student2();
        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);

        world.addObject(newstudent2, x, y);

    }
}
martijn13039 martijn13039

2012/5/24

#
Is it oké Now?
davmac davmac

2012/5/24

#
No. For example on line 57 the closing brace '}' doesn't line up correctly. You could fix this just by pressing Ctrl+shift+I, which I've already told you.
martijn13039 martijn13039

2012/5/24

#
is it now oke i have do almost evry line ctrl+shift+I, and almost the whole time there stay it is already good
[code]import greenfoot.*; 

public class concierge extends school
{

    private Counter counter;
    private int studentDelayCount;

    public concierge(Counter pointCounter)
    {
        counter = pointCounter;
        studentDelayCount = 0;

    }

    public void act()

    {
        processKeys();
        checkNewstudent();

        if(canSee(afval.class))
        {
            eat(afval.class);
            counter.add(8);
        }

        if(canSee(afval2.class))
        {
            eat(afval2.class);
            counter.add(4);
        }

        if(canSee(student.class))
        {
            eat(student.class);
            counter.add(20);
            newstudent();
        }

        if (canSee(student2.class))
        {
            eat(student2.class);
            counter.add(-25);
            createNewstudent2();
        }

    }

    public void processKeys()
    {

        if(Greenfoot.isKeyDown ("left")) {
            setLocation(getX() - 2, getY());
            setImage("concierge klein.png");

        }

        if(Greenfoot.isKeyDown ("down")) {
            setLocation(getX(),getY() + 2);
        }

        if(Greenfoot.isKeyDown ("right")) {
            setLocation(getX() + 2,getY());
            setImage("concierge rechts.png");

        }

        if(Greenfoot.isKeyDown ("up")) {
            setLocation(getX(),getY() - 2);
        }

    }

    private void newstudent()
    {
        studentDelayCount = 250;
    }

    private void checkNewstudent()
    {
        if (studentDelayCount > 0) {
            studentDelayCount--;
            if (studentDelayCount == 0) {
                createNewstudent();
            }
        }
    }

    private void createNewstudent()
    {

        student newstudent;
        newstudent = new student();
        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);

        world.addObject(newstudent, x, y);
    }

    private void createNewstudent2()
    {

        student2 newstudent2;
        newstudent2 = new student2();
        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);

        world.addObject(newstudent2, x, y);
    }
}
davmac davmac

2012/5/24

#
Ok, your indentation is correct now. But, you still have the same problem as per my original suggestion:
As to your problem with eating multiple students, the reason is that you only have one 'studentDelayCount' variable; if you eat two students, you add to it each time, but it still will only add one student back once it reaches 0. An easy solution would be to just make students go invisible (setImage(null)) when they are 'eaten' and have the counter in the student, rather than the concierge class; when the counter reaches 0, the student should become visible again.
ttamasu ttamasu

2012/5/24

#
If you don't want to do the way davmac stated, you can always make it one chain of else if s so that only one object gets eaten per cycle (ie insert "else" at the beginning of lines,28,34,and 41)
davmac davmac

2012/5/25

#
ttamasu, I think the problem is not multiple objects getting eaten per cycle, but rather multiple objects getting eaten over multiple cycles but before the first one respawns. There's no way to fix this without adding a collection to the concierge class or moving part of the logic to the student class.
You need to login to post a reply.