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

2020/3/17

Random number generator influencing "isKeyDown"

NotGoodAtThis NotGoodAtThis

2020/3/17

#
i'm making a racing game where four characters run from the left of the screen to the right. Four random number generators will run. The output of these generators must be stored as variables so that i can have four icons on screen change to show the appropriate number button to press. I must also have the player's "greenfoot.isKeyDown" change so that pressing the four buttons changes the players speed to a new value. This is the code for my "Blankclass" it exists purely to hold the random number generator
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Blankclass here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Blankclass extends Actor
{
    public float KeyOne = Greenfoot.getRandomNumber(9);
    public float KeyTwo = Greenfoot.getRandomNumber(9);
    public float KeyThree = Greenfoot.getRandomNumber(9);
    public float KeyFour = Greenfoot.getRandomNumber(9);
    /**
     * Act - do whatever the Blankclass wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}
This is the code for my player
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player1 extends Players
{
    /**
     
     * Act - do whatever the Player1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int Plyrspeed = 1;
    
    public void act() 
    {
     move(Plyrspeed);
     
     setImage(gifImage.getCurrentImage());
     
     if(isTouching(FinishLine.class))
       {
          Plyrspeed = 0;
       }
       
       if(Greenfoot.isKeyDown(KeyOne)) {
    Plyrspeed = 2;
}
    }    
    
    GifImage gifImage = new GifImage("128.gif");
}
I'm basically a beginner and thought something like this would be very easy to pull off. It's my college work so it's not like i can just drop it at this point. Thanks in advance.
danpost danpost

2020/3/17

#
A) I do not see where any Blankclass object is being created; I see nothing of the icons, either; B) KeyOne, KeyTwo, KeyThree and KeyFour are declared to hold float values; yet, you use them to hold the returned random int values; C) The isKeyDown method requires a String argument where you are trying to use one of type float; What keys are you allowing to increase the player's speeds?
NotGoodAtThis NotGoodAtThis

2020/3/17

#
Thanks for your reply. I've got four actors that each have an image of the keyboard key 1-9. They will use the output of the variables to decide which key to show. I want the output of the generator to be saved as a sting not an integers. The blankclass will never be in the world. It's just so that both the input indicators and the player can inherit from it.
danpost danpost

2020/3/17

#
NotGoodAtThis wrote...
I want the output of the generator to be saved as a sting not an integers.
An int value can be converted to a String representation of that value at any time using ""+intValue
The blankclass will never be in the world. It's just so that both the input indicators and the player can inherit from it.
You still need to create one, nonetheless, for any random values to be generated. Also, nothing prevents two random values being the same.
You need to login to post a reply.