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

2017/1/4

#
But wherever i put it i get error messages.
danpost danpost

2017/1/4

#
JulianYoung wrote...
But wherever i put it i get error messages.
You need to show your code (please use code tags) and the error message. Cannot fix without.
JulianYoung JulianYoung

2017/1/4

#
Also about my other problem when i implement my removal code for the chests in my actor as posted above Instead of checking if there is one chest beneath it and getting ridd of that it gets rid of them all. i am not sure what i am doing wrong. All the chests are randomly generated onto the grid using the following code. for(int b =1; b<11;b++){ int a = Greenfoot.getRandomNumber(8); int c = Greenfoot.getRandomNumber(8); a=(a+1)*50+75; c=(c+1)*50+75; if(Game.a==125&&Game.c==125){ a=a+50; c=c+50; } Chest chest = new Chest(); addObject(chest, a , c); }
JulianYoung JulianYoung

2017/1/4

#
This is one such place i get the error. This is the button that when pressed eventuall will make the person move the amounts that the user have entered in the two boxes. however i get the error cannot find symbol variable horizontalRangeBox */ 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; System.out.println("\nHorizontal range given: '"+horizontalRangeBox.getValue()+"'\nVertical range given: '"+verticalRangeBox.getValue()+"'"); } } } Also being new to greenfoot and coding how do you mean code tags
danpost danpost

2017/1/4

#
JulianYoung wrote...
when i implement my removal code for the chests in my actor as posted above Instead of checking if there is one chest beneath it and getting ridd of that it gets rid of them all. i am not sure what i am doing wrong. All the chests are randomly generated onto the grid using the following code. < Code Omitted >
It is nice to know that your chests are scattered randomly around your world and that it is possible for multiple chests to at the same location. However, that has little to do with having them removed.
danpost danpost

2017/1/4

#
JulianYoung wrote...
how do you mean code tags
There is a link below the reply box, 'Posting code? read this!' takes you to instructions on using code tags.
JulianYoung JulianYoung

2017/1/4

#
danpost wrote...
JulianYoung wrote...
when i implement my removal code for the chests in my actor as posted above Instead of checking if there is one chest beneath it and getting ridd of that it gets rid of them all. i am not sure what i am doing wrong. All the chests are randomly generated onto the grid using the following code. < Code Omitted >
It is nice to know that your chests are scattered randomly around your world and that it is possible for multiple chests to at the same location. However, that has little to do with having them removed.
I Know i sound like a novice but you have hit another point. I was wondering what code i could use so that when two chests are spawned in the same place one chest is deleted and it will remove one from int b wich controls the for loop. Sadly as you can see i am having problems with character detection and removal.
JulianYoung JulianYoung

2017/1/4

#
OK understand Code Tags Now.
danpost danpost

2017/1/4

#
The error is because the field is in your World subclass (which is fine) and not in the Run_Button class. To accesss the fields, you would do this:
// in your Run_Button class (with a World subclass called MyWorld)
MyWorld world = (MyWorld)getWorld();
Textbox horizontalRangeBox = world.horizontalRangeBox;
Textbox verticalRangeBox = world.verticalRangeBox;
Add something like this before trying to access the values.
JulianYoung JulianYoung

2017/1/4

#
I have added this code but get the error Horrizontalrangebox has private access in Game(name of world)
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;

        }
    }    
}
I was not sure if changing the code in Game to Public would help but i tried and it didn't work.
danpost danpost

2017/1/4

#
JulianYoung wrote...
I was wondering what code i could use so that when two chests are spawned in the same place one chest is deleted and it will remove one from int b wich controls the for loop. Sadly as you can see i am having problems with character detection and removal.
Try replacing these lines:
Chest chest = new Chest();
addObject(chest, a , c);
with this:
if (getObjectsAt(a, c, Chest.class).isEmpty())
{
    Chest chest = new Chest();
    addObject(chest, a, c);
}
else b--;
danpost danpost

2017/1/4

#
JulianYoung wrote...
I was not sure if changing the code in Game to Public would help but i tried and it didn't work.
If you changed it to 'public', it should (maybe you had the wrong case).
JulianYoung JulianYoung

2017/1/4

#
i am posting the whole code for my Game World below. When i implemented you suggestion above there were no errors but the Greenfoot will not compile. it says its taking a long time to restart and may have an infinite loop.
import greenfoot.*;

/**
 * Write a description of class Game here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game extends World
{
    private Move_Sideways horizontalRangeBox, verticalRangeBox;
    public static int a =1;
    public static int b=1;
    public static int c=1;
    public static int d=1;
    public static int e=1;
    public static int f=1;
    public static Actor chest;
    /**
     * Constructor for objects of class Game.
     * 
     */
    public Game()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(500, 500, 1); 
        Player player = new Player();
        addObject(player, 125,125 );

        Chest_counter ccounter = new Chest_counter();
        addObject(ccounter, 45,150 );

        Money_Counter MOcounter = new Money_Counter();
        addObject(MOcounter, 45,250 );

        Move_Counter Mcounter = new Move_Counter();
        addObject(Mcounter, 45,300 );        

        Bandit_counter bcounter = new Bandit_counter();

        Up_Down up = new Up_Down();
        addObject(up, 133,75 );
        Sideways side = new Sideways();
        addObject(side, 133,25 );
        addObject(bcounter, 45,200 );

        Run_Button rbutton = new Run_Button();
        addObject(rbutton, 375,50 );

        Run run = new Run();
        addObject(run, 450,50 );

        for(int b =1; b<11;b++){
            int a = Greenfoot.getRandomNumber(8);
            int c = Greenfoot.getRandomNumber(8);
            a=(a+1)*50+75;
            c=(c+1)*50+75;
            // starting place for player is 125,125
            if(a==125&&c==125){
                a=a+50;
                c=c+50;

            }
            if (getObjectsAt(a, c, Chest.class).isEmpty())
            {
                Chest chest = new Chest();
                addObject(chest, a, c);
            }
            else b--;                                                           
        }

        for(int d =1; d<6;d++){
            int e= Greenfoot.getRandomNumber(8);
            int f = Greenfoot.getRandomNumber(8);
            e=(e+1)*50+75;
            f=(f+1)*50+75;
            if(e==125&&f==125){
                e=e+50;
                f=f+50;

            }
            if (getObjectsAt(e, f, Bandit.class).isEmpty())
            {
                Bandit bandit = new Bandit();
                addObject(bandit, e, f);
            }
            else d--;                                                           
        }
    

    horizontalRangeBox = new Move_Sideways();
    addObject(horizontalRangeBox,250,75);
    verticalRangeBox = new 
Move_Sideways();

    addObject(verticalRangeBox,250,25);

}
}

JulianYoung JulianYoung

2017/1/4

#
danpost wrote...
JulianYoung wrote...
I was not sure if changing the code in Game to Public would help but i tried and it didn't work.
If you changed it to 'public', it should (maybe you had the wrong case).
Changed it and seems to be working now thanks. not sure why it didn't work when i changed it earlier.
danpost danpost

2017/1/4

#
First, remove lines 12 through 18. I do not know why you would want to retain any of those values or a specific chest object reference. Then, make line 11 'public' instead of 'private'. I do not see any reason why it would take long to construct the world as far as the code in the Game class. Did you change anything in one of the Actor subclass constructors (or code that is executed when the actors are created)?
There are more replies on the next page.
1
2
3
4
5