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

2011/7/5

Greenfoot.isKeyDown(any) ???

1
2
3
kiarocks kiarocks

2011/7/21

#
am i missing some thing?
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
52
53
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class poi here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class poi extends Actor
{
 
        public final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        public final String letters = "abcdefghijklmnopqrstuvwxyz"
        public final String Numbers = "0123456789"
        public final String Symbols = "`~!@#$%^&*()_+-=[]{}|;':<>?,./"
        public String txtString = ""
 
        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 
                return
            
            // 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 + " "
            int myIndex = goodChars.indexOf(myKey.charAt(0)); 
            if (myIndex > -1
            
                myText = goodChars.substring(myIndex, myIndex + 1); 
                txtString = txtString + myText; 
            
            new GreenfootImage(70, 60);
           // wrong method?
drawString(txtString, 60, 50);
            setImage(txtString);
        }
    }   
mjrb4 mjrb4

2011/7/21

#
What do you want it to do and what is it doing? Either way at the moment you're creating a new GreenfootImage (line 48) and not storing it anywhere - that's most likely a bug.
mjrb4 mjrb4

2011/7/21

#
mjrb4, is it possible you answered a question at the end of the first page of the discussion without realising the discussion already flowed on to another page? :)
Apologies, yes that's indeed what happened! Brain not in gear... :-)
kiarocks kiarocks

2011/7/21

#
how would you store it? and im making an image based on what you type
mjrb4 mjrb4

2011/7/21

#
Something like:
1
2
3
GreenfootImage image = new GreenfootImage(70,60);
image.drawString(txtString, 10, 10);
setImage(image);
At present you're trying to set the image to a string, which isn't an image (the method signature for setImage says it wants a GreenfootImage.) So thus it won't work, you need to paint the text to the image first.
kiarocks kiarocks

2011/7/21

#
does the delete key on mac work with greenfoot like the backspace on windows?
manster2008 manster2008

2011/7/24

#
I have another question that I would like answered: Can a string be separated into characters or an array, or etc.? I want to make this thing where if you enter a string in the programming, when it runs, it figures out what part of the string extends over the end of the image and moves on to a new line. Thanks for responses.
kiarocks kiarocks

2011/7/24

#
how could you make two or more lines of text? And why does the code not delete the letter until i type again?
kiarocks kiarocks

2011/7/24

#
oops, posted without looking again.__.
danpost danpost

2011/7/24

#
@kiarocks, was 'oops' a 'nvm' or was there a question you needed answered? Also, try to be more specific when asking questions. Tells us exactly what is happening, and in what order (not only on the screen, but when you do something as well). We would be better armed to answer your questions appropriately (and you would probably be more satisfied with the responses, as well as the time factor in getting the proper responses).
kiarocks kiarocks

2011/7/24

#
was a nvm, and when i press delete key(mac) it deletes, but it not noticeable until i start typing again. And there is no way to make two lines of text with my current code for the return key.
mjrb4 mjrb4

2011/7/24

#
@manster2008 - You might be better starting a new thread so as not to get caught up in this one. As a quick answer though you can separate a string into an array of chracters with the toCharArray() method.
akilino akilino

2015/4/6

#
What about the delete key? I want to add a condition for the "delete" key, and it doesn't work.
danpost danpost

2015/4/6

#
akilino wrote...
What about the delete key? I want to add a condition for the "delete" key, and it doesn't work.
No. 'pgup', 'pgdn', 'home', 'end', 'del' and 'ins' are not keys that are recognizable through greenfoot. You could, if you wanted, detect and act upon one of the function keys, 'F1' through 'F12', (or any other keys) to function as any of those keys.
You need to login to post a reply.
1
2
3