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

2015/2/9

bug/unexpexted use of isTouching

fejfo fejfo

2015/2/9

#
for a race game I make I want the car to slow down if itsn't touching the track to slow down the car this is the code in car :
public void act() 
    {
        Greenfoot.setSpeed(40);
        if(Greenfoot.isKeyDown("up")) {
            if(isTouching(RaceTrack.class)) {
                move(5);
            }
            else move(1);
        }
        
        if(Greenfoot.isKeyDown("left")) turn(-10);
        if(Greenfoot.isKeyDown("right")) turn(10);
            
    } 
    
    public boolean debug() {
        return isTouching(RaceTrack.class);
    }
but if the car touthes the alpha data part of the picture ( see true) the
if(isTouching(RaceTrack.class))
still returns true i didn't expect that is there an easy way to fix this (i don't want to create a track piece by piece without alphadata or hard code to co-oords of the tack) ?
danpost danpost

2015/2/9

#
Unfortunately, there really is no easy way to fix this. You need to either determine the alpha part of the track image at all four corners of the car image or use some type of pad intersection method whereby a method is executed on the intersecting pad that determines if the car is on the track or not. Two types of pads would be minimally required -- one for straight-aways and the other for turns. For the straight-aways, the pad can cover all locations the car can be located; for the turns, it can be placed at the focal point of the arc the turn makes (if the arc was allowed to continue around into a circle -- it would be placed at the center of that circle); the method would use two getObjectsInRange method calls to determine if the car was on the track or not (the car should be within the wider range and not within the shorter range).
You need to login to post a reply.