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

2017/4/3

Global variable

alinasomcutean alinasomcutean

2017/4/3

#
I have a die who is rolling until you press space and I want to keep in a global variable the number at the die was stopped when you pressed it, but after a few seconds the die starts rolling again. How should I write this? Thank you!
Super_Hippo Super_Hippo

2017/4/3

#
What exactly is a "global variable"? What are you trying to do with it?
danpost danpost

2017/4/3

#
alinasomcutean wrote...
I have a die who is rolling until you press space and I want to keep in a global variable the number at the die was stopped when you pressed it, but after a few seconds the die starts rolling again. How should I write this? Thank you!
You would need something like this:
// instance fields
int value; // die value (valid internal values are 0 through 5)
int timer; // to control rolling  (<= 0) and stopped time (> 0)

public void act()
{
    if (timer <= 0) roll(); else timer--;
}

private void roll()
{
    if (timer == 0) newface()
    timer = (--timer)%5;
    if (Greenfoot.isKeyDown("space")) timer = 180;
}

private void newFace()
{
    value = Greenfoot.getRandomNumber(6);
    setImage(""+(value+1)+".png");
}
alinasomcutean alinasomcutean

2017/4/4

#
I need it because i need to move a girl the number of boxes which the die showed. And i want to do this the computer, not the person who plays. And it is repeted until you arrive at 29 from 1. Did you understand?
You need to login to post a reply.