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

2016/5/27

Array Problem

Rabe123 Rabe123

2016/5/27

#
Hello guys, i have start to programming with Grennfoot and i have a Problem with my command. I would like to make an command that looks if there are buses in radius. When its so the cars should stop (speed ==0).
Super_Hippo Super_Hippo

2016/5/27

#
How is this an array problem?
if (getObjectsInRange(50 /*radius to check*/, Bus.class).isEmpty()) //if there is no bus
{
    //do something... move, turn and so on
}
moretinshop moretinshop

2016/5/27

#
Read up on this: https://www.greenfoot.org/files/javadoc/greenfoot/Actor.html#getObjectsInRange(int, java.lang.Class) Here is a working code example
public class Car extends Actor
{
    private int speed = 1;

    public void act() 
    {
        if(isBusInRange())
            speed = 0;
        else
            speed = 1;
    }
    
    private boolean isBusInRange(){
        return !getObjectsInRange(40, Bus.class).isEmpty(); //finds buses within a 40 pixel radius
    }
}
Rabe123 Rabe123

2016/5/27

#
Thans for you replys. I have already a code: public void touchingBus(){ for(int i=0;i<cars.length;i++){ if (cars != null){ if(!cars.getObjectsInRange(50,Bus.class).isEmpty()){ cars.nul(); // set speed = 0 } } } } My Problem is that it dont work.
Super_Hippo Super_Hippo

2016/5/27

#
Okay... try to explain what you think each line does.
danpost danpost

2016/5/27

#
First thing I see is that you are getting the length of 'car' in the for conditional, yet then ask if it is null in the following line. An array that has a length is never null. This means that the condition 'if cars != null' can be removed. Next thing is that you are trying to call a method on an array (the inner-most line: 'cars.nul()' ). You cannot call any methods on an array -- whether you write them or not. About the only thing you can do is refer to its length as in the for conditional. You should include information like what class the given code is in (and what class that class extends, repeatedly, up to Actor or World or Object). The code for the 'nul' method should also be given since a call to that method is included in the given code. Also, the declaration of the 'cars' array and how you fill that array with values (the code that do these things).
Rabe123 Rabe123

2016/5/27

#
public void touchingBus(){ for(int i=0;i<cars.length;i++){ //looks how long it is if (cars != null){ // looks if in the array are more then 0 cars if(!cars.getObjectsInRange(50,Bus.class).isEmpty()){ //then the cars in the array look if there are buses. cars.nul(); // if there are buses they should drive 0 (nul is alreday difined) } } } } so right ?
Rabe123 Rabe123

2016/5/27

#
 public car[]cars= new car[17];
Can i make a other method thats passes through the array and say then when a car (out the array) have an bus in the radius that the car should stop ?
danpost danpost

2016/5/27

#
Okay. The line you gave to initialize the 'cars' array to hold 17 'car' class objects is good, in itself; but you would have to load the array with 'car' objects. Currently, all 17 elements contain a 'null' value (no reference to a 'car' class object is present at any of the 17 places in the array). The way you are trying to code the behavior just will not work very easily, mainly becaue you cannot call 'getObjectsInRange' from outside the class of the object that is checking for in-range objects. You should code the behavior in the 'car' class in the 'act' method. Then, it is simply:
public void act()
{
    if (getObjectsInRange(50, Bus.class).isEmpty())
    {
        move(1);
        // you could perform some other actions here (like turning or "atEdge" or non-bus collision checking)
    }
}
With this 'car' class 'act' method, no car will move when within 50 pixels of a Bus object.
Rabe123 Rabe123

2016/5/27

#
Thank you danpost :)
You need to login to post a reply.