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

2016/5/27

Generating falling objects randomly on the X axis

KrystalLo KrystalLo

2016/5/27

#
Hello, I was just wondering how do you generate objects randomly on the X axis? I'm creating a game and their is a falling bombs and coins from the sky, but I don't know how to make the bombs and coins fall down randomly on the X axis. Please help me? And sorry, I'm actually new to this and only registered today :D Down below is my code for 'MyWorld' Currently the coins and bombs are just falling at the same time and in one place. I'm not too sure if the bomb can be in the act code as the coin but I created a separate one for the bomb, because the bomb makes the protagonist lose health and the coins increases the score points.
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    //background was taken from this website
    //background was edited to make the height higher
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(659, 483, 1);
        prepare();
         
       // randomCoins();
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Pirate pirate = new Pirate();
        addObject(pirate,306,411);
        pirate.setLocation(286,411);
        pirate.setLocation(268,414);
    }
    int iCounter = 0;
    
    public void act(){
    iCounter++;
    if (iCounter==90){
         iCounter =0;
          
           Coin myCoin = new Coin();
           addObject(myCoin,100,0);
             
            Bomb myBomb = new Bomb();
            addObject(myBomb,200,0);
             
        }
    }
}
danpost danpost

2016/5/27

#
On lines 46 and 49, you are using a specific value for the x-coordinate value to place the objects. If you change them to a random value between zero and one less than the width of your world. There are several ways to get a random value. The java.lang.Math class has a 'random' method that returns a non-negative double value less than one (which can be multiplied by your limit and rounded). There is a java.util.Random class that has a 'nextInt(int limit)' method that can be used to get a non-negative integer value less than the given limit; and finally, the Greenfoot class has a 'getRandomNumber(int limit)' method that does the same thing as the 'nextInt(int limit)' method of the java.util.Random class. Since you want a non-negative integer value less than the width of your world, the Greenfoot class 'getRandomNumber' method would suffice.
KrystalLo KrystalLo

2016/5/27

#
I've seemed to figured how how to generate it :D
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    //background was taken from this website
    //background was edited to make the height higher
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(659, 483, 1);
        prepare();
         
       // randomCoins();
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Pirate pirate = new Pirate();
        addObject(pirate,306,411);
        pirate.setLocation(286,411);
        pirate.setLocation(268,414);
    }
    int iCounter = 0;
    
    public void act(){
    iCounter++;
    if (iCounter==60){
         iCounter =0;
          
           Coin myCoin = new Coin();
           //addObject(myCoin,100,0);
            addObject(new Coin(),Greenfoot.getRandomNumber(getWidth()),0);
            //http://www.greenfoot.org/topics/4602/0
        }
        }
    }
I figured it out by looking at other discussions. Thank you anyway :D , and I was just wondering how do you set different images for one actor?
danpost danpost

2016/5/27

#
KrystalLo wrote...
how do you set different images for one actor?
Use the 'setImage' method of the Actor class on said actor. Refer to the Actor class API on that method. For any further information, you will need to be more specific (what conditions are required for the image to change, etc.).
KrystalLo KrystalLo

2016/5/27

#
Well I have 3 types of coins , gold, silver and bronze. And I want them to randomly mix and generate when falling. Gold will give the player 15 points, silver will give 10 and bronze 5. This is the coding I have so far, I've implemented a code I used that my teacher has taught my class. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Coin here. * * @author (your name) * @version (a version number or a date) */ public class Coin extends Actor { int iColor =0; public Coin(int iTn){ iColor = iTn; if((iColor % 2)==1){ this.setImage("coin2.png"); } } /** * Act - do whatever the Coin wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Coin(){ } public void act() { // Add your action code here. setLocation(this.getX(),this.getY()+3); // at the bottom of the world delete the coin if(getY()>430){ this.getWorld().removeObject(this); } } } coin1 (goldcoin) is currently the set image, and so I put coin2 in the code which is the silver coin. I'm not sure if I did the wrong code... but it worked while my class put the piece of code in another java scenario that we were learning on.
danpost danpost

2016/5/27

#
Okay -- so you only need to set the image for each coin once when it is created (not changing the image while it is in the world). This was "need to know information" in order to provide the proper reply to your query. Since your coin images are in "coin1.png", "coin2.png" and "coin3.png" (presumably), you can simply change your Coin class constructor to this:
1
2
3
4
public Coin(int iTn)
{
    setImage("coin"+(iTn+1)+".png");
}
This may not be the final result required as you are using the modulus function with a divisor of '2' in the given constructor and I am not sure what you were trying to accomplish with that. Maybe it was just a temporary thing or maybe you were going to allow larger numbers for the parameter and reduce them to the number within the valid image count range.
danpost danpost

2016/5/27

#
I also noticed that you had an empty basic constructor in the class -- specifically this:
1
2
public Coin() {
}
You should probably change that to the following:
1
2
3
public Coin() {
    this(0);
}
This will have the other constructor execute with a parameter value of zero.
KrystalLo KrystalLo

2016/5/28

#
1
2
3
4
public Coin(int iTn)
{
    setImage("coin"+(iTn+1)+".png");
}
So for this code, can I put in the 3 images? So it randomises a coin image? such as adding (iTn+1+2+3) something like that? and/or do I have to put it in another way? Sorry if i'm not explaining it properly. I'll probably ask my teacher for assistance if I don't understand it. Oh , and I got a few more questions and help I need. I was wondering, if I wanted to put a bomb into the game, and it randomises on the X axis aswell with the coins, how can i do it so it doesn't fall at the same time with the coins? I put the getObject, randomnumber code but this time with the bomb, but it kind of mirrors each other and isn't randomised and falls at the same time. But honestly, I think i'm doing it incorrectly. Sorry for asking so much questions.
danpost danpost

2016/5/28

#
KrystalLo wrote...
So for this code, can I put in the 3 images? So it randomises a coin image? such as adding (iTn+1+2+3) something like that? and/or do I have to put it in another way?
Well, not like that. Oh, before I forget what I missed -- you still need the 'iColor' field and to set 'iTn' to it in the constructor:
1
2
3
4
5
6
7
8
9
// instance field
private int iColor;
 
// main constructor
public Coin(int iTn)
{
    iColor = iTn;
    setImage("coin"+iTn+".png"); // I adjusted this line for simplification
}
What you need to do for random falling objects of any coin type or a bomb is to use the world act method. You have (at least as far as I know right now) four different types of "to-be random"ly falling objects. So:
1
2
3
4
// when determined it is time to spawn a new object (in world act method)
int rand = Greenfoot.getRandomNumber(4);
int x = Greenfoot.getRandomNumber(getWidth());
addObject(rand == 0 ? new Bomb() : new Coin(rand), x, 0);
You will probably need a method to return the type of coin, or at least the number of points that the coin will give to the player picking it up. In the Coin class, add the following:
1
2
3
4
public int getPoints()
{
    return 5*(4-iColor);
}
BTW, the 'addObject' line above is shorthand for this:
1
2
3
4
5
6
7
8
9
10
Actor actor = null;
if (rand == 0)
{
    actor = new Bomb();
}
else
{
    actor = new Coin(rand);
}
addObject(actor, x, 0);
KrystalLo KrystalLo

2016/5/28

#
Oh, I got it working! Thank you so much Dan! I'll probably ask more questions a bit later after I do a few more things with the game. Thank you!
You need to login to post a reply.