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

2019/9/5

Problems with getOneIntersectingOnject

RcCookie RcCookie

2019/9/5

#
import greenfoot.*;
public class Wheel extends Car
{
    public void act()
    {
        
    }
    public int getSurface()
    {
        return getOneIntersectingObject(Surface.class).getType();
    }
}


public class Surface extends Background
{
    private int type;
    
    public void setImage()
    {
        
    }
    
    public int getType()
    {
        return type;
    }
    
    public void setType(int pType)
    {
        type = pType;
        setImage();
    }
}
I am trying to programm a vehicle with all wheels being simulated seperatly. Therefore each wheel needs to detect the type of surface it is currently on. The class Surface simply has got a value and a get-method. To detect the Surface I am using getOneIntersectingObject which returns an Actor. Once I implement the shown Code though I am getting the error "cannot find symbol - method getType()". I have tried using the class Surface instead of getOneIntersectingObject, and it actually worked. But obviously I need to now a certain surface and not a static value. I would be very happy about some Response
danpost danpost

2019/9/5

#
A wheel is only a part of a car and is not a car in itself. Therefore, the Wheel class should not extend the Car class. What you can do is put the Wheel class (which should extend Actor) directly inside the Car class. A car can then, during construction, create its own wheels. A wheel will basically "belong" to the car that created it:
import greenfoot.*;

public class Car extends Actor
{
    private Wheel[] wheels;
    
    public Car()
    {
        wheels = new Wheel[] { new Wheel(), new Wheel(), new Wheel(), new Wheel() };
    }
    
    public int getWheelSurfaceType(int wheelNumber)
    {
        return wheels[wheelNumber].getSurfaceType();
    }
    
    // other car stuff

    public class Wheel extends Actor
    {
        public int getSurfaceType()
        {
            return ((Surface)getOneIntersectingObject(Surface.class)).getType();
        }
    }
}
If there is a possibility that a wheel at some point does not touch a surface, you will get a NullPointerException error. Make sure that when you create a subclass, it IS of the type the class extends (not a part or something else belonging to that type). Subclassing is used to give a more detailed description of the parent type whether by adding to or taking away some behavior(s) or adding new states. For example, an Actor object is any and all objects that can be placed into a world. An Obstacle object could be any Actor object used to restrict movement of another actor. A Wall object is a specific type of Obstacle object. A VanishingWall object could be a Wall that goes away after some time or event. I could go on. A Wheel is not a Car object. A SportsCar, a Convertible, an SUV, a Sedan, etc. are more specific types of cars.
RcCookie RcCookie

2019/9/6

#
You are right with the classes, I´m not that good at java just jet. Also, my problem was solved simply by adding these brackets around the "getOneIntersectingObject()" and adding the "(Surface)". I knew the problem with the nullPointExeption though. Thanks a lot, RcCookie
You need to login to post a reply.