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

2013/2/3

Adding randomDogs to DogWorld

CHERNANDEZ95 CHERNANDEZ95

2013/2/3

#
I am hoping someone could help me find the problem. I need to write and construct a constructor for randomDogs(int population) which introduces three Dog objects at random locations in the DogWorld. The code compiles without any problems, but I do not see any random dogs in the world. I am still learning but this is what I have so far: public class DogWorld extends World { /** * Constructor for objects of class DogWorld. * */ public DogWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); drawDog(); randomDogs(); } /** * Add Dog to world. */ public void drawDog() { addObject( new Dog(), 300, 200 ); } /** * Adds dogs to world at random locations. */ public void randomDogs() { if ( Greenfoot.getRandomNumber(10)== 3 ) { addObject( new Dog(), 69, 125); } } }
Gevater_Tod4711 Gevater_Tod4711

2013/2/3

#
You should change the method randomDogs() like this:
public void RandomDogs() {
    for (int i = 0; i < 3; i++) {
        addObject(new Dog(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    }
}
If you change the number '3' in the for loop to another value there will be this number of dogs.
danpost danpost

2013/2/3

#
With the code as it is written, there is a '1 in 10' chance that one new dog object will be created from 'randomDogs'. An 'if' statement is not what you need here. You need the kind of statement that will perform a task multiple times with the location coordinates of each one randomly chosen.
CHERNANDEZ95 CHERNANDEZ95

2013/2/3

#
Thank you very much for your explanations and sharing your knowledge with me.
CHERNANDEZ95 CHERNANDEZ95

2013/2/3

#
I hope I can trouble you all for another problem. I need to be able to control the Dog using the arrow keys and I was able to do that with the up, down, left, and right arrows; however, I ran into a problem when I tried to make the dog move northeast pressing the up and right arrow key at the same time. This is what I have: public void checkKeypress() { if(Greenfoot.isKeyDown("up" + "right")) { setRotation(45); move(5); setImage("dog-walk-right.png"); wait(3); setImage("dog-walk-right-2.png"); } } Could anyone please tell me what I am doing wrong?
Gevater_Tod4711 Gevater_Tod4711

2013/2/4

#
The problem is that you are using Greenfoot.isKeyDown("up" + "right") what is the same than Greenfoot.isKeyDown("upright") and this key just doesn't exist. If you want to check whether both of the keys are pressed you have to use this:
if (Greenfoot.isKeyDown("up") && Greenfoot.isKeyDown("right")) {
    //...
}
danpost danpost

2013/2/4

#
The use of 'wait(3)' will cause the whole scenario to essentially pause for the duration of the waiting time, which I doubt seriously is what you want. What you need to do is add a counter to count the act cycle;, and every so many cycles, change the image; and after so many more cycles, change the image back and reset the counter.
You need to login to post a reply.