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

2013/4/14

Trying to access a method from a different class

thekidj thekidj

2013/4/14

#
I'm trying to access a method I created in one of the subclasses for the World class. I named the subclass for the world "Level1" and the method is called "selectRandom". I want to access it from one of my actor subclasses so I tried Level1.selectRandom() and that didn't work. Am I going about this wrong?
danpost danpost

2013/4/14

#
You need a world object to run the 'selectRandom' method on. You can get the world the object is in with:
Level1 lev1 = (Level1) getWorld();
The only thing is, you may be in a Level2 world and not a Level1 world. Therefore you will have to check each possible instance:
World world = getWorld();
if (world instanceof Level1) ((Level1)world).selectRandom();
if (world instanceof Level2) ((Level2)world).selectRandom();
// etc.
thekidj thekidj

2013/4/14

#
@danpost I'm trying to make a game that generates a random number and displays a die with the number that was generated, but it doesn't stop generating a random number after the first one.
public class Level1 extends World
{
    int points = 0;
    private String[] leftDie = {"one", "two", "three", "four", "five", "six"};
    private String[] rightDie = {"one", "two", "three", "four", "five", "six"};
    private int[] roll = {1, 2, 3, 4, 5, 6};
    /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        setBackground("old table.jpg");
    }

    public void act()
    {
       selectRandom();
       displayImage();
    }
    
    public int selectRandom()
    {
        int r = Greenfoot.getRandomNumber(roll.length)+1;
        return r;        
    }
    public void displayImage()
    {
        if (selectRandom() == 1)
        {
            LeftDie die = new LeftDie(leftDie[0]+".png");
            addObject(die, 200, 250);
        }
        if (selectRandom() == 2)
        {
            LeftDie die = new LeftDie(leftDie[1]+".png");
            addObject(die, 200, 250);
        }
        if (selectRandom() == 3)
        {
            LeftDie die = new LeftDie(leftDie[2]+".png");
            addObject(die, 200, 250);
        }
        if (selectRandom() == 4)
        {
            LeftDie die = new LeftDie(leftDie[3]+".png");
            addObject(die, 200, 250);
        }
        if (selectRandom() == 5)
        {
            LeftDie die = new LeftDie(leftDie[4]+".png");
            addObject(die, 200, 250);
        }
        if (selectRandom() == 6)
        {
            LeftDie die = new LeftDie(leftDie[5]+".png");
            addObject(die, 200, 250);
        }
    }
}
This is what I have so far. Anything stand out to you that looks incorrect?
danpost danpost

2013/4/15

#
Yeah. There are many things (unfortunately) that I see wrong here. The biggest thing is that most of the code given should be in the 'die' class (by convention, class names should start with an uppercase letter); and you should only have one 'Die' class (used for both dice).
// your world should have this:
import greenfoot.*;

public class Level1 extends World
{
    int points;
    Die leftDie = new Die();
    Die rightDie = new Die();

    public Level1()
    {    
        super(600, 400, 1); 
        setBackground("old table.jpg");
        addObject(leftDie, 200, 250);
        addObject(rightDie, 250, 200); // wherever
    }

    public void act()
    {
        if (leftDie.isRolling() || rightDie.isRolling()) return;
        // nothing more at the moment
    }
}
// then, the basic Die class should be like this:
import greenfoot.*;

public class Die extends Actor
{
    private static final GreenfootImage[] IMAGES = {
        new GreenfootImage("one.png"),
        new GreenfootImage("two.png"),
        new GreenfootImage("three.png"),
        new GreenfootImage("four.png"),
        new GreenfootImage("five.png"),
        new GreenfootImage("six.png") };
    private static final int TUMBLE_SPAN = 20;
    private static final int MIN_TUMBLES = 5;
    private static final int RNG_TUMBLES = 6;
    private rollTimer;
    private value;

    public Die()
    {
        setRandomValue();
    }

    public void act()
    {
        if (rollTimer == 0) return;
        rollTimer--;
        if (rollTimer % TUMBLE_SPAN == 0) tumble();
    }

    private void tumble()
    {
        setRandomValue();
        // maybe play a sound here
    }

    private void setRandomValue()
    {
        value = Greenfoot.getRandomNumber(6)+1;
        setImage(IMAGES[value-1]);
        setRotation(Greenfoot.getRandomNumber(360));
    }

    public void toss()
    {
        int randRange = Greenfoot.getRandomNumber(RNG_TUMBLES);
        rollTimer = TUMBLE_SPAN * (MIN_TUMBLES+ randRange);
        tumble();
    }

    public boolean isRolling()
    {
        return (rollTimer == 0);
    }

    public int getValue()
    {
        return value;
    }
}
The code in the world class act method prevents any action from occuring while any die is still rolling.
You need to login to post a reply.