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

2012/5/3

Help with booleans

toytttttt toytttttt

2012/5/3

#
In a scenario I am working on I want a system so that when you press a button it will set a boolean so another actor can do something when this button is pressed. I currently have: In the button class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
/**
 * Write a description of class welcomeBanner here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class welcomeBanner extends Actor
{
    /**
     * Act - do whatever the welcomeBanner wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        textMake();
        gettingStarted();
    }    
    public void textMake()
    {
        setImage(new GreenfootImage(" Welcome to Doors OS, Click here to get started ", 30, Color.BLACK, Color.BLUE));
    }
    public void gettingStarted()
    {
        if (Greenfoot.mouseClicked (this))
        {
             boolean timeToGetStarted = true;
              System.out.println("worked");
        }
    }
}
In the world class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Background here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{

    /**
     * Constructor for objects of class Background.
     * 
     */
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        welcomeBanner welcomebanner = new welcomeBanner();
        addObject(welcomebanner, 285, 190);
    }
    public void startingUp()
    {
        if (timeToGetStarted == true)
        {
            //do whatever to do
        }
    }
}
But when I do this in the world class it highlights timeToGetStarted and says cannot find symbol variable timeToGetStarted What am I doing wrong? Any help will be greatly appreciated, With thanks, toytttttt
tylers tylers

2012/5/3

#
you need to do
if (welcomeBanner.timeToGetStarted == true)  
        {  
            //do whatever to do  
        }  
hope that works
danpost danpost

2012/5/3

#
1) I do not see the method 'startingUp()' in the world class being called from anywhere 2) I do not see a variable declaration for the boolean timeToGetStarted in either class (probably should be in the 'welcomeBanner' class) It would probably be easier just to have the banner remove itself when clicked on and the world would only have to wait until the 'welcomebanner' object is no longer in the world, with
if (getObjects(welcomeBanner.class).isEmpty()) ...
This way you avoid the boolean variable altogether.
toytttttt toytttttt

2012/5/4

#
Tylers, I tried this but I got the same error message, any help? Danpost, You are right about about starting up not being called, but I didn't think that would mean I would get the error message I got (tell me if I'm wrong) will fix. For your second suggestion, it is declared on line 29, or I think that is declaring it. Also, I sort of want the button to stay where it is, even after being clicked, so thanks for the suggestion but it wouldn't quite work. Thanks, toytttttt
tylers tylers

2012/5/4

#
have you declared timeToGetStarted in Welcomebanner as danpost said?
ttamasu ttamasu

2012/5/4

#
I am going to basically repeat exactly what tyler and danpost said but make it more explicit. On line 29 of welcomeBanner, you make a local variable declaration timeGetStarted. What this means is that once you leave the if statement, the variable no longer exists (causing a symbol cannot find error). Like danpost mentioned you need to make a class variable (or one in which the variable exists as long as the object of the class exists ) To fix this you move the variable declaration on line 29 to somewhere like line 12. Second point that danpost made is that you need to call StartingUp() in the Act() method of the Background class. Also you need to tell it that the startup code has been used so after it does the startup code you need to set timeGetStarted to false (or every cycle you will do the startup code). on lines
You need to login to post a reply.