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

2020/2/24

need help

Starlord_20 Starlord_20

2020/2/24

#
public class MyKaraIO extends KaraIO {
    int pos1,pos2;
    /**
     * In the 'act()' method you can write your program for Kara <br>
     * <i>In der Methode 'act()' koennen die Befehle fuer Kara programmiert werden</i>
     */
    public void act() {
        int pos = 0,x = 0; String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
        for(int i = 0;i < 26;i++) {
            char ltd = alphabet.charAt(pos);
            Map<Character,Letter> letterMap = new HashMap<Character,Letter>();  letterMap.put(ltd, new Letter(pos, ltd));
            getWorld().addObject(letterMap.get(ltd),x,3); pos ++; x ++;
        }
        Greenfoot.delay(1);
        pos1 = alphabet.indexOf(JOptionPane.showInputDialog("Bitte einen Buchstaben von A-Z!").toUpperCase()); pos2 = (int)(26*Math.random()); 
        if(pos1 < pos2) {
            getWorld().showText("zu klein!",5,5);
            for(int i = 0;i <= pos1;i++) {
                Letter ltd = (Letter) getWorld().getObjectsAt(i, 3, Letter.class);
                getWorld().removeObject(ltd);
            }
        } else if(pos1 > pos2) {
             getWorld().showText("zu Groß!",5,5);
            for(int i = 25;i >= pos1;i--) {
                Letter ltd = (Letter) getWorld().getObjectsAt(i, 3, Letter.class);
                getWorld().removeObject(ltd);
            }
        }
    }

}
public class Letter extends Actor
{

    int i;
    public Letter (int alphabet, char letter) {
        i = alphabet; setImage(new GreenfootImage(""+letter,25 ,Color.BLACK, new Color(0,0,0,0)));
    }
}
I want to remove the letters that are out of the question, but it does`nt work...
danpost danpost

2020/2/24

#
You probably have a gazillion letters there all on top of each other. Your for loop to add new letters into the world is in the act method without any conditions being placed on its execution.
Starlord_20 Starlord_20

2020/2/24

#
No that part works fine, by adding 1 to int x, they're all next to each other. My problem lies in the part that should remove the letters from the world.For example I want to say, that if the input letter is c and the random letter is d, the letters a and b are removed.
danpost danpost

2020/2/24

#
Starlord_20 wrote...
No that part works fine, by adding 1 to int x, they're all next to each other. My problem lies in the part that should remove the letters from the world.For example I want to say, that if the input letter is c and the random letter is d, the letters a and b are removed.
Try this: pause the scenario and use the mouse to drag a letter away from its place.
Starlord_20 Starlord_20

2020/2/25

#
no my world has only a width of 26 pixels. I only need help with the delete of the actors. Maybe if I could expand the declaration beyond the for loop...Could that work?
danpost danpost

2020/2/25

#
Starlord_20 wrote...
no my world has only a width of 26 pixels. I only need help with the delete of the actors. Maybe if I could expand the declaration beyond the for loop...Could that work?
Still, try dragging a letter to where a different letter is at after running and pausing (or after clicking on the Act button a few times).
Starlord_20 Starlord_20

2020/2/25

#
if i do, it summons again after clicking act()
danpost danpost

2020/2/26

#
Starlord_20 wrote...
if i do, it summons again after clicking act()
-- whether you do or not.
You need to login to post a reply.