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

2017/1/2

User Input - Storage

1
2
3
4
JulianYoung JulianYoung

2017/1/2

#
Hi i am new to greenfoot and i am trying to create a game where there are two values the user needs to enter. i have taken the following code and it allows me to write in a box (credit to danpost as i found this on another discussion) static final int MAX_INPUT_WIDTH = 20; String text = "-8,8"; public Move_up() { updateImage(); text = ""; } private void updateImage() { GreenfootImage image = new GreenfootImage(15*MAX_INPUT_WIDTH, 30); image.setColor(new Color(128, 0, 0)); image.fill(); image.setColor(Color.lightGray); image.fillRect(3, 3, image.getWidth()-6, 24); GreenfootImage textImage = new GreenfootImage(text, 24, Color.black, null); image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, 15-textImage.getHeight()/2); setImage(image); } public void act() { String key = Greenfoot.getKey(); if (key == null) return; if ("enter".equals(key) && text.length() > 0) { return; } if ("backspace".equals(key) && text.length() > 0) text = text.substring(0, text.length() - 1); if ("escape".equals(key)) text = ""; if ("space".equals(key)) key = " "; if (key.length() == 1 && text.length() < MAX_INPUT_WIDTH) text += key; updateImage(); } this works but i can then not click on to my other box to change the data in that one. It is the same code. If you could help me out that would be great! also if i want to call the string at a later date how would i do that, what is it stored as. e.g. later on i change a value to the value the user has inputted.
Super_Hippo Super_Hippo

2017/1/2

#
You can only use the 'getKey' method once. I think you should have a boolean to check which box is active and then, only execute the act method if it is active.
danpost danpost

2017/1/2

#
Super_Hippo wrote...
You can only use the 'getKey' method once. I think you should have a boolean to check which box is active and then, only execute the act method if it is active.
A boolean will only allow up to two input boxes. Better would be the following (I called the class 'Textbox'):
import greenfoot.*;
import java.awt.Color;

public class Textbox extends Actor
{
    private static final int MAX_INPUT_WIDTH = 20;
    private static Textbox focusOn;
    
    private String text;
     
    public Textbox()
    {
        text = "";
        updateImage();
    }
     
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(15*MAX_INPUT_WIDTH, 30);
        image.setColor(new Color(128, 0, 0));
        image.fill();
        image.setColor(Color.lightGray);
        image.fillRect(3, 3, image.getWidth()-6, 24);
        GreenfootImage textImage = new GreenfootImage(text, 24, null, null);
        image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, 15-textImage.getHeight()/2);
        setImage(image);
    }
 
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            while(Greenfoot.getKey() != null);
            focusOn = this;
        }
        if (focusOn == this)
        {
            if (Greenfoot.mouseClicked(null) && !Greenfoot.mouseClicked(this))
            {
                focusOn = null;
                return;
            }
            String key = Greenfoot.getKey();
            if (key == null) return;
            if ("enter".equals(key) && text.length() > 0) return;
            if ("backspace".equals(key) && text.length() > 0) text = text.substring(0, text.length() - 1);
            if ("escape".equals(key)) text = "";
            if ("space".equals(key)) key = " ";
            if (key.length() == 1 && text.length() < MAX_INPUT_WIDTH) text += key;
            updateImage();
        }
    }
    
    public String getValue()
    {
        return text;
    }
}
The 'getValue' method can be used to acquire the current text for a box. Line 33 clears the keyboard buffer of extraneous keys pressed prior to the box being selected.
JulianYoung JulianYoung

2017/1/3

#
would i then just paste the code into the other box i want just changing the name of the class. Also i am trying to use System.out.printIn to display these values but am not sure how to do it. Thanks for your help
JulianYoung JulianYoung

2017/1/3

#
the main plan is the user writes the numbers they want to go up and down (-8 to 8) and left and right. They then press a button and they will move how ever far they have typed in the boxes.
Super_Hippo Super_Hippo

2017/1/3

#
danpost wrote...
A boolean will only allow up to two input boxes.
Well, I thought about setting the one which is clicked to 'true' and all others to 'false', but your way with the static variable seems to be much better. @Julian, you do not create a separate class for each Textbox object. Use this class and simply create two boxes.
JulianYoung JulianYoung

2017/1/3

#
ok thanks. So how would i output the data somewhere else? thanks
JulianYoung JulianYoung

2017/1/3

#
also i Am having difficulty getting my remove objects to work. i have the following code which should remove the actor chest instead it removes all of them do you now how i can do this. This is also affecting my other code. Where if two objects are in the same place one should be removed and replaced. Actor Chest; Chest = getOneObjectAtOffset(0, 0, Chest.class); if(Chest!=null) { World World; World = getWorld(); World.removeObject(Chest); }
danpost danpost

2017/1/4

#
JulianYoung wrote...
i have the following code which should remove the actor chest instead it removes all of them do you now how i can do this. This is also affecting my other code. Where if two objects are in the same place one should be removed and replaced. < Code Omitted >
The code given can only remove one center-touching Chest object from the world per act. Maybe you should clarify exactly what is going wrong and how it should behave when going right.
So how would i output the data somewhere else
You should retain references to the Textbox objects in your World subclass. For example:
// instance fields
private Textbox horizontalRangeBox, verticalRangeBox;

// in constructor
horizontalRangeBox = new Textbox();
addObject(horizontalRangeBox, < wherever >);
verticalRangeBox = new Textbox();
addObject(verticalRangeBox, < wherever >);

// now, you can get the values, for example, as following
System.out.println("\nHorizontal range given: '"+horizontalRangeBox.getValue()+"'\nVertical range given: '"+verticalRangeBox.getValue()+"'");
JulianYoung JulianYoung

2017/1/4

#
Thanks a lot that code is really helpfull! My character should be able to enter value to move around a grid. If he hits a chest it should realise chest has been hit add money to the money counter and remove it. there are 10 chests and you have to go round and find them avoiding bandits.
JulianYoung JulianYoung

2017/1/4

#
I have just been implementing the code you have given me but when i add this code horizontalRangeBox = new Textbox(); it says cannot find variable - horizontalRangeBox. this is in my world class and i put the first line in my text box class. Am i doing this wrong? thanks for you continued help and support.
danpost danpost

2017/1/4

#
JulianYoung wrote...
i put the first line in my text box class.
None of the code given goes in the Textbox class. It all goes in your world class.
JulianYoung JulianYoung

2017/1/4

#
I have changed the first line and put it into My world and my actor and that now seems to be working. However when i try to print out the code i get errors. If i put it in my actor class it tells me to change the first lien you gave me to have a semicolon in it but that does nothing. what am i doing wrong?
JulianYoung JulianYoung

2017/1/4

#
ok changed so it goes in world. Still not sure about where to write it out so it is displayed.
danpost danpost

2017/1/4

#
JulianYoung wrote...
ok changed so it goes in world. Still not sure about where to write it out so it is displayed.
I think that is something you will need to decide.
There are more replies on the next page.
1
2
3
4