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

2013/3/16

Needing some help with text, new here

OxiClean OxiClean

2013/3/16

#
So I'm trying to display some text and then change it using this code:
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
    /**
     * Act - do whatever the Title wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        Text1();
        Text2();
    }   
     
    public void Text1()
    {
        //Displays initial message
        setImage(new GreenfootImage("Zaneth was once a peacful place...serene...pure...", 18,
                                    Color.YELLOW, Color.BLACK));
  
        Text2();
    }
     
    public void Text2()
    {
        //On clicked, switch to message part 2
        if (Greenfoot.mouseClicked(null)) {
        setImage(new GreenfootImage("But all things end, and Felix the Terrible rose to power...", 18,
                                    Color.YELLOW, Color.BLACK));
        }
    }
}
I don't quite understand why it is not working, when I click it doesn't change messages. If anyone would help me out it would be much appreciated!
danpost danpost

2013/3/16

#
What is happening is that when you click the mouse the image changes for one act cycle, then reverts back to the original image. To test this, slow the speed of the scenario down using the speed slidebar. Instead of calling the 'Text1' method from the 'act' method, call it from the 'Title' constructor:
1
2
3
4
public Title()
{
    Text1();
}
This way, it will not revert back to the original image after being changed.
OxiClean OxiClean

2013/3/16

#
Then how do I call the Title constructor? Sorry, I'm quite new at this but I appreciate your fast response!
danpost danpost

2013/3/16

#
The code in the constructor is executed when you instantiate an object of that class (when you create a new object from that class). Most times, the creation and addition of objects into the world is done in a world class that 'extends World' (in a sub-class of the World class). When you use 'new Title()', the constructor of the Title class is called. You could use the following to create a Title object and set a local field to reference that object:
1
Title title = new Title();
The act method will not run until the object is added to an active world object. The referenced object can be placed into the world with this:
1
addObject(title, 300, 200);
Once this line executes, the 'act' method in the Title class will execute repeatedly on that Title object (if the scenario is running). The same order of events will occur when using the following, which creates and adds the new Title object into the world in one statement:
1
addObject(new Title(), 300, 200);
OxiClean OxiClean

2013/3/16

#
Thanks!
danpost danpost

2013/3/16

#
You may want to check out the Java Trail on Learning the Java Language. A wealth of information is available there.
You need to login to post a reply.