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

2015/4/27

Problem with "getObjectsInRange"

1
2
xWanna xWanna

2015/4/27

#
Hey, Like the title says, I have a problem with "getObjectsInRange". I have a door and you should be able to open it if you are in the range of 40 Pixels. But I can open it from the whole world.
 boolean keydown = false;
    boolean geschlossen = true;
/***
     *Öffnet das Tor wenn "e" gedrückt wird
     * 
     */public void öffnen(){
        if(!Greenfoot.isKeyDown("e") && keydown){

            keydown = false;
        }

        
        if(getObjectsInRange(40, Spieler.class) != null && Greenfoot.isKeyDown("e") && !keydown)
        {
            if(geschlossen)
            {
                GreenfootImage img = getImage();
                setImage("Wände/Tor.Seite.png");  
                geschlossen = false;
                ausrichten();
            }else{
                GreenfootImage im = getImage();
                setImage("Wände/Tor.png");

                geschlossen = true;
                ausrichten();

            }
            keydown = true;
        }
    }

    /**
     * Richtet das Tor passend aus
     * 
     */
    public void ausrichten(){
        if(!geschlossen){
            int x = getX();  // Nimmt die X-Koordinate auf
            int y = getY();// Nimmt die Y-Koordinate auf
            setLocation(x + 35, y - 30);

        }else{
            int x = getX();  // Nimmt die X-Koordinate auf
            int y = getY();// Nimmt die Y-Koordinate auf
            setLocation(x - 35, y + 30);

        }

    }
Thanks for your help guys. xWanna
danpost danpost

2015/4/27

#
Questions: (1) what is the 'super' call in your world constructor? (2) do you have any classes that extend the Spieler class?
xWanna xWanna

2015/4/27

#
My super super(800, 600, 1, true); and I have one class that extends the Spieler.class
danpost danpost

2015/4/27

#
Objects of the class that extends Spieler are also considered Spieler objects. So, maybe that is why you are able to open the door from anywhere.
xWanna xWanna

2015/4/27

#
I removed the extending class for Tor.class and it still doesn't work.
danpost danpost

2015/4/27

#
xWanna wrote...
I removed the extending class for Tor.class and it still doesn't work.
Is the Tor class the class that was extending Spieler?
xWanna xWanna

2015/4/27

#
Ah no, my fault. The Charakter1.class extends the Spieler.class. But I seperated them and it's still not working and I changed the code so it says
if(getObjectsInRange(40, Charakter1.class) != null && Greenfoot.isKeyDown("e") && !keydown)
        {
            if(geschlossen)
            {
                GreenfootImage img = getImage();
                setImage("Wände/Tor.Seite.png");  
                geschlossen = false;
                ausrichten();
            }else{
                GreenfootImage im = getImage();
                setImage("Wände/Tor.png");
 
                geschlossen = true;
                ausrichten();
 
            }
            keydown = true;
        }
The Charakter1 is the character you are playing with and it is supposed to interact with Tor.class. Charakter1.class:
public static int X1 = 0;
    public void getX1(){
        X1 = getX();
    }
    public static int Y1 = 0;
    public void getY1(){
        Y1 = getY();
    }
    int Blick=0;
    int X;
    int Y;
    /**
     * Sorgt dafür, dass es möglich ist die Figur bewegen zu können.
     * Zeigt je nach Richtung verschiedene Bilder an
     * 
     * 
     */
    public void bewegen() {
        int x = getX();  // Nimmt die X-Koordinate auf
        int y = getY();  // Nimmt die Y-Koordinate auf
        X = x;
        Y = y;
      

        if (Greenfoot.isKeyDown("right") && getOneObjectAtOffset(img.getWidth()/2, 0, Wände.class)==null && getOneObjectAtOffset(img.getWidth()/2, 0, Tor.class)==null && getOneObjectAtOffset(img.getWidth()/2, 0, Fenster.class)==null){
            setLocation(x + 4, y);
            setImage("CharRechts.png");

            //setImage("Schlagen/Rechts/Rechts.png");
            Blick = 3; 
        }else if (Greenfoot.isKeyDown("left") && getOneObjectAtOffset(img.getWidth()/-2, 0, Wände.class)==null  && getOneObjectAtOffset(img.getWidth()/-2, 0, Tor.class)==null && getOneObjectAtOffset(img.getWidth()/-2, 0, Fenster.class)==null){
            setLocation(x - 4, y);
            setImage("CharLinks.png");

            //setImage("Schlagen/Links/Links.png");
            Blick=4;
        }else if (Greenfoot.isKeyDown("up") && getOneObjectAtOffset(0, img.getHeight()/-2, Wände.class)==null  && getOneObjectAtOffset(0, img.getHeight()/-2, Tor.class)==null && getOneObjectAtOffset(0, img.getHeight()/-2, Fenster.class)==null){
            setLocation(x, y - 4);
            setImage("CharHinten.png");

            //setImage("Schlagen/Hinten/Hinten.png");
            Blick=1;
        }else if (Greenfoot.isKeyDown("down") && getOneObjectAtOffset(0, img.getHeight()/2, Wände.class)==null  && getOneObjectAtOffset(0, img.getHeight()/2, Tor.class)==null && getOneObjectAtOffset(0, img.getHeight()/2, Fenster.class)==null){
            setLocation(x, y + 4);
            setImage("CharFront.png");

            //setImage("Schlagen/Vorne/Vorne.png");
            Blick=2;
        }
    }
danpost danpost

2015/4/27

#
Do you have any class that extends the Charakter1 class? Are you creating more than one Charakter1 instance (actor object)? Double-check this by right clicking on world when paused and selecting the World class method 'getObjects' and entering 'Charakter1.class' in the inputbox for the class. The pop-up results box should show a List object. Double click on it (or single click and click the 'Inspect' button) to see the size of the list. It should only be one (1).
xWanna xWanna

2015/4/27

#
It shows this error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
danpost danpost

2015/4/27

#
Run the project; do not open the door; pause the project. Then inspect. Make sure you enter the class correctly -- it should be: Charakter1.class
xWanna xWanna

2015/4/27

#
I did it, also tried it with other classes, but always the same error.
danpost danpost

2015/4/27

#
Do you get the error when entering this?-- null
xWanna xWanna

2015/4/27

#
Yes
danpost danpost

2015/4/27

#
Try again after closing out greenfoot totally and re-opening it.
xWanna xWanna

2015/4/27

#
Same error again :(
There are more replies on the next page.
1
2