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

2015/10/16

Loading next segment

sse0005 sse0005

2015/10/16

#
I am working on a game where the user can make their own map out of pre made squares with a track on them. The car that will travel along the track needs to be able to find the next square so it knows what track to follow. i am attempting to do this using the getObjectsAt, everything compiles but starts it starts throwing pointer exceptions when it comes time to load the next piece. What i am trying to do is check for each type of track piece. the method calls are correct, when i run to in debug it fails at the "rawInfo =" line, i don't think i am getting a list of objects.
private void loadTrack()
    {
        if (Math.abs(xList[29]-xList[28])>Math.abs(yList[29]-yList[28]))
        {
            
            if(xList[29]>xList[28])
            {
                nextX= xList[29]+10;
                nextY= yList[29];
            }
            else
            {
                nextX= xList[29]-10;
                nextY= yList[29];
            }
        }
        else
        {
            if(yList[29]>yList[28])
            {
                nextY= yList[29]+10;
                nextX= xList[29];
            }
            else
            {
                nextY= yList[29]-10;
                nextX= xList[29];
            }
        }
        
        
        if(getWorld().getObjectsAt(nextX,nextY,Start.class)!=null)
        {
            rawInfo =((Start) getWorld().getObjectsAt(nextX,nextY,Start.class).get(0)).getLaneInfo(nextLane);
            trackType =((Start) getWorld().getObjectsAt(nextX,nextY,Start.class).get(0)).getTrackType();
        }
        
        if(getWorld().getObjectsAt(nextX,nextY,StraitLR.class)!=null)
        {
            rawInfo =((StraitLR) getWorld().getObjectsAt(nextX,nextY,StraitLR.class).get(0)).getLaneInfo(nextLane);
            trackType =((StraitLR) getWorld().getObjectsAt(nextX,nextY,StraitLR.class).get(0)).getTrackType();
        }
        
        if(getWorld().getObjectsAt(nextX,nextY,StraitUD.class)!=null)
        {
           rawInfo =((StraitUD) getWorld().getObjectsAt(nextX,nextY,StraitUD.class).get(0)).getLaneInfo(nextLane);
           trackType =((StraitUD) getWorld().getObjectsAt(nextX,nextY,StraitUD.class).get(0)).getTrackType();
        }
        
        if(getWorld().getObjectsAt(nextX,nextY,CurveLD.class)!=null)
        {
            rawInfo =((CurveLD) getWorld().getObjectsAt(nextX,nextY,CurveLD.class).get(0)).getLaneInfo(nextLane);
            trackType =((CurveLD) getWorld().getObjectsAt(nextX,nextY,CurveLD.class).get(0)).getTrackType();
        }
        
        if(getWorld().getObjectsAt(nextX,nextY,CurveLU.class)!=null)
        {
            rawInfo =((CurveLU) getWorld().getObjectsAt(nextX,nextY,CurveLU.class).get(0)).getLaneInfo(nextLane);
            trackType =((CurveLU) getWorld().getObjectsAt(nextX,nextY,CurveLU.class).get(0)).getTrackType();
        }
        
        if(getWorld().getObjectsAt(nextX,nextY,CurveRD.class)!=null)
        {
            rawInfo =((CurveRD) getWorld().getObjectsAt(nextX,nextY,CurveRD.class).get(0)).getLaneInfo(nextLane);
            trackType =((CurveRD) getWorld().getObjectsAt(nextX,nextY,CurveRD.class).get(0)).getTrackType();
        }
        
        if(getWorld().getObjectsAt(nextX,nextY,CurveRU.class)!=null)
        {
            rawInfo =((CurveRU) getWorld().getObjectsAt(nextX,nextY,CurveRU.class).get(0)).getLaneInfo(nextLane);
            trackType =((CurveRU) getWorld().getObjectsAt(nextX,nextY,CurveRU.class).get(0)).getTrackType();
        }
        
        nextLane = rawInfo[0][0];
        xList = rawInfo[1];
        yList = rawInfo[2];
        speedList = rawInfo[3];
        angleList = rawInfo[4];
    }
danpost danpost

2015/10/16

#
The problem is that your 'if' conditions (lines 32, 38, 44, 50, 56, 62 and 68) will always return true; whether an object of the given class is at the given location or not. The condition asked is whether 'getObjectsAt' returns a List object or not. Well, yes; it always returns a List object. What you really want to ask is if that returned List object is empty or not:
if ( ! getWorld().getObjectsAt(nextX, nextY, Start.class).isEmpty() )
sse0005 sse0005

2015/10/17

#
Thanks, that was the problem.
You need to login to post a reply.