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

2014/6/10

I don't know why this doesn't work....

Meinersjee Meinersjee

2014/6/10

#
This is the code, but it doesn't work. When you press q and there is an actor on 0,2 he should turn gray but he makes it red, so he should follow if but instead he follows else.
 public void removeFromWorld()  
{  
        
    if ("q".equals(Greenfoot.getKey()))
        {   
         if (this.getX() == 0 && this.getY() == 2)
        {
            setImage(new GreenfootImage("grijs.png"));
            getWorld().addObject(new blokje(), Greenfoot.getRandomNumber(4), 0 );
            setLocation(getX(), getY()+1);
            Greenfoot.playSound("POP.mp3");
        }
        else {
            getWorld().addObject(new Rood(), 0, 2 );
            Greenfoot.stop();
            Greenfoot.playSound("Lose.mp3");
        }
        
    }
danpost danpost

2014/6/10

#
Change the method to the following (I only added some terminal print lines to the code):
public void removeFromWorld()  
{  
    if ("q".equals(Greenfoot.getKey()))
    {
        System.out.println("q keystroke detected");
        if (getX() == 0 && getY() == 2)
        {
            System.out.println("actor at (0, 2)");
            setImage(new GreenfootImage("grijs.png"));
            getWorld().addObject(new blokje(), Greenfoot.getRandomNumber(4), 0 );
            setLocation(getX(), getY()+1);
            Greenfoot.playSound("POP.mp3");
        }
        else
        {
            System.out.println("no actor at (0, 2)");
            getWorld().addObject(new Rood(), 0, 2 );
            Greenfoot.stop();
            Greenfoot.playSound("Lose.mp3");
        }
    }
}
and report back what gets printed.
Meinersjee Meinersjee

2014/6/17

#
He still only does the else and reports this: q keystroke detected no actor at (0, 2)
Meinersjee Meinersjee

2014/6/17

#
This method doesn't see the actor on (0, 2) And I can not find why this is happening, my teacher could not find it either.
danpost danpost

2014/6/17

#
The problem could be somewhere else within the class.
Meinersjee Meinersjee

2014/6/17

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class blokje extends Actor
{
    
    public void act() 
    {
       removeFromWorld();
    }      
public void removeFromWorld()    
{    
    if ("q".equals(Greenfoot.getKey()))  
    {  
        System.out.println("q keystroke detected");  
        if (getX() == 0 && getY() == 2)  
        {  
            System.out.println("actor at (0, 2)");  
            setImage(new GreenfootImage("grijs.png"));  
            getWorld().addObject(new blokje(), Greenfoot.getRandomNumber(4), 0 );  
            setLocation(getX(), getY()+1);  
            Greenfoot.playSound("POP.mp3");  
        }  
        else  
        {  
            System.out.println("no actor at (0, 2)");  
            getWorld().addObject(new Rood(), 0, 2 );  
            Greenfoot.stop();  
            Greenfoot.playSound("Lose.mp3");  
        }  
    }  

    
       else if ("w".equals(Greenfoot.getKey())) 
    {   

        if (getX() == 1 && getY() == 2)
        {
            setImage(new GreenfootImage("grijs.png"));
            getWorld().addObject(new blokje(), Greenfoot.getRandomNumber(4), 0 );
            setLocation(getX(), getY()+1);  
            Greenfoot.playSound("POP.mp3");
        } 
        else {
            getWorld().addObject(new Rood(), 1, 2 );
            Greenfoot.stop();
            Greenfoot.playSound("Lose.mp3");
        }
        
    }
       else if ("e".equals(Greenfoot.getKey())) 
    {   

        if (getX() == 2 && getY() == 2)
        {
            setImage(new GreenfootImage("grijs.png"));
            getWorld().addObject(new blokje(), Greenfoot.getRandomNumber(4), 0 );
            setLocation(getX(), getY()+1);
            Greenfoot.playSound("POP.mp3");
        } 
        else {
            getWorld().addObject(new Rood(), 2, 2 );
            Greenfoot.stop();
            Greenfoot.playSound("Lose.mp3");
        }
        
    }
       else if ("r".equals(Greenfoot.getKey())) 
    {   

        if (getX() == 3 && getY() == 2)
        {
            setImage(new GreenfootImage("grijs.png"));
            getWorld().addObject(new blokje(), Greenfoot.getRandomNumber(4), 0 );
            setLocation(getX(), getY()+1);
            Greenfoot.playSound("POP.mp3");
        } 
        else {
            getWorld().addObject(new Rood(), 3, 2 );
            Greenfoot.stop();
            Greenfoot.playSound("Lose.mp3");
        }
        
    }

}
}
This is my whole code.
danpost danpost

2014/6/17

#
One big problem is your multiple use of 'Greenfoot.getKey' within the same act cycle. This method acts like a buffer, in that once a key is returned, the pointer moves to the next position for the next key. In other words, it may return a key when checking with "q", but when checking with "w", "e" and "r", it will always return a 'null' value. What you can do is save the key returned in a local String variable:
String key = Greenfoot.getKey();
if ("q".equals(key)) // q block
else if ("w".equals(key)) // w block ... etc.
This may or may not fix your issue (it depends on whether the issue was described accurately or not -- in this case, it may fix it, if it was not accurately described).
danpost danpost

2014/6/17

#
Since all four keys do basically the same thing (at their specified locations), I would simplify the code to something like this:
int x = "qwer".indexOf(Greenfoot.getKey());
if (x == -1) return;
System.out.println(key+" keystroke detected");
if (getX() == x && getY() == 2)  
{  
    System.out.println("actor at ("+x+", 2)");  
    setImage(new GreenfootImage("grijs.png"));  
    getWorld().addObject(new blokje(), Greenfoot.getRandomNumber(4), 0 );  
    setLocation(getX(), getY()+1);  
    Greenfoot.playSound("POP.mp3");  
}  
else  
{  
    System.out.println("no actor at ("+x+", 2)");  
    getWorld().addObject(new Rood(), x, 2 );  
    Greenfoot.stop();  
    Greenfoot.playSound("Lose.mp3");  
}
Meinersjee Meinersjee

2014/6/23

#
Thanks for the help but it still doesn't work. I just can't figure out why....
danpost danpost

2014/6/23

#
Please show your updated code. Also, the problem may be somewhere else; not in this class. Are you using 'getKey' in a different class as well?
Meinersjee Meinersjee

2014/6/23

#
He doesn't recognize 'key' I deleted the update because it didn't work. I don't even know how to use that last one, how could you give qwer another if (getX() == x && getY() == 2)?
Meinersjee Meinersjee

2014/6/23

#
But we are going to set it back to the mouse. We have to finish it this week :)
danpost danpost

2014/6/23

#
It should work as good as your original. Line 1: sets 'x' to 0 for 'q', 1 for 'w', 2 for 'e' and 3 for 'r'. If none of these keys are detected, then it sets x to -1. Line 2: exits the method if none of the four keys were detected. Line 4: same as lines 15, 36, 53, and 70 of your code above. The rest: same as your blocks for the different keys. I just used the value of 'x' instead of hard-coding '0', '1', '2' and '3' where needed. This should not alter the way the code works in any way and probably will not fix your problem (which I presume resides elsewhere in your code). Maybe you should post your world class code (the problem may be in it -- again, I cannot be sure, however).
You need to login to post a reply.