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

2023/11/24

How can I modify an int value from a different class?

alexn256 alexn256

2023/11/24

#
I am trying to make it so that I can change the value of a static integer, diff, I made in MyWorld class in an Actor class when I click my mouse on the Actor. Here is the code in the act method of my Actor class:
if(Greenfoot.mouseClicked(this)){
            ((MyWorld)getWorld()).diff = 1;
            System.out.println("diff = 1");
            getWorld().removeObjects(getWorld().getObjects(Medium.class));
            getWorld().removeObjects(getWorld().getObjects(Hard.class));
            getWorld().removeObjects(getWorld().getObjects(ChooseYourDifficulty.class));
            World world;
            world = getWorld();
            world.removeObject(this);
        }
it is printing the "diff = 1" but ((MyWorld)getWorld()).diff = 1; is not working. in my MyWorld class:
private int myLevel;
    private int[][] map;
    static int diff;
    //int x;
    //int y;
    private Tadpole myTadpole;
    private Frog myFrog;
    public MyWorld()
    {    
        super(750, 750, 1);
        setPaintOrder(Titlescreen.class, ChooseYourDifficulty.class, Score.class, Rocket.class, Fly.class, Fly2.class, Car.class, Ambulance.class, Crocodile.class, Crocodile2.class, Rock.class, Frog.class, Log.class, Tadpole.class, Lilypad.class);
        Titlescreen titlescreen = new Titlescreen();
        addObject(titlescreen, getWidth()/2, getHeight()/2);
        ChooseYourDifficulty chooseYourDifficulty = new ChooseYourDifficulty();
        addObject(chooseYourDifficulty, getWidth()/2, getHeight()/2);
        Easy easy = new Easy();
        addObject(easy, 110, 400);
        Hard hard = new Hard();
        addObject(hard, 630, 400);
        Medium medium = new Medium();
        addObject(medium, 380, 400);
        //mouseClicked();
        if(diff == 1){
            System.out.println("diff ====1");
            myLevel = 0;
            myTadpole = new Tadpole();
            myFrog = new Frog();
            map = level1;
            loadLevel();
            prepare();
        }
        if(diff == 2){
            myLevel = 1;
            myTadpole = new Tadpole();
            myFrog = new Frog();
            map = level2;
            loadLevel();
            prepare();
        }
        if(diff == 3){
            myLevel = 2;
            myTadpole = new Tadpole();
            myFrog = new Frog();
            map = level3;
            loadLevel();
            prepare();
        }
    }
danpost danpost

2023/11/24

#
alexn256 wrote...
I am trying to make it so that I can change the value of a static integer, diff, I made in MyWorld class in an Actor class when I click my mouse on the Actor. << Code Omitted >>]
Line 2 in the clicking block of code would be better written as:
MyWorld.diff = 1;
You may need to make the static int on line 2 public to be seen by the actor. If it continues to reset to some other value then the problem is elsewhere in your project. Everything from line 22 and on needs to be placed where it can execute more than once, when the world was created (the value of diff would be zero and no action would be taken by that one checking). Use an act method in your World subclass.
jherper25 jherper25

2023/11/25

#
:)
You need to login to post a reply.