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

2014/11/23

How to make an actor move relative to the result of a dice

SilentPenguin SilentPenguin

2014/11/23

#
How do i make an actor move relative to the result of a die. My die code is below, how can I gather information from one actor and give it to another (similar to worldref) and then to move the actor accordingly to the result of the dice. Many thanks import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Dice here. * * @author (your name) * @version (a version number or a date) */ public class Dice extends Actor { /** * Act - do whatever the Dice 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)) { int randNumber = Greenfoot.getRandomNumber(4); randNumber = randNumber + 1; GreenfootImage Dice = new GreenfootImage( randNumber+"" , 25, Color.WHITE, Color.BLACK); set
danpost danpost

2014/11/23

#
In the following, I am presuming that you only have one Dice object in the world. If you had a reference to the Dice object in your world class, you could check for the click in the act method of that class and call a method in the Dice class that returns the value of the roll (instead of using the act method in the Dice class which does not return a value being it has a 'void' return type). So, the Dice class would be:
import greenfoot.*;
import java.awt.Color;

public class Dice extends Actor
{
    // constructor to give the dice its initial image
    public Dice()
    {
        roll();
    }

    // called by world to get a new dice roll value
    public int roll()
    {
        int randNumber = 1+Greenfoot.getRandomNumber(4);
        setImage(new GreenfootImage(""+randNumber, 25, Color.WHITE, Color.BLACK));
        return randNumber;
    }
}
In your world class, you would have the following parts:
// instance reference field to hold the Dice object 
private Dice dice = new Dice();
// in world constructor (add dice to world)
addObject(dice, /* x and y cooridnates */);
// in world act method
if (Greenfoot.mouseClicked(dice))
{
    int rollValue = dice.roll();
    // etc.
SilentPenguin SilentPenguin

2014/11/23

#
Thank but what actually does this do does this therefore return a random number between 1 and 4. How would I go about making one player move accordingly to the result of the die roll. This is what i think what do I need to change. I know the basics but an error keeps occuring saying that randNumber is not defined, do I need to define it again and where or does this code compensate? Die code public void Dice() { if (Greenfoot.mouseClicked(this)) { int randNumber = Greenfoot.getRandomNumber(4); randNumber = randNumber + 1; GreenfootImage Dice = new GreenfootImage( randNumber+"" , 25, Color.WHITE, Color.BLACK); setImage(Dice); } Now my thoughts on making players move according to die result If randNumber == 1 { move(1) } And then it would therefore move one when clicked. Etc for result of 2 , move 2, 3, move 3 etc. Thanks
danpost danpost

2014/11/23

#
Line 15 in the roll method above will set the 'randNumber' variable to a number between 1 and 4 and line 17 sends that same value back to the calling statement (which is the last line of actual code within that post -- line 8 of the world class code, where it is saved in a variable called 'rollValue'). At that point you can use 'move(rollValue)' on the player to move. But you will need an array for the players in the game and an int field to track which one has the turn. For example:
// fields
private Dice dice = new Dice();
private Player[] players = { new Player(0), new Player(1) };
private int hasTurn = 0;
// in act when rolling dice to move player
players[hasTurn].move(dice.roll());
You need to login to post a reply.