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

2022/3/10

Hello, i need help with my dialog class.

Focused Focused

2022/3/10

#
import greenfoot.*;
import java.awt.Color;
 
public class Dialog extends Actor
{
    private GreenfootImage baseImage, textImg;
    private int textspeed;
    private GreenfootImage scroll = new GreenfootImage("scroll.png");
    private String text[];
    
    public Dialog(String text[], int size)

    {
        scroll.scale(544,64);
        baseImage = new GreenfootImage(544,64);
        
        baseImage.drawImage(scroll,0,0);
        setText(text, size);
        updateImage();
    }
 
    
    public void setText(String text[], int size)
    {
        int fontsize = size;
        for(int i = 0; i<text.length;i++){
        GreenfootImage stringpic = new GreenfootImage(text[i], fontsize, greenfoot.Color.BLACK, null);
        Font textFont = new Font ("Calibri", fontsize);
        stringpic.setFont(textFont);
        textImg = new GreenfootImage(stringpic.getWidth(), 50);
        textImg.drawImage(stringpic, 0, 40-stringpic.getHeight());
        textspeed = baseImage.getWidth(); 
        while(!Greenfoot.isKeyDown("space")){
            Greenfoot.delay(1);
        }
    }
  }
 
    
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(baseImage);
        image.drawImage(textImg, textspeed, 0);
        setImage(image);
    }
 
    public void act()
    {
        textspeed--;
        if (textspeed ==-textImg.getWidth()) textspeed = baseImage.getWidth();
        updateImage();
        
    }
}
Hello, this is my class of a scrolling dialog. My goal is that the Class displays a scrolling dialog text on its image. The text proceeds after pressing a key. The text is stored in an array. However the world doesnt load, i think it is because of the while. Maby i also messed something else up.
RcCookie RcCookie

2022/3/11

#
The while loop waits until the space key get's pressed. You should normally not use "Greenfoot.delay()", because it freezes the whole scenario and the world will not be rendered in the meantime. You should rather use a counter or a boolean flag in the act loop and check every frame.
Focused Focused

2022/3/11

#
RcCookie wrote...
The while loop waits until the space key get's pressed. You should normally not use "Greenfoot.delay()", because it freezes the whole scenario and the world will not be rendered in the meantime. You should rather use a counter or a boolean flag in the act loop and check every frame.
First of all thanks for your advice! I tried to implement what you said, and the first String of the array did load with the world. But after the KeyInput, where it shall switch to the next, it crashes with a Null Pointer Exception. What did i do wrong? java.lang.NullPointerException at Dialog.act(Dialog.java:66) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.NullPointerException at Dialog.act(Dialog.java:66)
import greenfoot.*;
import java.awt.Color;
  
public class Dialog extends Actor
{
    private GreenfootImage baseImage, textImg;
    private int textspeed;
    private GreenfootImage scroll = new GreenfootImage("scroll.png");
    private String text[];
     int counter = 0;
     boolean changed;
     
    public Dialog(String text[], int size)
 
    {
        scroll.scale(544,64);
        baseImage = new GreenfootImage(544,64);
        String text0 = text[0];
        baseImage.drawImage(scroll,0,0);
        setText(text0, size);
        updateImage();
    }
  
     
    public void setText(String text, int size)
    {
        int fontsize = size;
        
        GreenfootImage stringpic = new GreenfootImage(text, fontsize, greenfoot.Color.BLACK, null);
        Font textFont = new Font ("Calibri", fontsize);
        stringpic.setFont(textFont);
        textImg = new GreenfootImage(stringpic.getWidth(), 50);
        textImg.drawImage(stringpic, 0, 40-stringpic.getHeight());
        textspeed = baseImage.getWidth(); 
        if(changed == true){
            
        }
    }
    
  
  
     
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(baseImage);
        image.drawImage(textImg, textspeed, 0);
        setImage(image);
    }
    
    public void act()
    {
    
        textspeed--;
        if (textspeed ==-textImg.getWidth()) textspeed = baseImage.getWidth();
        updateImage();
        if(Greenfoot.isKeyDown("space")){
            //changed = true;
            /**
            for(  counter = 0; counter<text.length;counter++){
                String aktText = text[counter];
                setText(aktText,18);
        }
        }
        **/
        counter = counter+1;
        if(counter<text.length){
            
            setText(text[counter],18);
        }
        }
    }
}


Super_Hippo Super_Hippo

2022/3/12

#
Is the “text” variable set to anything anywhere?
Focused Focused

2022/3/12

#
The array text variable is set by the constructor. The String text ist just a parameter of the setText method. So i use it just when i use the method.
RcCookie RcCookie

2022/3/12

#
No, it’s not. You have a parameter „text“, but you don’t assign the variables or to that parameter. Add the line
this.text = text;
Ingo the constructor.
Focused Focused

2022/3/12

#
Thanks RcCookie and Super_Hippo for the help. I appreciate it. It works perfectly now! Firstly, just the first and second string of the dialog showed up. So I changed the isKeyDown method to getKey. Also i checked the length of array, so no arrayoutofBoundserror. The dialog gets removed from the world after last string. That text String works also now. It works perfectly fine now!
You need to login to post a reply.