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

2013/11/22

Need some help

Multi Multi

2013/11/22

#
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
32
33
34
35
36
    public void act()
    {
        time = time + 1;
        if (time <= 150 ){
            move(0);
        }
        else {
 
            setLocation (Greenfoot.getRandomNumber(),getY()+dy);
            if (getY() >= getWorld().getHeight() - 1){
                dy = -5 ;
                Greenfoot.playSound("button-14.wav");
            }   
            if (getX() >= getWorld().getWidth() - 1){
                Greenfoot.setWorld ( new space());
            }   
            if (getY() <= 20){
                dy = 5;
                Greenfoot.playSound("button-14.wav");
            }
            if (getX() <= 20){
                Greenfoot.setWorld ( new space());
            }
            if (isTouching(P1.class)){
                dx = -dx;
                dy = dy-1;
                Greenfoot.playSound("button-3.wav");
            }
            if (isTouching(P2.class)){
                dx = -dx;
                dy = dy-1;
                Greenfoot.playSound("button-9.wav");
            }
        }   
    }
}
I want the object to go to a random direction in the beginning but I cant use the gerRandomNumber here is what it says any help?
Gevater_Tod4711 Gevater_Tod4711

2013/11/22

#
The method Greenfoot.getRandomNumber needs a parameter. You can't just write Greenfoot.getRandomNumber() but you have to use Greenfoot.getRandomNumber(int). In your case you probably need a random number from 0 to the worlds width (-1). So you can use Greenfoot.getRandomNumber(getWorld().getWidth()), ...
danpost danpost

2013/11/22

#
Multi wrote...
I want the object to go to a random direction in the beginning
By placing the 'setLocation' statement with the 'getRandomNumber' call in the 'act' method, the object will erratically jump vertically until a new world is set due to the 'getX()<=20' or the 'getX()>=getWorld().getWidth()-1' checks. The initial location should be set when the object is added into the world.
You need to login to post a reply.