I am working on a dialoguebox with a working "next" button that changes the line of text being shown, everytime it's clicked. In the "Nextbutton" class, i have managed to make a variable where if you click the next button, a counter goes up. The problem arises when i try to bring that variable from my 'Nextbutton' class over to 'Dialoguebox' class. I already made a string containing the script. When i try to start the game it gave me a "java.lang.NullPointerException", so i did some experimenting the world class but i can't wrap my head around it, can someone please help me?
code Nextbutton:
code Dialoguebox:
code world:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | public class Nextbutton extends Scriptje { public boolean mouseDown; public int currentline; private GreenfootImage image; public boolean lineChanged = false ; /** * Act - do whatever the Nextbutton wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Nextbutton() { mouseDown = false ; verkleinButton(); image = getImage(); } public void act() { if (!mouseDown && Greenfoot.mouseClicked( this )) { currentline++; getWorld().showText( "currentline: " + currentline, 200 , 100 ); //verwijder dit zodra dat het script werkt lineChanged = true ; } } private void verkleinButton() { GreenfootImage img = new GreenfootImage( "nextbutton.png" ); img.scale(img.getWidth()/ 10 , img.getHeight()/ 10 ); setImage(img); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class Dialoogbox extends Scriptje { public String[] lines={ "line 1" , "line 2" , "line 3?" , "line 4" }; private int followscript; public boolean scriptChanged = false ; /** * Act - do whatever the Dialoogbox wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ // dialoogbox (klasse van knopje) public Dialoogbox() { followscript = getWorld().getObjects(Nextbutton. class ).get( 0 ).currentline; scriptChanged = getWorld().getObjects(Nextbutton. class ).get( 0 ).lineChanged; } public void script(){ if (!scriptChanged) { GreenfootImage image = new GreenfootImage(lines[followscript], 24 , Color.BLACK, new Color( 255 , 255 , 255 )); } } public void act() { } } |
1 2 3 4 5 6 7 8 9 10 | public MyWorld() { super ( 600 , 400 , 1 ); prepare(); setPaintOrder(Nextbutton. class , Dialoogbox. class , Verkoper. class , Klant. class ); setBackground( "wereldkaart.jpg" ); toonTekst( "Kies het continent dat je wilt bezoeken:" , getWidth() / 2 , getHeight() / 4 ); Nextbutton knopje = new Nextbutton(); Dialoogbox boxje = new Dialoogbox(knopje); } |