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

2012/12/9

Global variables Incompatible types

Dytanoth Dytanoth

2012/12/9

#
Hello people, I have been trying to understand how the global variables work. I found a scenario about a rocket and shooting some asteroids, but I can't get it to work. It says Incompatible types. The idea is, that when pressing a star it will choose the difficulty of the game, then when pressed on the playbutton it will get the information from the selected star, so it will go to the level with the right difficulty. I have got a world (menu), a button (ster), and a button (playbutton). In my world, I have put the following code:
private menu difficulty;
public menu choosedifficulty(){
        return difficulty;
    }
My button has got the following code:
private void getdifficulty(){
       
        menu menuWorld=(menu) getWorld();
        playbutton difficult=menuWorld.choosedifficulty();
        difficult.difficulty("hard");
    }
And my button has:
public void difficulty(String difficulty){
        //executable code with data
    }
I've got the error Incompatible types in my buttons code at the
playbutton difficult=menuWorld.choosedifficulty();
I get the error, while it highlights the () .. I have no idea what is wrong with it, I hope someone can help me with this.
vonmeth vonmeth

2012/12/9

#
playbutton difficult=menuWorld.choosedifficulty(); This line expects a 'playbutton' class, but it returns a 'menu' class. Change:
private menu difficulty;  
public menu choosedifficulty(){  
        return difficulty;  
    } 
to
private playbutton difficulty;  
public playbutton choosedifficulty(){  
        return difficulty;  
    } 
Shouldn't give an error now, though whether it works how you want it to, I can't exactly say with the code you have given. Good luck!
Dytanoth Dytanoth

2012/12/10

#
Thanks, the incompatible type is gone now. But now I've got another problem. Whenever I try to run it, it will say: java.lang.NullPointerException at ster.getdifficulty(ster.java:87) at ster.animation(ster.java:66) at ster.clicked(ster.java:53) at ster.act(ster.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) So when I click the star, it will call this function:
private void getdifficulty(){  
         
        menu menuWorld=(menu) getWorld();  
        playbutton difficult=menuWorld.choosedifficulty();  
        difficult.difficulty("hard");  
    }  
But this gives a null value, I just don't know how and why.
nccb nccb

2012/12/10

#
Depending on which line is 87, it's either because the "ster" action has left the world earlier in act() (and thus getWorld() is returning null), or because menu.choosedifficulty() is returning null. I suspect the latter -- you have the variable "difficulty" which you are returning, but unless you assign to that variable somewhere before choosedifficulty() is called, the variable will be null, and thus choosedifficulty() will return null.
vonmeth vonmeth

2012/12/10

#
It looks like "playbutton difficulty" is never actually set to anything, so it is null.
private playbutton difficulty;    
public playbutton choosedifficulty(){    
        return difficulty;    
    }
I don't think you are coding this is the best way. You might want to try coding it in the following way: When a "star" is pressed, it gets "menu" and then passes the difficulty to it, which then stores it. Star will simply have a way to detect a mouse press, and then a method to pass difficulty information on to the menu.
private int difficulty; //this gets set to some value somewhere, probably the constructor

private void setDifficulty(){           
        menu menuWorld = (menu) getWorld();    
        menuWorld.setDifficulty(difficulty);
    }
menu will have a method something like this:
private int difficulty = 2; // default difficulty of medium

public void setDifficulty(int passedDifficulty){
           difficulty = passedDifficulty;
    }
When "playbutton" is pressed, it tells "menu" to start the game.
You need to login to post a reply.