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

2015/1/26

Help with my Type box project

fejfo fejfo

2015/1/26

#
I just publish a senario called type box where u could make as manny thype boxes as you want by just creating an object of the class I descoverd that if you had multiples it was quid of random I witch u type . I tried a fix for it but it didn't work : this is the current ( falling ) code :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Text here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Text extends Actor
{
    int nKeysStoreable = 100;
    String[] key = new String[nKeysStoreable];
    String keys;
    int cKey = 0;

    public String toString() {
        keys = "0";
        for(int i = 0; i < nKeysStoreable; i++) {
            if(key[i] != null) {
                keys = keys+ key[i];
            }
        }
        return keys;
    }

    public int toInt() {
        return Integer.parseInt(this.toString());
    }

    public void act() 
    {
        if(Greenfoot.getMouseInfo().getActor() == this) {
            run();
        }        
    } 

    public void run() {        
        getWorld().showText(this.toString(),getX(),getY());
        String keyIn = Greenfoot.getKey();

        if(keyIn != null & keyIn != "backspace" & keyIn != "enter" & keyIn != "space" & keyIn != "shift" & keyIn != "control" & cKey < nKeysStoreable) {
            key[cKey] = keyIn;
            cKey++;
            //Greenfoot.delay(30);
        }

        if(keyIn == "backspace" && cKey > 0) {
            cKey--;
            key[cKey] = null;
            //Greenfoot.delay(30);
        }

        if(keyIn == "space") {
            key[cKey] = " ";
            cKey++;
            //Greenfoot.delay(10);
        }        
    }
}
pretti mutch the only thing I changed was
public void act() 
    {
        if(Greenfoot.getMouseInfo().getActor() == this) {
            run();
        }        
    } 
instead of
public void act() 
    {
       
         run();
              
    } 
Sam146 Sam146

2015/1/26

#
try using this
MouseInfo mouse = Greenfoot.getMouseInfo();
       
      
               if (mouse != null)
           {
                List objects = getWorld().getObjectsAt(mouse.getX(), mouse.getY(), TeamA1.class);
            for (Object object : objects)
            {
                if (object == this)
                {

}
}
}
Sam146 Sam146

2015/1/26

#
The "TeamA!.class" should be changed to the name of your class
fejfo fejfo

2015/1/26

#
It can't find the class List
danpost danpost

2015/1/26

#
@Sam146, the only way that way would work is if the mouse was located at the exact center of the actor at the time the mouse event was caught (if you were off by just 1 cell, or pixel, the text box actor will not be returned in the list -- if the list was properly returned to begin with). Your recent change calls a method ( 'getActor' ) on an MouseInfo object reference that could be null ( 'Greenfoot.getMouseInfo' ). Using the mouse may be alright to select (or change focus to) an object; but, using it to keep focus on the object is not the best way to go about it. If text boxes are the only controls that might be 'focused' on then you can place a static Text field in the Text class to hold the object that currently has the focus and check to see if it references 'this' before getting the keystrokes.
// add class field
private static Text focusedOn;
// in act
if (Greenfoot.mouseClicked(this)) focusedOn = this;
if (focusedOn == this) run();
Hopefully that will get you closer to what you want.
fejfo fejfo

2015/1/27

#
thanks for your help
You need to login to post a reply.