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

2015/4/5

How does random work?

MrFlesh MrFlesh

2015/4/5

#
I am trying to make pipes appear lower or higher on my flappy bird tutorial, but I am not sure how to insert the random code.
1
addObject(pipe, getWidth(), getHeight()/2 + image.getHeight(getRandomNumber(int -20,-60) );
How can I fix this?
danpost danpost

2015/4/5

#
The 'getRandomNumber' method is not a member of the World class. Nor is it a member of any other class you are likely to be coding in. It is a member of the Greenfoot class and you must inform the compiler that you are using a method from that class. This is done by preceding the method name with the class name followed by a dot. The 'import greenfoot.*;' statement on the first line brings the class into the scope of the project (makes the class available to use members of). If you look at the Greenfoot class documentation and check out the 'getRandomNumber' method, you will find that only one int value is allowed in its parameter list. Also, if you look at the GreenfootImage class documentation, you will find you cannot put any parameters in the 'getHeight' method. As I do not know what your actual intention was with the line you gave above and cannot discern what the '-20' and '-60' represent, I probably cannot make the correction you need without more background information and code. 'pipe' and 'image' are variables that are not defined in the code and it is unclear if the image is of the upper pipe, the lower pipe, or both combined.
MrFlesh MrFlesh

2015/4/5

#
Thank you for the reply danpost, here is the full code for my world class:
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
31
public class FlappyWold extends World
{
     
    int counter= 0;
 
    /**
     * Constructor for objects of class FlappyWold.
     *
     */
    public FlappyWold()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false);
         
        //create a flappy bird object
        FlappyBird flappy = new FlappyBird();
         
        //add flappy to our world
        addObject(flappy,100, getHeight()/2);
    }
     
    public void act() {
        counter++;
        if (counter == 100) {
            //create a pipe
            Pipe pipe = new Pipe();
            GreenfootImage image = pipe.getImage();
            addObject(pipe, getWidth(), getHeight()/2 + image.getHeight + (getRandomNumber(int -20,-60) );
        counter = 0;
    }
    }
My intentions were to spawn pipes everyone 100 tics that have random height of -20 to -60 pixels high.
danpost danpost

2015/4/5

#
To get a random number between -20 and -60, use:
1
int rand = Greenfoot.getRanomNumber(40)-60;
The range is 40 and the minimum value is -60.
Super_Hippo Super_Hippo

2015/4/5

#
Wouldn't it be this?
1
int rand = Greenfoot.getRandomNumber(41)-60;
40-60 = -20 0-60 = -60 By the way, a height can't really be negative.
danpost danpost

2015/4/5

#
Super_Hippo wrote...
Wouldn't it be this?
1
int rand = Greenfoot.getRandomNumber(41)-60;
40-60 = -20 0-60 = -60
To include the higher limit of '-20', yes.
By the way, a height can't really be negative.
I presumed the number was actually an offset from a specific height. Also, MrFresh stated 'pixels high', which would be in the negative direction as the y-coordinates go.
MrFlesh MrFlesh

2015/4/5

#
Lol this is incredibly frustrating. public void act() { counter++; if (counter == 100) { //create a pipe Pipe pipe = new Pipe(); GreenfootImage image = pipe.getImage(); addObject(pipe, getWidth(), int rand = Greenfoot.getRanomNumber(40)-60); counter = 0; } } } STILL errors are occuring
MrFlesh MrFlesh

2015/4/5

#
Nothing is working for this addObject(pipe, getWidth(), getHeigth(int rand =(Greenfoot.getRanomNumber(40)-60));
Super_Hippo Super_Hippo

2015/4/5

#
I don't know what exactly you are doing with the 'getHeight' there. This code should work, but not sure if it does what is should:
1
addObject(pipe, getWidth(), Greenfoot.getRandomNumber(41)-60);
MrFlesh MrFlesh

2015/4/5

#
Super_Hippo wrote...
I don't know what exactly you are doing with the 'getHeight' there. This code should work, but not sure if it does what is should:
1
addObject(pipe, getWidth(), Greenfoot.getRandomNumber(41)-60);
Thank you :D
You need to login to post a reply.