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

2014/3/30

Using arrays to bring random images into the screen

samarthj samarthj

2014/3/30

#
I am trying to create a math project and I want random numbers to come from the right side of the screen. I have created the following code in a subclass (called Background) of the World. Request you to help me with the right code. (Numbers is the class) (below the public Background()) int numbers={ 13,20,56,34,23,21}; public void act() { if(Greenfoot.getRandomNumber (100)<3) { for (int i=0; i<numbers.length; i++) { addObject(new Numbers(), 967, Greenfoot.getRandomNumber (531)); } } }
samarthj samarthj

2014/3/30

#
Repost please help with the code or correction
danpost danpost

2014/3/30

#
One thing is your array of numbers should be declared as follows:
1
int[] numbers = { 13, 20, 56, 34 23, 21 };
the square brackets informs the compiler that you are using an array of that type. With what you have then, you should have six '0's appearing at the right edge of the screen (presuming that the image of the numbers is not the same color as the background and the Numbers field produces the image of a number). However, to have the numbers in your array displayed by objects of the Numbers class, you will have to pass each number to each Numbers object you create. The way to do that is to add an argument to the Numbers constructor to receive that number.
1
2
3
4
5
public Numbers(int number)
{
    // create image of number
    // set image
}
Then, to pass a number every so often (rotating through the array):
1
2
3
4
5
6
7
8
9
10
private int index;
 
public void act()
{
    if (Greenfoot.getRandomNumber(100) < 3)
    {
        index = (index+1)%numbers.length; // points to each element in the array in succession, then repeats
        addObject(new Numbers(numbers[index]), 967, Greenfoot.getRandomNumber(531));
    }
}
samarthj samarthj

2014/3/31

#
Thanks a lot for the reply. I want multiple images to come from the right side of the screen where as I am currently able to only see the logo of greenfoot. I tried setImage but it is only giving me a single image. Please help me with the code and the class where the code needs to be added.
danpost danpost

2014/3/31

#
samarthj wrote...
I tried setImage but it is only giving me a single image.
Please show what you tried (show what code you tried in the Numbers class). Use the 'code' link below the 'Post a reply' box to insert your code into your post.
samarthj samarthj

2014/3/31

#
The code is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public Numbers(int numbers) 
    
        // create image of number 
         setImage("a0.bmp")
         setImage("a1.bmp")
         setImage("a2.bmp")
         setImage("a3.bmp")
         setImage("a4.bmp")
         setImage("a5.bmp")
          
         
          
    }
    int[] numbers = {0,1,2,3,4,5};
    int i = 0;
 
    private int index;
 
{
        move(-5);
        if(Greenfoot.getRandomNumber(200)<3)
        {     
             
            index = (index+1)%numbers.length; // points to each element in the array in succession, then repeats 
            addObject(new Numbers(numbers[index]), 967, Greenfoot.getRandomNumber(531));
 
             
 
        }
    }
Please help me with the corrections.
danpost danpost

2014/3/31

#
Well, your world class should be spawning the number and adding them into itself -- that is: everything from line 21 on should be in your world class act method. I think you may have a misunderstanding of what a class is about. A class is a 'blueprint' for an object (the 'specifications', you might say). You can create one or a multitude of similar distinct objects from one class. For a class that would display a number, I would start with:
1
2
3
4
5
6
7
8
9
import greenfoot.*;
 
public class Number extends Actor
{
    public Number(int num)
    {
        setImage(new GreenfootImage(""+num, 48, java.awt.Color.blue, null));
    }
}
This will create an object with the given number as its image. You can now add an act method to it to allow objects created from this class to move from right to left across the screen:
1
2
3
4
public void act()
{
    move(-5);
}
You probably will need to remove the object if it reaches the left edge of the world, so add to the act method:
1
if (getX() == 0) getWorld().removeObject(this);
The rest of your code above from line 21 on (plus lines 14 and 17) should go in your subclass of World. The world is what will control the spawning of the Number objects.
samarthj samarthj

2014/3/31

#
Amazing!!!!! Thanks a lot. I am spellbound!!! It is coming out very nice. I am facing another problem, that is when I tried to use the following code I am facing an error. The code is :
1
2
3
4
5
6
7
8
9
10
11
12
import greenfoot.*;
 
public class Background extends World
{
     GreenfootSound backgroundMusic = new GreenfootSound("Bright_Skies_030_sec_preview.mp3");
 
public Background()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(978, 531, 1);
        backgroundMusic.playLoop();
        prepare();
The error is:
1
2
3
4
5
6
7
8
Exception in thread "SoundStream:file:/C:/Users/manish/Desktop/Greenfoot%20workshop%20by%207C%20boys/Math-o-meter/sounds/Bright_Skies_030_sec_preview.mp3" java.lang.ArrayIndexOutOfBoundsException: 580
    at javazoom.jl.decoder.LayerIIIDecoder.huffman_decode(LayerIIIDecoder.java:795)
    at javazoom.jl.decoder.LayerIIIDecoder.decode(LayerIIIDecoder.java:278)
    at javazoom.jl.decoder.LayerIIIDecoder.decodeFrame(LayerIIIDecoder.java:219)
    at javazoom.jl.decoder.Decoder.decodeFrame(Decoder.java:147)
    at greenfoot.sound.Mp3AudioInputStream.read(Mp3AudioInputStream.java:230)
    at greenfoot.sound.SoundStream.run(SoundStream.java:302)
    at java.lang.Thread.run(Thread.java:744)
This error comes in the Greenfoot:Terminal Window - Math - o -Meter Please help me with the corrections to be performed.
danpost danpost

2014/3/31

#
I appears that your sound file has information tags within it that are messing with greenfoot's decoder. Try to resave the sound file without the info tags.
samarthj samarthj

2014/4/1

#
Thank you. I have another doubt. The question and answer is appering very good and if the main actor collides with the correct answer, the screen says correct answer and after that I want to proceed to the next question. Right now the first question itself appears again and with the same options but does not show the second. Please help me with the code that needed to be executed. I want to put 3 questions for it.
danpost danpost

2014/4/1

#
Cannot help without seeing what code you are trying to use. Everything dealing with the fields related to the question and answers.
You need to login to post a reply.