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
5
6
7
JulianYoung JulianYoung

2017/1/4

#
danpost wrote...
I thought it might work. My last suggestion about Paint was not good. Paint does not have a resizing feature. Photoshop may, however. What did you use?
Paint does have a resizing option and i used that.
JulianYoung JulianYoung

2017/1/4

#
Question if i wanted to have it so that i could visit each chest three times and then disappeared how would i go about doing this?
JulianYoung JulianYoung

2017/1/4

#
well this didn't take me long to find another question. i now want the user inputs to be used as how far the character will move. i have tried using the code below but it says it cannot convert a string to a int. How could i get this to work? this is the run button
     if (Greenfoot.mouseClicked(this)) {

            Player.a=1;
            Move_Counter.moves=Move_Counter.moves+1;
            Game world = (Game)getWorld();
            
              
            Move_Sideways horizontalRangeBox = world.horizontalRangeBox;
            Move_Sideways verticalRangeBox = world.verticalRangeBox;
            System.out.println("\nHorizontal range given: '"+horizontalRangeBox.getValue()+"'\nVertical range given: '"+verticalRangeBox.getValue()+"'");

            // x and y and the value to change position of player.
          
             Player.x=horizontalRangeBox.getValue();
              Player.y=verticalRangeBox.getValue();
             

        }
this is what it affects
 for(a=a;a<=2;a++){
            setLocation (getX()+25*x,getY()+25*y);

        }
JulianYoung JulianYoung

2017/1/4

#
JulianYoung wrote...
Question if i wanted to have it so that i could visit each chest three times and then disappeared how would i go about doing this?
when i say then disappeared i mean make the chest disappeared not the person.
danpost danpost

2017/1/4

#
JulianYoung wrote...
Paint does have a resizing option and i used that.
I found it (overlooked it earlier).
it says it cannot convert a string to a int.
Use Integer.parseInt(String) to convert a number within a string to an int value. Add an int field to the Chest class to count visited times:
private int visited;
You can make use of the value of the field not only to count the times visited; but, also to determine when the player has left (by using negative values):
if (visited >= 0 && isTouching(Player.class)) visited = -(++visited);
if (visited < 0 && ! isTouching(Player.class)) visited = -visited;
if (visited == 3) getWorld().removeObject(this);
JulianYoung JulianYoung

2017/1/4

#
danpost wrote...
JulianYoung wrote...
Paint does have a resizing option and i used that.
I found it (overlooked it earlier).
it says it cannot convert a string to a int.
Use Integer.parseInt(String) to convert a number within a string to an int value. Add an int field to the Chest class to count visited times:
private int visited;
You can make use of the value of the field not only to count the times visited; but, also to determine when the player has left (by using negative values):
if (visited >= 0 && isTouching(Player.class)) visited = -(++visited);
if (visited < 0 && ! isTouching(Player.class)) visited = -visited;
if (visited == 3) getWorld().removeObject(this);
i have added your code but im wondering where i should put the code.
 Money_Counter.money= Money_Counter.money+10;
so that every time i go to that block it adds ten. however if it stayed where it was it just goes up to 100 as its touching the object for lots of time.
JulianYoung JulianYoung

2017/1/4

#
it says it cannot convert a string to a int.
Use Integer.parseInt(String) to convert a number within a string to an int value. i have tried by using it like this
  Integer.parseInt(horizontalRangeBox);
            Integer.parseInt(verticalRangeBox);
            Player.x=horizontalRangeBox.getValue();
            Player.y=verticalRangeBox.getValue();
but it says that Move_sideways(my text box) cannot be converted to java.lang.String
JulianYoung JulianYoung

2017/1/4

#
i tried changing it to
 Integer.parseInt(horizontalRangeBox.getValue);
            Integer.parseInt(verticalRangeBox.getValue);
but it says it can not find variable getValue()
danpost danpost

2017/1/4

#
JulianYoung wrote...
it says it cannot convert a string to a int.
Use Integer.parseInt(String) to convert a number within a string to an int value. i have tried by using it like this
  Integer.parseInt(horizontalRangeBox);
            Integer.parseInt(verticalRangeBox);
            Player.x=horizontalRangeBox.getValue();
            Player.y=verticalRangeBox.getValue();
but it says that Move_sideways(my text box) cannot be converted to java.lang.String
The horizontalRangeBox and verticalRangeBox objects are not String object -- they are Textbox (or, in your case, Move_Sideways) objects. Get the value from the boxes first, as String values, and then convert the strings. For example:
String horizontalText = horizontalRangeBox.getValue();
Player.x = Integer.parseInt(horizontalText);

// or just
Player.x = Integer.parseInt(horizontalRangeBox.getValue());
JulianYoung JulianYoung

2017/1/4

#
Thanks that seems to be working. however every so often this pops up java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:592) at java.lang.Integer.parseInt(Integer.java:615) at Run_Button.act(Run_Button.java:30) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) i only affects the program some times i have added the code it is calling to. Could it be to do with using negative numbers. it will still move it most of the time but it does pop the error message up.
import greenfoot.*;

/**
 * Write a description of class Run_Button here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Run_Button extends Actor
{
    /**
     * Act - do whatever the Run_Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this)) {

            Player.a=1;
            Move_Counter.moves=Move_Counter.moves+1;
            Game world = (Game)getWorld();

            Move_Sideways horizontalRangeBox = world.horizontalRangeBox;
            Move_Sideways verticalRangeBox = world.verticalRangeBox;
            System.out.println("\nHorizontal range given: '"+horizontalRangeBox.getValue()+"'\nVertical range given: '"+verticalRangeBox.getValue()+"'");

            // x and y and the value to change position of player.

            String horizontalText = horizontalRangeBox.getValue();
            Player.x = Integer.parseInt(horizontalText);
            String verticalText  = verticalRangeBox.getValue();
            Player.y = Integer.parseInt(verticalText);

        }
    }    
}
Also if you could tell me where to place my money counter so that every time it visits it goes up that would be great thanks!!!
danpost danpost

2017/1/4

#
JulianYoung wrote...
every so often this pops up java.lang.NumberFormatException: For input string: ""
I had a feeling that would be next. Unless the textbox is given valid integer input, you will get that. There are a couple of places that this can be dealt with; one is in the class of the textbox itself, controlling the value so that it will always be a valid integer. The other is when getting the value in another class. Getting the String value is no problem. You can test it for 'Integer' content with the following steps: (1) remove outside whitespace
stringValue.trim();
(2) must not be an empty string
if (! "".equals(stringValue))
(3) check first character -- must be one of " +-123456789"
if (" +-123456789".indexOf(stringValue.charAt(0)) >= 0)
(4) check remaining characters -- must be one of "0123456789"
 for (char c : stringValue.substring(1))
{
    if ("0123456789".indexOf(c) >= 0)...
If any of the checks fail, do not try to parseInt the string. You could presume the value is zero at that point:
int horizontalValue = 0;
String horiz = ((Game).getWorld()).horizontalRangeBox.getValue().trim();
if (!"".equals(horiz) && " +-123456789".indexOf(horiz.charAt(0)) >= 0 && remainingCharsOkay(horiz)) horizontalValue = Integer.parseInt(horiz);

// with the following method
private boolean remainingCharsOkay(String value)
{
    for (char c : value.substring(1)) if ("0123456789".indexOf(c) < 0) return false;
    return true;
}
You can place the code that adds to the money counter in the chest class when initially touching player.
JulianYoung JulianYoung

2017/1/4

#
how would you get it so it knows when its initially touching the person.
danpost danpost

2017/1/4

#
JulianYoung wrote...
how would you get it so it knows when its initially touching the person.
That was given above
danpost wrote...
if (visited >= 0 && isTouching(Player.class)) visited = -(++visited);
Just expand it (add the brackets, move the one line in and add the code to increase the money counter).
JulianYoung JulianYoung

2017/1/4

#
i am not sure where to put the code you have provided. Should it be at the end of the act method of my Move_sideways? I have pasted the code below if you could tell me which line it should go under that would be great!
import greenfoot.*;
import java.awt.Color;

public class Move_Sideways extends Actor
{

    private static final int MAX_INPUT_WIDTH = 20;
    private static Move_Sideways focusOn;

    private String text;

    public Move_Sideways()
    {
        text = "";
        updateImage();
    }

    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(5*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;

    }
    
}
JulianYoung JulianYoung

2017/1/4

#
I tried adding it in. But im not sure i put it in the right place. the minute i press start it instantly collects 100 coins and wins the game code below.
if (visited >= 0 && isTouching(Player.class)) visited = -(++visited);{
    Money_Counter.money=Money_Counter.money+10;
}
There are more replies on the next page.
1
2
3
4
5
6
7