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

2013/4/8

Text input with actors.

Kiara Kiara

2013/4/8

#
I am displaying text on the screen, and trying to make it so that with a specific text another actor will set another image. Please help as soon as you can!! This is the code
   StuffThatFlies stuffThatFlies;
 
    public Text(String words )
    {
        GreenfootImage img = new GreenfootImage(100, 50);
        img.drawString(words, 2, 20);
        setImage(img);
        if(words == "plane")
        {
            stuffThatFlies.setImage("airplane.png");
        }
I am getting a null pointer exception...
danpost danpost

2013/4/8

#
You have not set 'stuffThatFlies' to anything. Maybe it should be:
StuffThatFlies stuffThatFlies = new StuffThatFlies();
Kiara Kiara

2013/4/8

#
How would that set the image for StuffThatFlies to be the airplane? @danpost
danpost danpost

2013/4/8

#
That should get rid of the null pointer exception. By having the method recieve the string equal to "plane" will your code set the image to "airplane.png". You might need to change your test from if (words == "plane") to if ("plane".equals(words))
danpost danpost

2013/4/8

#
Kiara wrote...
How would that set the image for StuffThatFlies to be the airplane? @danpost
This does not change the class image to be an airplane. It changes an instance of the class to be an airplane.
Kiara Kiara

2013/4/8

#
oh, I get it!!
Kiara Kiara

2013/4/8

#
So, how do I get it to look like a plane?
danpost danpost

2013/4/8

#
You must have the image file for the airplane in the 'images' folder of the specific scenario you are working on. You can do this by right-clicking on the class icon and selecting 'Set image...'. Go to 'transportation' and select the "airplane.png" image and click OK. That will automatically move the file into the appropriate folder. You can repeat the process and select 'No image' to remove the default image for the class (the file will stay in the folder).
Kiara Kiara

2013/4/8

#
THANK YOU!! :)
You need to login to post a reply.