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

2014/1/19

Determine right or wrong

1
2
3
4
Blhaar2014 Blhaar2014

2014/1/19

#
I'm having some trouble with my game. The plan is to decide if certain pictures of people and their ID cards are matched, or not. The game has an 'allow' and a 'deny' button to use for this decision. The problem I ran into was rather strange; The game has to decide if the decission the user made was correct or not. The user has 3 'lives'/'hearts' which go down by 1 each time the user makes a mistake. But instead of deciding if the shown picture and ID card were a match, the game seems to decide if the next two are a match or not. And that kinda messes up the entire "life-system" ... Here's the code that generates the combinations and the code that decides if it's right or wrong;
if(Greenfoot.mouseClicked(this))
         {
            getWorld().removeObjects(getWorld().getObjects(Dirk.class));
            getWorld().removeObjects(getWorld().getObjects(Fleur.class));
            getWorld().removeObjects(getWorld().getObjects(Coen.class));
            getWorld().removeObjects(getWorld().getObjects(Remco.class));

            int RandomNum = Greenfoot.getRandomNumber(4);
            int RandomNum2 = Greenfoot.getRandomNumber(50);
            if (RandomNum == 0){
                getWorld().addObject (new Dirk(),120,140);
            }    else if (RandomNum == 1) {
                getWorld().addObject (new Fleur(),120,140);
            }   else if (RandomNum == 2) {
                getWorld().addObject (new Coen(),120,140);
            }    else if (RandomNum == 3) {
                getWorld().addObject (new Remco(),120,140);
            }    
..
if (RandomNum == 0){
                if (RandomNum2 <= 30){
                    getWorld().addObject (new IdcardDirk(),120,620);
                }    else  if (RandomNum2 >= 30) {
                    getWorld().addObject (new IdcardFleur(),120,620);
                }   else if (RandomNum2 >= 37) {
                    getWorld().addObject (new IdcardCoen(),120,620);
                }    else if (RandomNum2 >= 44) {
                    getWorld().addObject (new IdcardRemco(),120,620);
                }   
            }   else if (RandomNum == 1){
                if (RandomNum2 >= 30){
                        getWorld().addObject (new IdcardDirk(),120,620);
                    }    else  if (RandomNum2 <= 30) {
                        getWorld().addObject (new IdcardFleur(),120,620);
                    }   else if (RandomNum2 >= 37) {
                        getWorld().addObject (new IdcardCoen(),120,620);
                    }    else if (RandomNum2 >= 44) {
                        getWorld().addObject (new IdcardRemco(),120,620);
                    }   
            }    else if (RandomNum == 2){
                if (RandomNum2 >= 44){
                        getWorld().addObject (new IdcardDirk(),120,620);
                    }    else  if (RandomNum2 >= 37) {
                        getWorld().addObject (new IdcardFleur(),120,620);
                    }   else if (RandomNum2 <= 30) {
                        getWorld().addObject (new IdcardCoen(),120,620);
                    }    else if (RandomNum2 >= 30) {
                        getWorld().addObject (new IdcardRemco(),120,620);
                    }    
            }    else if (RandomNum == 3){
                if (RandomNum2 >= 37){
                        getWorld().addObject (new IdcardDirk(),120,620);
                    }    else  if (RandomNum2 >= 44) {
                        getWorld().addObject (new IdcardFleur(),120,620);
                    }   else if (RandomNum2 >= 30) {
                        getWorld().addObject (new IdcardCoen(),120,620);
                    }    else if (RandomNum2 <= 30) {
                        getWorld().addObject (new IdcardRemco(),120,620);
                    }     


...
...
...

if(isGeraakt1())    
           {    
              removeHealth();        
           }    
        
         if(isGeraakt2())    
          {    
              removeHealth();        
          }    
        
         if(isGeraakt3())    
           {    
               removeHealth();        
           }    
        
         if(isGeraakt4())    
          {    
             removeHealth();        
          }    

 public boolean isGeraakt1()
               {
                 boolean raak = false;
                  int RandomNum = Greenfoot.getRandomNumber(4);
                  int RandomNum2 = Greenfoot.getRandomNumber(50);
                 if ( RandomNum == 0 && RandomNum2 <= 30 && Greenfoot.mouseClicked(this))
                 {
                  raak = true;
                 }
                 return raak;
               }
               
      public boolean isGeraakt2()
               {
                 boolean raak = false;
                  int RandomNum = Greenfoot.getRandomNumber(4);
                  int RandomNum2 = Greenfoot.getRandomNumber(50);
                 if ( RandomNum == 1 && RandomNum2 <= 30 && Greenfoot.mouseClicked(this))
                 {
                  raak = true;
                 }
                 return raak;
               }         
               
               
       public boolean isGeraakt3()
               {
                 boolean raak = false;
                  int RandomNum = Greenfoot.getRandomNumber(4);
                  int RandomNum2 = Greenfoot.getRandomNumber(50);
                 if ( RandomNum == 2 && RandomNum2 <= 30 && Greenfoot.mouseClicked(this))
                 {
                  raak = true;
                 }
                 return raak;
              }
            
    public boolean isGeraakt4()
       {
                boolean raak = false;
                 int RandomNum = Greenfoot.getRandomNumber(4);
                 int RandomNum2 = Greenfoot.getRandomNumber(50);
                if ( RandomNum == 3 && RandomNum2 <= 30 && Greenfoot.mouseClicked(this))
                {
                    raak = true;
                }
                    return raak;
               }
               
      
         
        
               
      public void removeHealth()
      {
       Controlecentrum subWorld = (Controlecentrum) getWorld();   
       Heart HeartObject = (Heart) subWorld.getObjects(Heart.class).get(0);   
       HeartObject.adjustValue(-1);    
           
       if (HeartObject.getValue() == 0) removeLive();  
         
       }
       
       public void removeLive()    
        {    
          int lives = getWorld().getObjects(Heart.class).size();    
          getWorld().removeObject((Actor)getWorld().getObjects(Heart.class).get(lives-1));  
    
           if (lives != 1)  
           {  
                //
           }  
          else  
           {     
                Greenfoot.setWorld(new GameOverScreen());
           }  
Anyone who thinks he/she has the answer, thanks in advance!
danpost danpost

2014/1/20

#
I think the problem is that you are (1) not saving the initial random numbers chosen and (2) creating new random numbers to use in the checks in the 'isGeraakt#' methods.
Blhaar2014 Blhaar2014

2014/1/20

#
danpost wrote...
I think the problem is that you are (1) not saving the initial random numbers chosen and (2) creating new random numbers to use in the checks in the 'isGeraakt#' methods.
How do you suggest I should take the outcome of those two integers and implement them into the booleans below? Since I'm a bit in over my head right now... Thanks for the help, appreciate it!
danpost danpost

2014/1/20

#
First, make the two int fields, RandomNum and RandomNum2, instance fields by declaring those fields outside the method:
int RandomNum, RandomNum2;

public void act()
{
...
Next, remove the 'int' from in front of the lines where you set the value of the fields (lines 8 and 9); finally, remove all the lines that set new values to those fields within all the 'isGeraakt#' methods. Then report back as the any problems at that point (there most assuredly will be some); but, at least the fields are better (for now).
Blhaar2014 Blhaar2014

2014/1/20

#
danpost wrote...
First, make the two int fields, RandomNum and RandomNum2, instance fields by declaring those fields outside the method:
int RandomNum, RandomNum2;

public void act()
{
...
Next, remove the 'int' from in front of the lines where you set the value of the fields (lines 8 and 9); finally, remove all the lines that set new values to those fields within all the 'isGeraakt#' methods. Then report back as the any problems at that point (there most assuredly will be some); but, at least the fields are better (for now).
Thank you, I will see what happens and look into any new problems that may occur!
Blhaar2014 Blhaar2014

2014/1/20

#
danpost wrote...
First, make the two int fields, RandomNum and RandomNum2, instance fields by declaring those fields outside the method:
int RandomNum, RandomNum2;

public void act()
{
...
Next, remove the 'int' from in front of the lines where you set the value of the fields (lines 8 and 9); finally, remove all the lines that set new values to those fields within all the 'isGeraakt#' methods. Then report back as the any problems at that point (there most assuredly will be some); but, at least the fields are better (for now).
Okay, so now the code works, I guess, but now no hearts dissapear, in any case. Since I have an allow and deny button, I've used the code in ways where the int's are and are NOT a certain value. Now there aren't any errors that Greenfoot stumbles upon, but the code simply doesn't delete hearts where it should. Any idea's ? or any code you'd like to see as example for now?
danpost danpost

2014/1/20

#
This does in no way help you in your current dilemma, but it seems that all the code from line 66 through to 133 can be reduced to simply:
if (RandomNum2 <= 30 && Greenfoot.mouseClicked(this)) removeHealth();
I say this because the only difference between each 'isGeraakt#' method is the check on the RandomNum field; but you are checking all four possible values by using ' || ' and or'ing the possibles. The value of RandomNum will be either '0 or 1 or 2 or 3'; so, therefore, that part will be true regardless. Although this may not fix the problem, it may shed some light as to why the hearts are not being removed. It would appear (although not stated) that the bottom code is from the 'Deny' button class (removing health because the picture and the idcard are matching and therefore 'Allow' should be the correct button. I think we will have to look at the 'Heart' class code to possibly find the answer.
Blhaar2014 Blhaar2014

2014/1/20

#
danpost wrote...
This does in no way help you in your current dilemma, but it seems that all the code from line 66 through to 133 can be reduced to simply:
if (RandomNum2 <= 30 && Greenfoot.mouseClicked(this)) removeHealth();
I say this because the only difference between each 'isGeraakt#' method is the check on the RandomNum field; but you are checking all four possible values by using ' || ' and or'ing the possibles. The value of RandomNum will be either '0 or 1 or 2 or 3'; so, therefore, that part will be true regardless. Although this may not fix the problem, it may shed some light as to why the hearts are not being removed. It would appear (although not stated) that the bottom code is from the 'Deny' button class (removing health because the picture and the idcard are matching and therefore 'Allow' should be the correct button. I think we will have to look at the 'Heart' class code to possibly find the answer.
Wow, it does indeed clean the slate a little! But now the same problem has shown itself again, it only deletes a heart if the NEXT combination is "right" (considering I'm pressing the 'deny' button now) If it is any help at all, here's the code that's written within the "Heart" class;
public class Heart extends Actor
{
 
          public static final boolean VERTICAL = true;  
          public static final boolean HORIZONTAL = false;  
          private static final int GAP = 10;  
          private boolean orientation = HORIZONTAL;  
          private GreenfootImage icon;  
          private int count;
          private boolean direction;
          private int initValue;  
          private int value;
          
        
          
          public Heart()  
          {  
            icon = new GreenfootImage("herz.png");  
            count = 3;
            direction = false;
            value = initValue = count;  
            orientation = direction;  
            updateImage();
            
            
          } 
          
          private void updateImage()  
          {  
            int across = value;  
            int down = 1;  
            int xGap = GAP;  
            int yGap = 0;  
            if (orientation == VERTICAL)  
            {  
              across = 1;  
              down = value;  
              xGap = 0;  
              yGap = GAP;  
            }  
            int wide = icon.getWidth()*across+xGap*(across+1);  
            int high = icon.getHeight()*down+yGap*(down+1);  
            GreenfootImage base = new GreenfootImage(wide, high);  
            for (int j=0; j<down; j++) for (int i=0; i<across; i++)  
            {  
              int xOffset =xGap+i*(xGap+icon.getWidth());  
              int yOffset = yGap+j*(yGap+icon.getHeight());  
              base.drawImage(icon, xOffset, yOffset);  
            }  
            setImage(base);  
          }  
          
          public void adjustValue(int change)  
          {  
            if (value+change > initValue || value + change < 0) return;  
            value += change;  
            updateImage();  
          }  
          
          public int getValue()  
          {  
            return value;  
          }  

        }
danpost danpost

2014/1/20

#
Where did you get that Heart class (it looks like one of mine)?
Blhaar2014 Blhaar2014

2014/1/20

#
It sure is, haha! Good eye! A friend of mine is in the same class as me, and I've been following this forum myself for a while too! We're in the same group, but working on different games. The "life" system was kinda the same, though, until I encountered the buttons it had to be linked to ....
danpost danpost

2014/1/20

#
It has been some time since I wrote that Heart class; but the style and wording were big clues (and the added orientation control). Anyways, knowing how the hearts work will aid in determining how to fix your problem. My problem is this: you are referring to health and lives; and you have methods for 'removeHealth' and 'removeLive'. How is each represented in your world (what type of actor shows what value).
Blhaar2014 Blhaar2014

2014/1/20

#
I've removed everything that wasn't needed or referred to in my game itsself, and checked if everything is referred to in the right way. In the world class only the heart and needed buttons are added in the scenario, where as the heart is referred to as such;
 

addHeart();

...

   private void addHeart()
    {
        Heart Heart = new Heart();
        addObject(Heart, 390, 40);    
    }

I hope I didn't make it too hard to follow just now, since even I myself have trouble with finding out where the problem may come from...
Blhaar2014 Blhaar2014

2014/1/20

#
danpost wrote...
It has been some time since I wrote that Heart class; but the style and wording were big clues (and the added orientation control). Anyways, knowing how the hearts work will aid in determining how to fix your problem. My problem is this: you are referring to health and lives; and you have methods for 'removeHealth' and 'removeLive'. How is each represented in your world (what type of actor shows what value).
I've run the scenario a few times, and just mashed the buttons a bit to see if any errors occured, and sure enough there did. After a while a certain fault within the "allow" button class popped up, where it said there was an IndexOutOfBoundsExeption. This line was highlighted when I pressed the fault.
 public void removeHealth()
      {
       Controlecentrum subWorld = (Controlecentrum) getWorld();   
       Heart HeartObject = (Heart) subWorld.getObjects(Heart.class).get(0);   // This Line !!
       HeartObject.adjustValue(-1);    
           
       if (HeartObject.getValue() == 0) removeLive();  
         
       }

Any idea what it means, and if it has anything to do with the malfunctioning buttons?
danpost danpost

2014/1/20

#
The only index on that line that could cause that exception in the 'get(int)' call. It is telling you that there are no Heart objects in your Controlecentrum world. I would try commenting out the last line in your 'removeHealth' method and running it then. See if you still get the same error or something else (if something else, post the error message and location of the error).
Blhaar2014 Blhaar2014

2014/1/20

#
danpost wrote...
The only index on that line that could cause that exception in the 'get(int)' call. It is telling you that there are no Heart objects in your Controlecentrum world. I would try commenting out the last line in your 'removeHealth' method and running it then. See if you still get the same error or something else (if something else, post the error message and location of the error).
I don't get any reports so far, the only thing that I notice now is that the game is still determining if a heart should be lost or not by the combination that's next... But for some reason, if I take the // comment section away, everything seems to work now. (except for what I just mentioned ofcourse!) I forgot to use that 'shortcut' of code you suggested in the "allow" button as well... that might have been it(?) The only thing now just seems to be that problem where the wrong combination is checked. Any idea's?
There are more replies on the next page.
1
2
3
4