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

2016/3/26

get score if close to another object

ryvetra ryvetra

2016/3/26

#
i want an object(car) get a score if it close to another object ( not touching ) the size of the car = 36 x 77 andthe size of the mobil/mobil2 = 32 x 75 i already try with other cordination its really this code to use it ? or is there another method ?
private void scorePoint()
    {
        Actor actor = getOneObjectAtOffset(40, 80, Mobil.class);
        Actor actor2 = getOneObjectAtOffset(40 , 80, Mobil2.class);
        if(actor != null )
        {                         
            counter.add(1);
        }
        if(actor2 != null)
        {                         
            counter.add(2);
        }
    }
Super_Hippo Super_Hippo

2016/3/26

#
You could use the getObjectsInRange-method.
ryvetra ryvetra

2016/3/26

#
how do i use that method properly ? when i put that on actor/int variable , its incompatible type. and when i put that direct as condition at if, it compiled
ryvetra ryvetra

2016/3/26

#
right now i use this
private void scorePoint()
    {
        
        if(getObjectsInRange(10, Mobil.class) == null  )
        {                         
            counter.add(1);
        }
        if(getObjectsInRange(10, Mobil2.class) == null)
        {                         
            counter.add(2);
        }
    }
danpost danpost

2016/3/26

#
It was incompatible when you change the method used because the old method returned an Actor object while the new one returns a List object. This List object is never returned as null; it is sometimes returned as an empty list -- but never null. When you check the documentation on the 'getObjectsInRange' method of the greenfoot.Actor class, you will see that it returns a List object. If you check the documentation of the java.util.List class, you will find what methods can be called on an object of that type that may help you (specifically, 'size' or 'isEmpty').
ryvetra ryvetra

2016/3/26

#
so what should i write ? i'm still confuse..
danpost danpost

2016/3/26

#
ryvetra wrote...
so what should i write ? i'm still confuse..
Well, since the List object returned is never 'null', you cannot compare it with null as it will never be true. You can ask if its size is zero (or not zero) or you can ask if it is empty (or not empty):
if (getObjectsInRange(10, Mobil2.class).size() > 0)
// or
if (!getObjectsInRange(10, Mobil2.class).isEmpty())
Note that '10' is quite a small range -- probably smaller than the size of the image for the actor. You will probably need to increase the range to more than the larger dimension of the image of the actor.
ryvetra ryvetra

2016/3/26

#
ah thanks you , it works. but getObjectInRange return object within radius(circle) is there anymethod that get Object and return int withn size(x,y) ?
danpost danpost

2016/3/26

#
ryvetra wrote...
ah thanks you , it works. but getObjectInRange return object within radius(circle) is there anymethod that get Object and return int withn size(x,y) ?
There is a 'getNeighbours' method that you could use; however, it returns objects within a square or diamond area around the object -- not a rectangular one. I guess you could move the actor left and right and back to original location to encompass a rectangular one (getting neighbors at each place individually).
You need to login to post a reply.