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

2017/1/2

User Input - Storage

3
4
5
6
7
JulianYoung JulianYoung

2017/1/7

#
HI I was just wondering if you could explain how the code below works
if (visited >= 0 && isTouching(Player.class))
{
    visited = -(++visited);
    Money_Counter.money=Money_Counter.money+10;
}
i know it checks if the actor has been visited but i am not sure what the visited = -(++visited); part does.
JulianYoung JulianYoung

2017/1/7

#
Also i am having difficulty puttiing the following code into my work.
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;
}
If i try to put it into the code below i get an error message.
 */
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;

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


            // with the following method
           
            
            Player.x = Integer.parseInt(horizontalText);
            String verticalText  = verticalRangeBox.getValue();
            Player.y = Integer.parseInt(verticalText);

        }
    }    
    
}
i tried putting it in as follows
import greenfoot.*;


public class Run_Button extends Actor
{
   
    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;

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

            // String horizontalText = horizontalRangeBox.getValue();
            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);

            
            Player.x = horizontalValue;
            
            //String verticalText  = verticalRangeBox.getValue();
           // Player.y = Integer.parseInt(verticalText);

        }
    }    

    private boolean remainingCharsOkay(String value)
    {
        for (char c : value.substring(1)) if ("0123456789".indexOf(c) < 0) return false;
        return true;
    }

}
But it says illegal start of expresions and put in bold the word Game in String horiz = ((Game). help would be appreciated.
danpost danpost

2017/1/7

#
The '++' before a variable increments its value before operating with it (which we want to do to register a visit). the '-' negates that value which makes the condition of the block false until such time as the value again becomes non-negative. In all, it would be the same as the following:
if (visited >= 0 && isTouching(Player.class))
{
    visited = visited + 1; // or just 'visited++;'
    visited = -visited; // or 'visited *= -1;'
    Money_Counter.money = Money_Counter.money + 10; // or 'Money_Counter.money += 10;'
}
danpost danpost

2017/1/7

#
JulianYoung wrote...
But it says illegal start of expresions and put in bold the word Game in String horiz = ((Game). help would be appreciated.
Sorry, added an extra dot after '(Game)'. Remove it.
JulianYoung JulianYoung

2017/1/7

#
Okay thanks however still have problem when i run it (using code above) on this line i get an error
        for (char c : value.substring(1)) if ("0123456789".indexOf(c) < 0) return false;
It says. for-each not applicable to expression type required :array or java.lang.Iterable; found: java.lang.String this is when i have added it in my run section as shown above. I have removed the extra full stop as well.
danpost danpost

2017/1/7

#
JulianYoung wrote...
It says. for-each not applicable to expression type required :array or java.lang.Iterable; found: java.lang.String
Again, my apologies. Try this:
for (char c : value.substring(1).toCharArray()) ...
JulianYoung JulianYoung

2017/1/10

#
HI I was wondering if there was a way to create a IF statment using the reset button on Greenfoot (image of it below). as when i reset it does not reset some of my value. something like
if(reset pressed){
money =0;
bandits=5;
}
and so on and so forth.
Super_Hippo Super_Hippo

2017/1/10

#
Clicking on 'reset' does not re-create static variables. You probably should remove the static from these variables. If you don't want to (or there is a reason why you shouldn't do it), reset the variables in the starting world's constructor.
JulianYoung JulianYoung

2017/1/10

#
IN a different section of my world i can change the variables for my game. Up in till now i would have it when you clicked the play button it would make sure all the variables were reset but now i cant do that as they may of changed those variables. so i want that when i click reset it turns those value back to what they were back to default.
danpost danpost

2017/1/10

#
nvm, just saw your last post.
danpost danpost

2017/1/10

#
Are you saying that you want the values of these fields to be retained when reset is pressed?
JulianYoung JulianYoung

2017/1/10

#
no i want the original default setting to be reset and what the user has inputted forgotten.
danpost danpost

2017/1/10

#
Are you using 'static' fields:
static int money;
static int bandits = 5;
? (would be best if you show your codes)
JulianYoung JulianYoung

2017/1/10

#
yes
public class Game extends World
{
    public Move_Sideways horizontalRangeBox, verticalRangeBox;
    public static int d=1;
    public static int j=11;
    public static int k=6;
       public static int Grid=0;
    public static Actor chest;
JulianYoung JulianYoung

2017/1/10

#
Also i have move around some items is there a way to automatically change it to where they now are. i have heard of saving the world but the button can not be clicked on (in controls) when the program is paused or playing.
There are more replies on the next page.
3
4
5
6
7