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

2018/2/8

Scrolling Background in direction of actor...

Timmy_11 Timmy_11

2018/2/8

#
I want to make a racing game were the car turns and the background moves in the direction that the car is pointing but i have no idea were to start... Please give help. Thanks.
danpost danpost

2018/2/8

#
Would this be a top-down view where the car actually turns to face different directions but the track is larger than the world window?
Timmy_11 Timmy_11

2018/2/8

#
yes, but i figured it out in the mean time thanks any way :)
Timmy_11 Timmy_11

2018/2/8

#
I did
if(Greenfoot.isKeyDown("up"))
        {
            setLocation(280, 280);
            if(Greenfoot.isKeyDown("left"))
            {
                turn(-1);
            }
            if(Greenfoot.isKeyDown("right"))
            {
                turn(1);
            }
            move(1);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(280, 280);
            if(Greenfoot.isKeyDown("left"))
            {
                turn(1);
            }
            if(Greenfoot.isKeyDown("right"))
            {
                turn(-1);
            }
            move(-1);
        } 
for the Car and
if(Greenfoot.isKeyDown("up"))
        {
            int myX = getX();
            int myY = getY();
            if(Greenfoot.isKeyDown("up"))
            {
                Race_Car car = (Race_Car)getWorld().getObjects(Race_Car.class).get(0);
                int x = car.getX()-280;
                int y = car.getY()-280;
                setLocation(myX+x, myY+y);
            }
        }
        if(Greenfoot.isKeyDown("down"))
        {
            int myX = getX();
            int myY = getY();
            if(Greenfoot.isKeyDown("down"))
            {
                Race_Car car = (Race_Car)getWorld().getObjects(Race_Car.class).get(0);
                int x = car.getX()-280;
                int y = car.getY()-280;
                setLocation(myX+x, myY+y);
            }
        }
for the track
Timmy_11 Timmy_11

2018/2/8

#
But the car if a little shaky, also the world doesn't always travel the same exact direction that the car is pointing. For example the car can be pointing slightly down but the track will continue on in a strait line not down a little as it should. How can the be fixed????
Timmy_11 Timmy_11

2018/2/8

#
Help please.
danpost danpost

2018/2/8

#
Timmy_11 wrote...
But the car if a little shaky, also the world doesn't always travel the same exact direction that the car is pointing. For example the car can be pointing slightly down but the track will continue on in a strait line not down a little as it should. How can the be fixed????
I will have to get my head around what is actually happening.
danpost danpost

2018/2/8

#
I am not sure what the last location of the track has to do with where it needs to be; or why any key needs to be down. Try:
public void act()
{
    Actor car = (Actor)getWorld().getObjects(Race_Car.class).get(0);
    setLocation(car.getX()-280, car.getY()-280);
}
Timmy_11 Timmy_11

2018/2/8

#
I need the last position cause else when the car resets it will put the track way back
danpost danpost

2018/2/8

#
Timmy_11 wrote...
I need the last position cause else when the car resets it will put the track way back
Well, you do not want to use it when scrolling -- only for when resetting.
Timmy_11 Timmy_11

2018/2/8

#
use the
public void act()
{
    Actor car = (Actor)getWorld().getObjects(Race_Car.class).get(0);
    setLocation(car.getX()-280, car.getY()-280);
}
would be for for resetting????
danpost danpost

2018/2/8

#
Forget what I gave above. It will not work for you. Working on solution.
danpost danpost

2018/2/8

#
If you are wanting the appearance of scrolling, then the car should not move at all -- just turn; and when it is to appear to move, the track should move -- not the car. To achieve this, you can have the car re-locate the track. Remove the act method from the track class and at the beginning of the car code you game above, add the following lines:
int x = getX();
int y = getY();
Then at the end, add these lines:
int dx = getX()-x;
int dy = getY()-y;
Actor track = (Actor)getWorld().getObjects(Track.class).get(0);
track.setLocation(track.getX()+dx, track.getY()+dy);
setLocation(getX()+dx, getY()+dy);
Timmy_11 Timmy_11

2018/2/9

#
Thanks it'll work now thanks for your help and for your time :)
You need to login to post a reply.