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

2017/1/13

Random number generator problems

PeterL PeterL

2017/1/13

#
I am having problems as my code is not creating my bandits on my game grid my code is below.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import javax.swing.*;
/**
 * Write a description of class Game here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game extends World
{

    /**
     * Constructor for objects of class Game.
     * 
     */
    public Game()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(720, 480, 1); 
        game_background rect = new game_background();
        addObject(rect,600,240);
        //String inputstring = JOptionPane.showInputDialog("Your question here:");
        Player P = new Player();
        addObject (P,30,450);
        Bandit band = new Bandit();
        int z = 0;
        while(z<=5){
            int xc = Greenfoot.getRandomNumber(8);
            int yc = Greenfoot.getRandomNumber(8);
            xc = (xc*60)+30;
            yc = (xc*60)+30;
            if(xc == 30 && yc == 450){
                addObject(band, xc+60,yc-60);
                z++;
            }else if(getObjectsAt( xc, yc, Bandit.class).isEmpty()) {
                addObject(band, xc, yc);
                z++;
            }
        }
    }
}
plz help
danpost danpost

2017/1/13

#
You are only creating one instance of the Bandit class (one Bandit object -- see line 26). The 'addObject' lines in the 'while' loop are trying to add that same bandit into the world again and again. To create multiple bandits, line 26 needs to be moved down to inside the 'while' loop. You can still end up with multiple bandits at coordinates (90, 390) if the random numbers on lines 29 and 30 end up to be 0 and 7 more than once while iterating through the loop. You can use 'null' (or 'Actor.class') instead of 'Bandit.class' in line 36 to have it check for the player as well (instead of the excess code to defer the bandit from being placed on the player). Lines 27 through 40 can be replaced with this:
int z = 0;
while(z<=5){
    int xc = Greenfoot.getRandomNumber(8);
    int yc = Greenfoot.getRandomNumber(8);
    xc = (xc*60)+30;
    yc = (yc*60)+30;
    if (getObjectsAt(xc, yc, null).isEmpty()){
        Bandit band = new Bandit();
        addObject(band, xc, yc);
        z++;
    }
}
PeterL PeterL

2017/1/17

#
Thankyou Danpost
PeterL PeterL

2017/1/19

#
I'm having problems with transferring variable across classes, and then using an actor to display that variable, could you please help.
public class xset_butt extends Actor
{
    public String xSet()
    {
        String xset = "0"; 
        return xset;
    }; 
    
    /**
     * Act - do whatever the xset_butt wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
         if (Greenfoot.mouseClicked(this))
        {
          String xset = JOptionPane.showInputDialog("Set X movement");
        }
    }    
}

public class Xvar extends Text
{
    /**
     * Act - do whatever the Xvar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    xset_butt xset = new xset_butt();
    public void act() 
    {
        setImage(new GreenfootImage(xset.xSet(),35,Color.YELLOW,new Color (0,0,0,0)));
        Greenfoot.updateImage();
    }    
}
PeterL PeterL

2017/1/19

#
these are in two different actors.
Super_Hippo Super_Hippo

2017/1/19

#
1. The xSet method will always return the String "0". 2. The xset in line 5/6 is different from the one in line 17. 3. No xset variable is saved anywhere. To get the variable from the Xvar class, you need a reference to the xset_butt object (!) which is holding the variable you want to get. And first, you have to save the variable in a field in the xset_butt class. I would probably do it the other way around. From your xset_butt class, whenever the object is clicked and a value is put in, you pass that value to the Xvar object which can then change its image accordingly. So after line 17, you need to get a reference to the Xvar object, call a method on it (which you have to add in the Xvar class) and pass the xset. The new method in the Xvar class could then look like this (and you can remove the act method of that class).
public void updateText(String xset)
{
    setImage(new GreenfootImage(xset,35,Color.YELLOW,new Color (0,0,0,0)));
}
Btw, what is line 32 doing?
PeterL PeterL

2017/1/19

#
I was told this was how to update an image.
danpost danpost

2017/1/19

#
PeterL wrote...
I was told this was how to update an image.
I wonder who told you that (unreliable source?). There is no 'updateImage' method in the Greenfoot class at all.
You need to login to post a reply.