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

2016/10/30

Need to use an Objects location within an Actor

Kelvin. Kelvin.

2016/10/30

#
Hello, I have a dog actor which eats the bone if he touches the bone. But I also have a dogwithglasses Actor which needs to move to the Bone if he sees it and then eat it if he touches it. Both actors move randomly and the Bone is randomly placed in the world. Ofcourse the dog with glasses needs to be able to see the bone from further away therefore I need to use the getObjectsInRange() method. But if the Bone is in range how do I get the location of the bone to use in the dogwithglasses actor? I need the location, so if the dogwithglasses can "see" the bone with getObjectsInRange I can use setLocation() to move the dog to the bone so he can eat the bone. How would you do this? dogwithglasses:
public class Dogwithglasses extends Animal
{
    private int dSteps = 0;
    public void act() 
    {
        move(5);
        dSteps++;
        life();
    }   

    public void life()
    {
        if(Greenfoot.getRandomNumber(100) > 90){
            turn(Greenfoot.getRandomNumber(90) - 45);

        }

        bark();

    }
    
    public void glasses()
    {
    
        if (getObjectsInRange(50, Bone.class))
        {
            setLocation(Bone Location);
        }
    
    }
}
Obviously "Bone location isn't valid code" Thanks -Kelvin
danpost danpost

2016/10/30

#
Kelvin. wrote...
Obviously "Bone location isn't valid code"
Neither is:
if (getObjectsInRange(50, Bone.class))
The 'if' condition must be of boolean type (true or false). The line given has a List object for the parameter. Because it is not a boolean value, some type of comparison needs to be executed to create a boolean value -- like is the size of the list not zero; or is the list not empty:
if (!getObjectsInRange(50, Bone.class).isEmpty())
If the list is not empty, then a Bone object was located within range. The range value used seems like quite a small one -- like half the distance would probably barely reach to the point where the Bone object will not intersect the dog. Anyway, now that we know that the list returned is not empty, we can get a Bone object from the list:
Actor bone = (Actor)getObjectsInRange(50, Bone.class).get(0);
Finally, now that we have a reference to the bone, we can use 'bone.getX()' and 'bone.getY()' to get the location of the bone. Using 'setLocation' seems a bit off here, as that would instantly have the dog "jump" directly to the bone and not move to it to eat it. Maybe using 'turnTowards' is a better option.
Kelvin. Kelvin.

2016/10/30

#
danpost wrote...
Neither is: ? 1 if (getObjectsInRange(50, Bone.class)) The 'if' condition must be of boolean type (true or false). The line given has a List object for the parameter. Because it is not a boolean value, some type of comparison needs to be executed to create a boolean value -- like is the size of the list not zero; or is the list not empty: ? 1 if (!getObjectsInRange(50, Bone.class).isEmpty()) If the list is not empty, then a Bone object was located within range. The range value used seems like quite a small one -- like half the distance would probably barely reach to the point where the Bone object will not intersect the dog. Anyway, now that we know that the list returned is not empty, we can get a Bone object from the list: ? 1 Actor bone = (Actor)getObjectsInRange(50, Bone.class).get(0); Finally, now that we have a reference to the bone, we can use 'bone.getX()' and 'bone.getY()' to get the location of the bone. Using 'setLocation' seems a bit off here, as that would instantly have the dog "jump" directly to the bone and not move to it to eat it. Maybe using 'turnTowards' is a better option.
Thanks for the reply, and it works. However if the bone is not in range when I start the world it gives me this error? java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at Dogwithglasses.glasses(Dogwithglasses.java:30) at Dogwithglasses.act(Dogwithglasses.java:18) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) The glasses method now looks like this:
    public void glasses()
    {   
        Actor bone = (Actor)getObjectsInRange(50, Bone.class).get(0);
        if (!getObjectsInRange(50, Bone.class).isEmpty())
        {
            turnTowards(bone.getX(), bone.getY());

        }

    }
And like I said if you place the dog with glasses already within reach of the bone before you run the scenario. It turns to the bone and eats it like it should ,but if you start with the dog out of the reach of the bone. You get the error like above and the scenario stops. Any idea how I could fix this?
danpost danpost

2016/10/30

#
Kelvin. wrote...
if the bone is not in range when I start the world it gives me this error?
You cannot expect a bone to be in the list until you are sure that the list is not empty. Cut line 3 and paste it (inserted) at line 6.
Kelvin. Kelvin.

2016/10/30

#
danpost wrote...
Kelvin. wrote...
if the bone is not in range when I start the world it gives me this error?
You cannot expect a bone to be in the list until you are sure that the list is not empty. Cut line 3 and paste it (inserted) at line 6.
You're right it works now. Thanks :3
You need to login to post a reply.