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

2011/8/12

Trouble with updating the GreenfootImage

1
2
kiarocks kiarocks

2011/8/12

#
i cannot figure out what is wrong with my image! Here is the code:
public void act()  
    {  
        
        String myKey = Greenfoot.getKey();  
        if (myKey == null) { return; }  
        String myText = "";  
        if (myKey == "space") { myKey = " "; } // If spaces are not wanted, change 'myKey = " ";' to 'return;'  
        if (myKey == "backspace")  
        {  
            // Code for when 'backspace' is pressed  
            if (txtString.length() > 0)  
            {  
                txtString = txtString.substring(0, txtString.length() - 1);  
            }  
            
            return;  
            
        }  
        if (myKey == "enter")  
        {  
            // Code for when 'enter' is pressed  
            myKey = "\n";

        }  
        // You can continue for other keys: arrow keys, function keys, 'tab' and 'escape' (and any others I may have missed)  
        
            // Change the concatenation in the following statement to only wanted characters  
        String goodChars = LETTERS + letters + Numbers + Symbols + " " + "\n";  
        int myIndex = goodChars.indexOf(myKey.charAt(0));  
        if (myIndex > -1)  
        {  
            myText = goodChars.substring(myIndex, myIndex + 1);  
            txtString = txtString + myText;  
        }  
        GreenfootImage image = new GreenfootImage(txtString, 15, Color.BLACK, Color.WHITE);
        setImage(image);
        
    }
and here is the problem- When i press the backspace key, it successfully works- but not until you type again. The image is constantly set, what is the problem?
supermagicmilk supermagicmilk

2011/8/12

#
Not 100% sure, but I don't think you can do myKey == "some String" Usually you use myKey.compareTo("some String") == 0 //zero means the strings match Here's the documentation for it: http://download.oracle.com/javase/6/docs/api/java/lang/String.html#compareTo%28java.lang.String%29
kiarocks kiarocks

2011/8/12

#
you can, there is a
if (myKey == null) { return; }  
part that allows it.
davmac davmac

2011/8/12

#
The problem is that you return; without updating the image first, in this part of the code:
        if (myKey == "backspace")  
        {  
            // Code for when 'backspace' is pressed  
            if (txtString.length() > 0)  
            {  
                txtString = txtString.substring(0, txtString.length() - 1);  
            }  
            
            return;  
            
        }  
davmac davmac

2011/8/12

#
Also, supermagicmilk was correct, you shouldn't normally use '==' to compare strings, instead use .equals():
        if (myKey.equals("backspace"))
        {  
            ....
kiarocks kiarocks

2011/8/13

#
kiarocks wrote...
you can, there is a
if (myKey == null) { return; }  
part that allows it.
notice it is fine here.
danpost danpost

2011/8/13

#
@kiarocks, add this method:
private void setNewImage()
{
GreenfootImage image = new GreenfootImage(txtString, 15, Color.BLACK, Color.WHITE);
setImage(image);
}
and call it before the 'return statement', and you can call it at the end of your act() method as well, instead of executing those same statements there. In other words, you can put 'setNewImage();' on line 15 and also replace lines 35 & 36 with the same.
kiarocks kiarocks

2011/8/13

#
ok, ill see if it works.
kiarocks kiarocks

2011/8/13

#
i have a non static method called clicked:
  public boolean clicked()
    {
        if(Greenfoot.mouseClicked(this))
        return true;
        else
        return false;
    } 
and how do i get it here without error?
if (s.startsWith("Save"))
                {

                    if (s.length() > 0)
                        click(1);
                }
                if(s.startsWith("Load"))
                {
                    if (s.length() > 0)
                        click(2);
                }
danpost danpost

2011/8/13

#
I am not sure where the second code listing is located or what context it is in. If the second code listing is calling the 'clicked()' method, then your 'click(1);' and 'click(2);' should probably be 'clicked(1);' and 'clicked(2);'. The main code in 'clicked()' can be reduced to just one statement -- 'return Greenfoot.mouseClicked(this);' which makes having the method at all pretty much not needed as the code can be placed in the main code, instead of calling a separate method. The thing is -- 'Greenfoot.mouseClicked()' IS a method that returns a boolean value already; there is no need to create a method that does the same.
kiarocks kiarocks

2011/8/13

#
sorry, those two are in different classes. here is code for second part from original.
s = s.substring(s.indexOf("/") + 1);
                if (s.startsWith("Save"))
                {

                    if (s.length() > 0)
                        SaveButton.clicked();
                }
                if(s.startsWith("Load"))
                {
                    if (s.length() > 0)
                        LoadButton.clicked();
                }
danpost danpost

2011/8/14

#
Seems to me like the internal 'if's that check to see if the length of 's' is greater than zero (0) are not needed since we already know that it either starts with 'Save' or 'Load', and therefore has a length greater than zero, unless your method 'startsWith(String)' does not do what its name implies. Anyways, it seems to me that there still is not enough information given for anyone to help at this point. 1) Are these in different Actor classes, or is one in the world class; and if so, which one? 2) What are you trying to accomplish, or what is the code going to do? 3) Is there a 'Button' class, and two buttons added to the world, one for 'Load', and one for 'Save'? (I would guess so) Let me run an idea by you; you decide if it worth persuing. 1) Add to 'World' class a String variable
public static String lastButton = "";
2) Add to act() in 'Button' class
if (Greenfoot.mousePressed(this) { World.lastButton = "button name"; }
I would guess your 'Button' class has an instance String variable that stores its text or name. 3) In whatever class, add
if (!"".equals(World.lastButton)) // if 'World' class, omit 'World.'
{
 // determine button and act code goes here
// ...
World.lastButton = "";
}
danpost danpost

2011/8/14

#
Only consider the above if you are actually acting on the mouse-press of a button in your world. In step (3), line (3) would start with something like 'if ("Save".equals(World.lastButton)) { ...'
kiarocks kiarocks

2011/8/14

#
ok, there is two classes for the buttons under the textmaker class. /Users/edtogami/Desktop/Screen shot 2011-08-13 at 7.57.49 PM.png the text world is trying to access the buttons. this is going to save/load what you type-Already accomplished. As you can see, no is the answer to #3 it will act on mouse press. Would
Greenfoot.mouseClicked(LoadButton.class);
work?
kiarocks kiarocks

2011/8/14

#
sorry about image.
There are more replies on the next page.
1
2