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

2015/1/2

How to let an object 'slide' after a movement?

AnthonioBanderas AnthonioBanderas

2015/1/2

#
Hi guys, I am making a game where my speedboat object needs to 'slide' a bit after I've done a movement. It eventually needs to stop (offcourse). When I am moving now and stop pressing the keys, it just immediately stops, but I want it to slide a little after I stop pressing the buttons in the direction it was moving. Does anyone here has an idea of how I should do this or could someone send me a piece of code please? This is the movement code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class SpeedBoat extends Actor
{
 
     
    public void act()
    {
    if(Greenfoot.isKeyDown("left"))
    {
        turn(-3);
    }
    if(Greenfoot.isKeyDown("up"))
    {
        move(1);
    }
    if(Greenfoot.isKeyDown("down"))
    {
        move(-1);
    }
    if(Greenfoot.isKeyDown("right"))
    {
       turn(3);
    }
 
}
Thanks for the help already!
Super_Hippo Super_Hippo

2015/1/2

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class SpeedBoat extends Actor
{
    private double v = 0;
      
    public void act()
    {
        if(Greenfoot.isKeyDown("left")) turn(-3);
        if(Greenfoot.isKeyDown("right")) turn(3);
        if(Greenfoot.isKeyDown("up"))
        {
            if (v<0.9) v+=0.1;
            else v = 1;
        }
        if(Greenfoot.isKeyDown("down"))
        {
            if (v>-0.9) v-=0.1;
            else v =-1;
        }
      
        if (v>0.1 || v<-0.1) {move(v); v*=0.95;}
        else v=0;
    }
}
This should work I think. But if you turn, you slide to that direction, which isn't really realistic for a boat.
danpost danpost

2015/1/2

#
Right now, the speedboat is already moving as slow as it can without skipping act cycles (not much of a speedster either, only moving one pixel per act). A smooth mover system is probably what you will need (something to track the location and movement of the boat more precisely).
Super_Hippo Super_Hippo

2015/1/2

#
Oh yes, I forgot that part. Without extending it a smooth mover (or copy the methods from there), 'move(v)' won't be accepted.
AnthonioBanderas AnthonioBanderas

2015/1/2

#
Super_Hippo wrote...
Oh yes, I forgot that part. Without extending it a smooth mover (or copy the methods from there), 'move(v)' won't be accepted.
Thanks for the effort! But the code doesn't work :( I get the error "Actor cannot be aplied to given types"
danpost danpost

2015/1/2

#
Do not just dismiss it because of an error you are getting. Show what you tried and maybe we can help resolve it.
Super_Hippo Super_Hippo

2015/1/2

#
Did you import the Smooth Mover class and have the SpeedBoat extend it?
You need to login to post a reply.