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

2014/6/18

Adding Text

BookwormGeekGirl BookwormGeekGirl

2014/6/18

#
I'm not sure on how to go about adding text to my bingo game. I want text to show up after a number, from 1 to 90, has been generated. For example, say 4 was generated then I would want 2+2=? to show up and I'm sure what code to use or where to add it. This is a snippet of my code, as my code goes from 1 to 90;
public void act()
      {
        if (Greenfoot.isKeyDown ("space"))
       {
          int Number = Greenfoot.getRandomNumber(3);
          setImage (new GreenfootImage ("" + Number, 100, Color.YELLOW, Color.BLACK));
          Greenfoot.delay(20);
          
       if (Number == 1)
       
       {
           if (Used_1 == 0)
           {
               
           Used_1 = 1;
           Counter counter = new Counter();
           getWorld().addObject(counter, 27, 98);

          }
       }
       
       if (Number == 2)
       
       {
           if (Used_2 == 0)
           {
               
           Used_2 = 1;
           Counter counter2 = new Counter();
           getWorld().addObject(counter2, 82, 98);
          }
       }
       
       
        if (Number == 3)
       
       {
            if (Used_3 == 0)
            {
                
           Used_3 = 1;
           Counter counter3 = new Counter();
           getWorld().addObject(counter3, 136, 99);
          }
       }
danpost danpost

2014/6/18

#
Wow, you coded that for all numbers from 1 to 90!?! There must be an easier way (and I am sure there is). Did you add the 'Greenfoot.delay' to the code to give time for the 'space' key to be released so that the code does not execute multiple times? If so, maybe you should use 'getKey' instead of 'isKeyDown'. What will be the ending arrangement of counter objects? (for example 10 across by 9 down Where is the last (# 90) of the counters placed into the world at? Looking at line 6 above, you obviously know how to create a text image and have an actor display it.
BookwormGeekGirl BookwormGeekGirl

2014/6/18

#
I was playing around and managed to find a way in which to do it, gladly I can say that it is fully functional so I can get off my laptop and back to reading as 6 of my final exams (GCSEs) are over! Whoop :D BTW, this is the working code;
 public void act()
      {
        if (Greenfoot.isKeyDown ("space"))
       {
          int Number = Greenfoot.getRandomNumber(91);
          setImage (new GreenfootImage ("" + Number, 100, Color.WHITE, Color.BLACK));
          Greenfoot.delay(20);
          
       if (Number == 1)
       
       {
           if (Used_1 == 0)
           {
               
           Used_1 = 1;
           Counter counter = new Counter();
           getWorld().addObject(counter, 27, 98);
           setImage (new GreenfootImage ("3-2=?", 50, Color.WHITE, Color.BLACK));
          }
       }
       
       if (Number == 2)
       
       {
           if (Used_2 == 0)
           {
               
           Used_2 = 1;
           Counter counter2 = new Counter();
           getWorld().addObject(counter2, 82, 98);
           setImage (new GreenfootImage ("1+1=?", 50, Color.WHITE, Color.BLACK));
          }
       }
       
       
        if (Number == 3)
       
       {
            if (Used_3 == 0)
            {
                
           Used_3 = 1;
           Counter counter3 = new Counter();
           getWorld().addObject(counter3, 136, 99);
           setImage (new GreenfootImage ("5-2=?", 50, Color.WHITE, Color.BLACK));
          }
       }
You need to login to post a reply.