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

2016/5/11

How to make car run fast on track and slow on the grass

fandyrazzle fandyrazzle

2016/5/11

#
i hope i can find some one who can solve this porblem. its whats happening. im planning to make a car race game. Car run fast on the track(image and as actor), and run slow on the grass (image as world). it looks like the track's transparent image is get involved. So when i used getOneIntersectingObject & isTouching. still makes my car fast. How do i do? do you have any idea how to solve my problem? im planning to make track as world, but i dont know how to write code to determine transparent area is grass that makes my car slow, and actual track makes car fast. here is screenshoot of my project. any suggestion is precious for me.
danpost danpost

2016/5/11

#
The fact that the track image is transparent off the actual track location should be enough:
1
2
3
4
Track track = (Track)getOneIntersectingObject(Track.class);
int goSpeed = speed; // 'speed' is track speed
if (track.getImage().getColorAt(getX(), getY()).getAlpha() == 0) goSpeed = speed2; // 'speed2' is off-track speed
move(goSpeed);
valdes valdes

2016/5/11

#
danpost wrote...
The fact that the track image is transparent off the actual track location should be enough:
1
2
3
4
Track track = (Track)getOneIntersectingObject(Track.class);
int goSpeed = speed; // 'speed' is track speed
if (track.getImage().getColorAt(getX(), getY()).getAlpha() == 0) goSpeed = speed2; // 'speed2' is off-track speed
move(goSpeed);
This code will fail if you are on open field (NullPointerException). Make this adjustment:
1
if (track == null || track.getImage().getColorAt(getX(), getY()).getAlpha() == 0) goSpeed = speed2;
danpost danpost

2016/5/11

#
valdes wrote...
< Quote Omitted > This code will fail if you are on open field (NullPointerException). Make this adjustment: < Code Omitted >
It was presumed that a track of one kind or another will always be there and that the entire world window would be covered by it. However, the inclusion of the extra condition would be a good adjustment as the presumptions I made may not be entirely accurate. Thanks valdes.
You need to login to post a reply.