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

2016/10/21

Random direction movement

JarJarBanks7 JarJarBanks7

2016/10/21

#
I need help figuring out some code to have an actor pick a random direction(1-360) and move that way. I dont want it to turn more than once just pick a direction and go till it touches the edge. I dont have any starting code
danpost danpost

2016/10/21

#
That can be done in the constructor of the class of the object you with to turn randomly. But, it can also be done when creating the object:
1
2
3
Actor actor = new ActorClassName();
actor.turn(Greenfoot.getRandomNumber(360));
addObject(actor, < x, y >); // plug in location coordinates
JarJarBanks7 JarJarBanks7

2016/10/21

#
i dont know how but its not turning, this look right? Should it be in the actor or world?
1
2
3
4
5
6
7
8
9
10
public void act()
    {
        move();
    }
    private void move(){
         
      Ball ball = new Ball();
      ball.turn(Greenfoot.getRandomNumber(360));
      move(1);
    }
update: if i removed the ball. from the turn method it just spins forever
danpost danpost

2016/10/22

#
JarJarBanks7 wrote...
i dont know how but its not turning, this look right? Should it be in the actor or world?
No-- it does not look right. The only thin in the act method of the ball class (at least for now) is line 9. You should previously had code in your World subclass for creating (my line 1 -- except 'ActorClassName' is 'Ball') and adding to the world (my line 3 -- with coordinate values). That is where you need to introduce my line 2. Unfortunately, you will find that even though your ball may take on any one of 360 degrees, it will only move in one of eight possible directions due to the limitations imposed by the pixels of the screen. From any pixel, when moving just one unit, you can only arrive at one of the surrounding pixels (above, below, to either side or diagonally in any of four directions).
JarJarBanks7 JarJarBanks7

2016/10/24

#
What i understood from that was put no code in the ball subclass.The code im showing now is from the world class.Im currently getting an error with the getBall() class it says,"invalid method declaration; return type required." do tell, how do i fix? after that problem is fixed, do you see any other problems ill run into? Also if you see anyway to add all those bricks via for loop please tell me.
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
import greenfoot.*;
public class PongWorld extends World
{
    private Ball ball = new Ball();
    public PongWorld()
    {   
        super(600, 400, 1);
        addObject(new Paddle(),300,380);
        addObject(new Brick(),50,43);
        addObject(new Brick(),100,43);
        addObject(new Brick(),150,43);
        addObject(new Brick(),200,43);
        addObject(new Brick(),250,43);
        addObject(new Brick(),300,43);
        addObject(new Brick(),350,43);
        addObject(new Brick(),400,43);
        addObject(new Brick(),450,43);
        addObject(new Brick(),500,43);
        addObject(new Brick(),550,43);
        addObject(new Brick(),600,43);
        getBall();
    }
    public getBall(){
        addObject(ball,300,200);
        Ball.turn(Greenfoot.getRandomNumber(360));
        move(1);
    }
}
danpost danpost

2016/10/24

#
I must have accidentally deleted a substring from my last post. This sentence:
The only thin in the act method of the ball class (at least for now) is line 9.
should have read like this:
The only thing that should be in the act method of the Ball class (at least for now) is line 9.
Replace lines 21 through 26 above with the following:
1
2
addObjects(ball,300,200);
ball.turn(Greenfoot.getRandomNumber(360));
JarJarBanks7 wrote...
if you see anyway to add all those bricks via for loop please tell me.
A 'for' loop would easily work. A 'while' loop could be used as an alternative, but not quite as simply.
You need to login to post a reply.