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

2012/11/25

Textfield

Sl3nDeRm4n Sl3nDeRm4n

2012/11/25

#
Hi, i have a problem. I tried to add an text field wich checks the tiped code if click at the screen. And opens a World wich fits with that typed code. But my programm opens one world every time? How can i do it?
danpost danpost

2012/11/25

#
I am lost with what you are trying to do. Could you elaborate and maybe show some code?
danpost danpost

2012/11/25

#
Are you saying that you have several objects of the same class that show different texts; and for each one, when clicked on, a different world should open, but the same one is opening every time?
Sl3nDeRm4n Sl3nDeRm4n

2012/11/26

#
import greenfoot.*;
import java.awt.Color;

/**
 * An actor for text input in Greenfoot.
 * 
 * @author Sven van Nigtevecht
 * @version 1.0
 */
public class TextField extends Actor
{
    private GreenfootImage beginImage;
    private String text;
    
    /**
     * Create a new text field with a width of 400.
     */
    public TextField()
    {
        this(400);
    }
    
    /**
     * Create a new text field with a given width.
     */
    public TextField(int width)
    {
        GreenfootImage img = new GreenfootImage(width, 50);
        img.setColor(Color.WHITE);
        img.fill();
        img.setColor(Color.WHITE);
        img.fillRect(3,3, img.getWidth() -6, img.getHeight() -6);
        img.setTransparency(175);
        setImage(img);
        this.beginImage = img;
        
        this.text = "";
    }
    
    /**
     * Sets the prefix of the text field (the text you see before
     * you typed anything).
     */
    
    /**
     * Sets the responder of the text field.
     */
    /**
     * Returns the responder of the text field.
     */
    
    /**
     * Returns the text of the text field.
     */
    public String getText()
    {
        if (this.text != null) return this.text;
        return "";
    }
    
    /**
     * Returns the image of the text field, without the text.
     */
    public GreenfootImage getBeginImage()
    {
        return this.beginImage;
    }
    
    /**
     * Add text onto the text field if needed.
     */
    public void act() 
    {
        addInput();
        
        if(Greenfoot.isKeyDown("n")){
            if(getText() == "Xp3sTv57J"){
                Greenfoot.setWorld(new World2L0());
            }else{
                     if(getText() == "KbZQ37x"){
                         Greenfoot.setWorld(new World2L1());
                    }else{
                        if(getText() == "MPS5291"){
                            Greenfoot.setWorld(new World2L2());
                        }else{
                            if(getText() == "JsX31pQr"){
                                Greenfoot.setWorld(new World2L3());
                            }else{
                                if(getText() == "IIMJPZx1"){
                                    Greenfoot.setWorld(new World3L0());
                                }else{
                                    if(getText() == "KKJxMpQr2"){
                                        Greenfoot.setWorld(new World3L1());
                                    }else{
                                        if(getText() == "15K3P9XwA"){
                                            Greenfoot.setWorld(new World3L2());
                                        }else{
                                            if(getText() == "B3uVTsM5"){
                                                Greenfoot.setWorld(new World3L3());
                                            }else{
                                                Greenfoot.setWorld(new World1());
                                            }                
                                        }
                                    }
                                }
                            }
                        }
                    }
            
        }
    }
}
    private void addInput()
    {
        String key = Greenfoot.getKey();
        if (key == null) return;        
            if (key.equals("backspace")){
                if (text.length() > 0) {
                    text = text.substring(0, text.length() -1);
                }
             else {
                if(key.equals("space")){
                    key = " ";
                }
                if (key.equals("shift") ||
                    key.equals("control") ||
                    key.equals("tab") ||
                    key.equals("escape") ||
                    (char)127 == key.charAt(0)) {
                        key = "";
                }
                text = text + key;
            }
        }
        refresh();
    }
    
    /**
     * Redraw the image of the text field.
     */
    public void refresh()
    {
        
            GreenfootImage img = new GreenfootImage(beginImage);
            GreenfootImage txtImg = new GreenfootImage(this.text, 40, null, null);
            int x = 10;
            int y = img.getHeight() /2 - txtImg.getHeight() /2;
            img.drawImage(txtImg, x,y);
            setImage(img);
        
    }
    
    
}
thats my code.
danpost danpost

2012/11/26

#
There are several problems with your 'addInput' method. (1) 'shift' and 'control' cannot be captured with 'getKey'; (2) there is a Java bug with the 'tab' key being captured with both 'getKey' and 'isKeyDown' (3) all the code between lines 118 and 133 is executed only when the keystroke was the 'backspace' key (you have a bracket out of place)
danpost danpost

2012/11/26

#
In your 'act' method, you need to make the following change for each string comparison: FROM: if (getText() == "string") TO: if("string".equals(getText())) as the input string is not the same instance as the literal string.
davmac davmac

2012/11/26

#
Also, you can use "else if":
            if(getText().equals("Xp3sTv57J")) {
                Greenfoot.setWorld(new World2L0());  
            } else if(getText().equals("KbZQ37x")) {
                 Greenfoot.setWorld(new World2L1());  
            } else if(getText().equals("MPS5291")) {
                 // etc
Sl3nDeRm4n Sl3nDeRm4n

2012/11/26

#
thank you
You need to login to post a reply.