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

2021/7/31

Check an input is in a certain range and if not displaying an object

1
2
xixEmilyxix xixEmilyxix

2021/7/31

#
I'm not sure how to write code to check an input is between 3 and 25 and if they are not then an object would be added into my world. If anyone could help that would be nice. This is the code that I set what the input will be:
public class beeInput extends Actor
{
    //create variable
    int beeValue;
    boolean error;
    /**
     * Act - do whatever the beeInput wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //Create input box, ask for value, set bounds for beeValue
        setImage(new GreenfootImage("Input.png"));
        //set value of bees based off the input
        if(Greenfoot.mouseClicked(this)){
            beeValue = Integer.parseInt(Greenfoot.ask("Please input a value for the amount of bees"));
        }
    }
}
RcCookie RcCookie

2021/7/31

#
if(beeValue < 3 || beeValue > 25) {
    // Do something if it's out of range
}
else {
    // Do something else if the value is within the range
}
xixEmilyxix xixEmilyxix

2021/8/1

#
I added that into my beeInput class last time but i can't add an object into my world unless the adding object is in my mainmenu world. i dont know how to make it so i can do that
danpost danpost

2021/8/1

#
Are you saying that the world you want to add the object into is not the same world as that which the beeInput actor is in?
xixEmilyxix xixEmilyxix

2021/8/1

#
it is but when i put the addobject code in the beeinput class it doesnt show up on the screen for the main menu class
danpost danpost

2021/8/1

#
xixEmilyxix wrote...
it is but when i put the addobject code in the beeinput class it doesnt show up on the screen for the main menu class
Show the line you tried to use (with surrounding code for context).
xixEmilyxix xixEmilyxix

2021/8/1

#
    
    public void displayError()
    {
        if(beeInput > 25 || beeInput < 3){
            addObject (errorMessagebees, getWidth(), getHeight());
            errorMessagebees.setLocation(400,400);
        } else {
            removeObject (errorMessagebees);
        }
    }
       
is what i have in the main menu (the world) but it gives an error saying bad operand types, im not sure how to make it check after someone has put in an input and not as soon as the scenario is run
xixEmilyxix xixEmilyxix

2021/8/1

#
When i had that piece of code in the beeinput class it said about cant find variables and also non static method cannot be referenced from a static context
danpost danpost

2021/8/1

#
xixEmilyxix wrote...
When i had that piece of code in the beeinput class it said about cant find variables and also non static method cannot be referenced from a static context
Shouldn't the condition be checking the value of beeValue in the beeInput actor? Although not shown, beeInput would seem to be the actor itself, probably defined as 'Actor beeInput;' or 'beeInput beeInput;', assigned using '= new beeInput()'.
xixEmilyxix xixEmilyxix

2021/8/1

#
danpost wrote...
xixEmilyxix wrote...
When i had that piece of code in the beeinput class it said about cant find variables and also non static method cannot be referenced from a static context
Shouldn't the condition be checking the value of beeValue in the beeInput actor? Although not shown, beeInput would seem to be an actor, probably defined as 'Actor beeInput;' or 'beeInput beeInput;', assigned using '= new beeInput()'.
yeah i noticed that and changed beeinput to beevalue but i was still getting the same error
danpost danpost

2021/8/1

#
xixEmilyxix wrote...
yeah i noticed that and changed beeinput to beevalue but i was still getting the same error
In your main menu class, you need more than just beeValue. You need beeValue of the beeInput actor in the world. Show main menu class codes.
xixEmilyxix xixEmilyxix

2021/8/1

#
I
danpost wrote...
xixEmilyxix wrote...
yeah i noticed that and changed beeinput to beevalue but i was still getting the same error
In your main menu class, you need more than just beeValue. You need beeValue of the beeInput actor in the world. Show main menu class codes.
i didnt put anything in the main menu class for the checking the value and adding an object
danpost danpost

2021/8/1

#
xixEmilyxix wrote...
i didnt put anything in the main menu class for the checking the value and adding an object
I sure looks like your displayError method is in your world class. Remove it. In beeInput class code above, insert at line 17:
if (beeValue < 3 || beeValue > 25)
{
    World world = getWorld();
    world.addObject(new errorMessagebees(), 400, 400);
}
else
{
    World world = getWorld();
    world.removeObjects(world.getObjects(errorMessagebees.class));
}
xixEmilyxix xixEmilyxix

2021/8/1

#
danpost wrote...
xixEmilyxix wrote...
i didnt put anything in the main menu class for the checking the value and adding an object
I sure looks like your displayError method is in your world class. Remove it. In beeInput class code above, insert at line 17:
if (beeValue < 3 || beeValue > 25)
{
    World world = getWorld();
    world.addObject(new errorMessagebees(), 400, 400);
}
else world.removeObjects(world.getObjects(errorMessagebees.class));
I took it out of my main menu so the only code to do with the error message is
public class beeInput extends Actor
{
    //create variable
    int beeValue;
    boolean error;
    /**
     * Act - do whatever the beeInput wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //Create input box, ask for value, set bounds for beeValue
        setImage(new GreenfootImage("Input.png"));
        //set value of bees based off the input
        if(Greenfoot.mouseClicked(this)){
            beeValue = Integer.parseInt(Greenfoot.ask("Please input a value for the amount of bees"));
        }
        
        if(beeValue < 3 || beeValue > 25)
        {
            World world = getWorld();
            World.addObject(new errorMessagebees(), 400, 400);
        }
        else world.removeObjects(world.getObjects(errorMessagebees.class));
        
    } 
}
but the line: world.addobject(new errorMessagebees(), 400, 400); is giving the error: non static method cannot be referenced from a static context and the line: else world.removeObjects(world.getObjects(errorMessagebees.class)); is giving the error: world is not public in greenfoot.actor
danpost danpost

2021/8/1

#
xixEmilyxix wrote...
but the line: world.addobject(new errorMessagebees(), 400, 400); is giving the error: non static method cannot be referenced from a static context and the line: else world.removeObjects(world.getObjects(errorMessagebees.class)); is giving the error: world is not public in greenfoot.actor
See edited lines of code above. Move line 17 to line position 25. Line 22 should begin with a lowercase 'w'.
There are more replies on the next page.
1
2