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

2014/8/20

getting value of text

1
2
3
davemib123 davemib123

2014/8/20

#
I am trying to change the value of the text only if a specific string is shown and the enter key is pressed. This is what I have so far in my world class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//variable in world class
public Text inGameText;
 
//inside constructor
inGameText = new Text("");
 
// method in world class
 public Text getText()
    {
        return inGameText;
    }
 
// initial text added to world
addObject(inGameText, 147, 289);             
inGameText.setText(inGameText.palletTown_Sign1);
This is what I've attempted from my text class act method:
1
2
3
4
5
CreateMap C = (CreateMap)getWorld();
        Text inGameText = C.getText();
        if (Greenfoot.isKeyDown("enter") && C.getText() == palletTown_Sign1)
        {
        }
Where am I going wrong with it?
NikZ NikZ

2014/8/20

#
What exactly are you doing in your Text class?
davemib123 davemib123

2014/8/20

#
Text class basically just displaying text strings:
1
2
3
4
5
public String sign1 = "this is sign 1"
public String sign2 = "this is sign 2"
public String intro1= "this is intro part 1"
public String intro2= "this is intro part 2"
public String intro3= "this is intro part 3"
I just want the text to change when intro1 is displayed and enter key is pressed.
danpost danpost

2014/8/20

#
Where are you declaring these String fields (in what class)? Where and how are you declaring the palletTown_Sign1 field? Are you going to have at most only one Text object ever in this project?
davemib123 davemib123

2014/8/20

#
danpost wrote...
Where are you declaring these String fields (in what class)? Where and how are you declaring the palletTown_Sign1 field? Are you going to have at most only one Text object ever in this project?
Q1 Response: Text.class Q2 Response: public String palletTown_Sign1 = "this is pallet town sign 1" Q3 Response: no text will be different based on signs and people
NikZ NikZ

2014/8/20

#
Where in your code are you changing the text?
danpost danpost

2014/8/20

#
On Q3: maybe you misunderstood, or maybe there is a problem. Let me ask that a different way. Is 'inGameText' the only object you will be creating from the Text class (what text it might change to is irrelevant)?
davemib123 davemib123

2014/8/20

#
yea misunderstood. inGameText is the only object created.
danpost danpost

2014/8/20

#
Please post the code for the 'setText' method of the Text class.
davemib123 davemib123

2014/8/20

#
1
2
3
4
public void setText(String text)
    {
        updateImage(text);
    }
danpost danpost

2014/8/20

#
Ok, how about the 'updateImage' method, then.
davemib123 davemib123

2014/8/20

#
1
2
3
4
5
6
private void updateImage(String text)
   {
       Color txtColor = new Color(96, 96, 96);
       Color bgColor = new Color(0, 0, 0, 0);
       setImage(new GreenfootImage(text, 20, txtColor, bgColor));
   }
danpost danpost

2014/8/20

#
Ok. Your Text object is not retaining the text it is displaying. So, how do you expect to know which text is showing? You need an instance String field added to the class to hold the current text:
1
2
3
4
5
6
7
8
9
10
11
12
// add instance field in Text class
private String currentText;
// add this line to your 'updateImage' method
currentText = text;
// change your act method to
public void act()
{
    if (Greenfoot.isKeyDown("enter") && currentText == palletTown_Sign1)
    {
        setText(intro1);
    }
}
Looking back at what you previously had, this:
1
if (Greenfoot.isKeyDown("enter") && C.getText() == palletTown_Sign1)
would never be true as 'C.getText' returns a Text object and 'palletTown_Sign1' is a String object.
davemib123 davemib123

2014/8/21

#
great. Thanks for clearing that up.
davemib123 davemib123

2014/8/21

#
following on from this I am trying to add rectangles to appear on the screen, using this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//text class act method
public void act()
    {
        CreateMap C = (CreateMap)getWorld();  
        if (getWorld().getObjects(Rectangle.class).size() == 0
        
            delay --;
        }
        if (delay <= 0 && Greenfoot.isKeyDown("enter") && currentText == palletTown_Sign2) 
        
            C.addRectangles();
            setText(palletTown_Sign2a); 
        }
       }
rectangles class:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
 
/**
 * Write a description of class Misc here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Rectangle extends Tiles
{
    int timer;
    private int delay = 50;
    public Rectangle(int countDown)
    {
        GreenfootImage Rectangle = new GreenfootImage(8, 20);
        Rectangle.setColor(new Color(0, 0, 0)); 
        Rectangle.fillRect(0, 0, 8, 20);
        setImage(Rectangle);
        Rectangle.setTransparency(255);
        this.speed = 10;
        timer = countDown;
    }
 
    /**
     * Act - do whatever the Misc wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        timer--;
        if (timer <= 0
        {
            fadeOut();
        }
        if (getWorld().getObjects(Rectangle.class).size() > 0 && Greenfoot.isKeyDown("enter"))
        {
            getWorld().removeObject(this);
        }
    }   
 
    public void fadeOut()
    {
        GreenfootImage Transition = getImage();
        Transition.setTransparency(Transition.getTransparency() - speed);
        if(Transition.getTransparency() <= 5)
        {
            getWorld().removeObject(this);
        }
    }
}
and world class method:
1
2
3
4
5
6
7
8
9
public void addRectangles()
   {
       int multipler = 400;
       for(int i=29 ; i<425 ; i+=8) {
           addObject(new Rectangle(i), i, 288);
           addObject(new Rectangle(i+(1*multipler)), i, 308);
           addObject(new Rectangle(i+(2*multipler)), i, 328);
       }
   }
if I run the
1
if (delay <= 0 && Greenfoot.isKeyDown("enter") && currentText == palletTown_Sign2)
from the act method without:
1
setText(palletTown_Sign2a); 
the rectangles work fine. But as soon as I add the text in the rectangles just flash and then switch off. Any ideas where I've gone wrong with it?
There are more replies on the next page.
1
2
3