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

2014/6/24

getOneIntersectingObject with the World

Hutanoi Hutanoi

2014/6/24

#
Hey men, another question of mine: I want to check if there is an Object in the World. If there is one of this Objects (called "Explode2") in the World i want it to print out the score. But the code this.getOneIntersectingObject(Explode2.class) does not work. (for a World) How can i see if my world intersects with an object??? Thanks for helping :)
danpost danpost

2014/6/24

#
The 'getOneIntersectingObject' method is of the Actor class and can only be called of an Actor object (you cannot call it on a World object). To see if an Explode2 object is in the world, use:
1
if (!getObjects(Explode2.class).isEmpty())
which says 'if the list of Explode2 actors in the world, returned using 'getObjects', is NOT empty, then ...'. The API documentations can be consulted to see what methods can be used on what objects, as well as what type of value or Object is returned when a method is called
Hutanoi Hutanoi

2014/6/25

#
I think there is a mistake in the code if (!getObjects(Explode2.class).isEmpty()) If i use this code it does not work, but the code getObjects(Explode2.class) works... Is it possible that you made a mistake with the "!" or the "isEmpty" ?
Hutanoi Hutanoi

2014/6/25

#
If i use the code if (getObjects(Explode2.class) != null ) it also doesnt work... Then it says that there is an "Explode2" in the World, even if it isnt.
danpost danpost

2014/6/25

#
Hutanoi wrote...
I think there is a mistake in the code < Details Omitted > Is it possible that you made a mistake with the "!" or the "isEmpty" ?
The code appears correct. What error message are you getting (or how does it not work)?
Hutanoi Hutanoi

2014/6/26

#
it doesnt work, if there is an "Explode2" it should print out the score, but it doesnt do anything when the "Explode2" appears (as if there would be no method) btw, i have the method in the act-method
danpost danpost

2014/6/26

#
Please show the code of the class (or at least the act method and the method that seems to not work). Also, use the code' tag below the 'Post a reply' box to insert your code into your post.
Hutanoi Hutanoi

2014/6/26

#
Okay thanks This is my Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int ein = 0;
 
    public void endscore()
    {      
 
        if (getObjects(Explode2.class) != null)
        {
            if (ein != 1)
            {
                scoreZ.setString("Gespielte Zeit: " + zeit/57 + "s");
                scoreP.setString("Erreichte Punktezahl: " + raumschiff.getPunkte());
                ein =1;
            }
        }
    }
then he prints out the new values for "zeit/57" and "raumschiff.getPunkte()" at the beginning, even if there is no object of the "Explode2" class. The second option (with your code) is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int ein = 0;
 
    public void endscore()
    {      
 
        if (!getObjects(Explode2.class).isEmpty()) 
        {
            if (ein != 1)
            {
                scoreZ.setString("Gespielte Zeit: " + zeit/57 + "s");
                scoreP.setString("Erreichte Punktezahl: " + raumschiff.getPunkte());
                ein =1;
            }
        }
    }
With this code he doesnt print out anything (not even the "Gespielte Zeit:" and "Erreichte Punktezahl:").
Hutanoi Hutanoi

2014/6/26

#
the "setString()"-method works sure reliably
danpost danpost

2014/6/26

#
I think you need to track the 'ein' field. If you place the following line (inserted) at line 8, you will probably see that it passes the first test (line 6), but fails the second (line 8):
1
System.out.println("Explode2 object detected");
like this:
1
2
3
4
5
6
if (!getObjects(Explode2.class).isEmpty()) 
{
    System.out.println("Explode2 object detected");
    if (ein != 1)
    {
        // etc.
Hutanoi Hutanoi

2014/6/26

#
okay, it works now, dont know why it didnt before nevertheless thanks :D
You need to login to post a reply.