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

2012/11/10

How can I make a Textbox correctly?

1
2
Yellowfoot Yellowfoot

2012/11/10

#
Hey guys, now I have another problem with Pacman :( I want to make a Textbox to show the core, but it doesn't work. I declared this variable:
public String myTextbox;
Then I wrote this Method:
 public void writeText(String myText)
    {
        GreenfootImage image = getImage();
        image.clear();
        image.setColor(Color.BLACK);
        image.drawString(meinText, 10, 10);

    }
Then I wrote this in the method "prepare()" (This is a method I wrote to design the world):
myTextbox = new Score();
        addObject(myTextbox, 4, 0);
        myTextbox.writeText("Hello");
And now this message appears when I try to compile this: cannot find symbol- method getImage() I hope you can help me again! :)
erdelf erdelf

2012/11/10

#
where is the method writeText?
SPower SPower

2012/11/10

#
You're not showing the line where the error is, please tell that. And if it's here:
GreenfootImage image = getImage();
and that code in in the world subclass, that's because the getImage method isn't available in world. You have to use getBackground. And to make it even simpler for you, you can download my 'Save it' scenario, which has a text field class (it also has one for passwords, protected text field I think it's called)
erdelf erdelf

2012/11/10

#
I dont think that he needs a writable textfield.
danpost danpost

2012/11/10

#
'getImage' is a method of the Actor class. You must apply it to an actor, not the world. That is, as written, it can only be used in a sub-class of Actor. Also, you cannot assign an actor object (which I believe Score to be) to a String object (which 'myTextbox' is declared to be).
Yellowfoot Yellowfoot

2012/11/10

#
Hey and thank you again for your fast answer.
SPower wrote...
You're not showing the line where the error is, please tell that.
It was the line you showed me, but getBackground didnt work too. I tried to make a Textfield with your "Save-it"-scenario but i failed. :( "cannot find symbol - class TextFieldResponder" appears. I just want a Textfield with a score (Numbers) in it which is saved in a "score"-variable (Sub-class "Pacman" of Actor).
I dont think that he needs a writable textfield.
You're right.
SPower SPower

2012/11/10

#
If you copy the TextField class, you also need to copy the TextFieldResponder class (I forgot that). It's at the bottom of the list of classes. And I misunderstood you, as erdelf said you don't need a text field like I thought you needed. For what you need, you can either use the Counter class from Greenfoot itself (go to the import menu and select the Counter class) or go to my library scenario and copy the class Text, but change it's superclass to Actor like this:
// before
extends SuperActor
// after
extends Actor
Yellowfoot Yellowfoot

2012/11/10

#
Thank you for your nice answer again :) I tried to do it with the Counter class, but I have some questions now: In the subclass "Pacman" of actor I declared a variable named "score". This variable save a new score in every act. How can I make Counter show the variable value in every act? Hope you can help me.
SPower SPower

2012/11/10

#
Add this to the act method of counter (at the beginning)
Actor a = getObject(Pacman.class);
if (a != null) {
    setValue( ( (Pacman) a).score);
}
Yellowfoot Yellowfoot

2012/11/10

#
Hmm, doesn't work.
 Actor a = getObject(cPacman.class);  
        if (a != null) {  
            setValue( ( (cPacman) a).score);  
        }  
(The Pacman-class is named "cPacman". I changed this) Error in line 1: "cannot find symbol - method getObject(java.lang.Class<cPacman>)
SPower SPower

2012/11/10

#
Sorry, change it to
getWorld.getObject(...
Yellowfoot Yellowfoot

2012/11/10

#
Now the error "cannot find variable getWorld" appears :/
erdelf erdelf

2012/11/10

#
change it to
getWorld()
I think SPower forgot this
danpost danpost

2012/11/10

#
Try
cPacman pacman = (cPacman) getObjects(cPacman.class).get(0);
Yellowfoot Yellowfoot

2012/11/10

#
Hmm, it seems that it don't works too...
Actor a = getWorld().getObject(cPacman.class);  
Error: "cannot find symbol - method getObject(java.lang.Class<cPacman>)
Actor a = getWorld(cPacman.class);  
Error: "method getWorld in class greenfoot.Actor cannot be applied to given types; required: no arguments; found: java.lang.Class<cPacman>; reason: actual and formal argument lists differ in length Does someone have another idea? :/ EDIT:
danpost wrote...
Try
cPacman pacman = (cPacman) getObjects(cPacman.class).get(0);
I will try.. :) regards
There are more replies on the next page.
1
2