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

2012/2/25

"Life is too short..." Using getObjectsInRange()

Omniscience Omniscience

2012/2/25

#
So, here I am again, but this time I've been experimenting with getObjectsInRange(). I have 2 classes, one is a car and the other a truck. If the truck comes into the proximity of the car, I need to swap the image of the Lives class from one showing 3 hearts to one showing 2 hearts. I managed to get this far in my head: java.Util.List Collision = getObjectsInRange(20,Truck); if (Collision.size() == 1) { Actor n = new Actor;...not sure how you write that part! n = Lives.class; n.setImage("2 Lives.png"); Any help as to how I can set the image of the "Lives" class from within "Car" class under the given conditions? Any help would be great, as always.
danpost danpost

2012/2/25

#
Do you mean this?
if (!getObjectsInRange(20, Truck.class).isEmpty())
{
    Lives lives = (Lives) (getWorld().getObjects(Lives.class).get(0));
    lives.setImage("2Lives.png");
}
You do not want to create another Lives instance. You want to change the image of the one that is already in the world.
Omniscience Omniscience

2012/2/25

#
Exactly... and the code you've written... would it change the Lives image of that which is already in the world? If so, could you explain to me how it works? Cheers danpost!
danpost danpost

2012/2/25

#
Line 1: if (!getObjectsInRange(20, Truck.class).isEmpty()) the method: getObjectInRange(20, Truck.class) creates a list of all Truck.class objects in the world that are within a radius of 20 grids of the current object; the method: isEmpty() creates a true/false object, returning true if the list is empty, else false; the qualifier: ! reverses the true/false state; In total: If the list of trucks within 20 of 'this' is not empty, then... Line 3: Lives lives = (Lives) (getWorld().getObjects(Lives.class).get(0)); the method: getWorld() return the world that 'this' resides in; the method: getObjects(Lives.class) creates a list of all Lives.class objects in the world; the method: get(0) returns the first item from the list of all Lives.class objects in the world; the cast: (Lives) casts that first item from the list as an instance of the Lives.class; the variable name: lives will refer to that newly casted Lives object; the type declaration: Lives tells the compiler that the variable name will hold an instance of a Lives.class object. Line 4: lives.setImage("2Lives.png"); the method: setImage("2Lives.png") sets the image of the object to that which is stored in the file "2Lives.png"; the object reference: lives as described above.
Omniscience Omniscience

2012/2/25

#
Wonderful explanation danpost! I've implemented this into my code and it works perfectly. I'm now modifying it to change from the images of 2 lives to 1 life accordingly. Will post back to you if it goes horribly wrong!
Omniscience Omniscience

2012/2/25

#
Okay, so I had a play with your code to make it work for more "Lives" images:
GreenfootImage a = new GreenfootImage("Full Lives.png");
        GreenfootImage b = new GreenfootImage("2 Lives.png");
        GreenfootImage c = new GreenfootImage("Last Life.png");
        if (!getObjectsInRange(70, Truck.class).isEmpty() && getY() == ORIGINY)
        {
            Lives lives = (Lives) (getWorld().getObjects(Lives.class).get(0));
            if (lives.getImage() == a) {
                lives.setImage("2 Lives.png");
            }
            if (lives.getImage() == b) {
                lives.setImage("Last Life.png");
            }
            if (lives.getImage() == c) {
                getWorld().removeObject(this); 
                getWorld().removeObject(lives);
            }
        }
However, it doesn't seem to switch from image to image any more... what do you recommend I do?
danpost danpost

2012/2/26

#
My suggestion is to set up the images in the Lives.class (instead of here), using an one-dimensional array as an instance variable of the Lives.class with an integer image tracker as follows:
private GreenfootImage[] image = { new GreenfootImage("Full Lives.png"),
                                                                     new GreenfootImage("2 Lives.png"),
                                                                     new GreenfootImage("Last Life.png") };
private int imageNumber = 0;
Then, in the constructor:
public Lives()
{
    setImage(image[imageNumber]);
}
And finally, in the Lives.class, create the following method:
public void nextImage()
{
    imageNumber++;
    if (imageNumber == 3)
    {
        getWorld().removeObject(this);
        return;
    }
    setImage(image[imageNumber]);
}
Now, with the code I originally provided, change line 4 (lives.setImage("2 Lives.png");) to
lives.nextImage();
if (lives.getWorld() == null) getWorld().removeObject(this);
I added the last statement to remove the Car object when the Lives object is removed from the world.
Omniscience Omniscience

2012/2/26

#
Thanks! Works great... plus I understood the logic behind it! I'm gonna try and create a Game Over dialogue Box, which will appear when the Car.class no longer exists in the world. Will post feedback! Thanks again danpost!
You need to login to post a reply.