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

2017/3/24

Problem with slide

Telko Telko

2017/3/24

#
Hey, Im struggling with creating a slide movement for my Actor. Im using the basic SmoothMover class that you can Import and found this piece of code created by Super_Hipp @/topics/8306/0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if ( World.gamemode == 3) {
          if (Greenfoot.isKeyDown("enter")) {
              Wold.playerspeed = 800;
          }
           if(Greenfoot.isKeyDown("Up"))
          {
              if (v<0.9) {v+=0.1;}
              else {v = 1;}
          }
          if(Greenfoot.isKeyDown("Down") && getY() < 450)
          {
              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;}
       
     }
v is a private double = 0 , but in the game it does not work. I checked that v does not get changed at any time which make me wonder. Thanks for youre help.
danpost danpost

2017/3/24

#
For one thing, "Up" should be "up" and "Down" should be "down" (lines 5 and 10).
Telko Telko

2017/3/24

#
Do you know how to make a vertical slide?
danpost danpost

2017/3/24

#
Telko wrote...
Do you know how to make a vertical slide?
What exactly do you mean by "a vertical slide"? What changes in acceleration and/or velocity are you wanting? What response do you want from each key while they are held down? and what should happen when they are not held down? explain each individually.
Telko Telko

2017/3/24

#
In my game there is the player on a fixed x-coordinate and from the right side cars spawn that move backwards and the player has to dodge them. I want it so that when i press "up" the objects y-coordinate changes to the top of the display (minus in coordinate system). When I stop pressing the button there should be a "run out" so the player cannot stay on a specific point. At the same time its important that when I press the "down" key this "run out" stops or accelerates in the other direction (plus in coordinate system). Velocity: should go smaller when the button is not pressed Acceleration: changes in acceleration are not necessary Any kind of form in between what i described will be helpfull, thanks for youre support man!
danpost danpost

2017/3/24

#
Basically, what you are asking for is that the keys act as accelerators in opposite vertical directions and there is a drag factor applied to the object. Without a SmoothMover class, you can simply do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// instance fields
private int speed; // x100 zoom factor
private int zoomY; // x100 zoom factor
 
// in act
int ds = 0; // to hold changes due to keypresses
if (Greenfoot.isKeyDown("up")) ds--;
if (Greenoot.isKeyDown("down")) ds++;
speed  = speed-((int)Math.signum(speed)*5)+ds*15; // new speed (with drag and accelerators)
if (speed < -600) speed = -600; // limit up speed
if (speed > 600) speed = 600; // limit down speed
zoomY += speed; // new zoomed target y-coordinate
if (zoomY/100 > getWorld().getHeight()-1) zoomY = (getWorld().getHeight()-1)*100; // high zoomed y-coordinate limit
if (zoomY < 0) zoomY = 0; // low y-coordinate limit
setLocation(getX(), zoomY/100);
You need to login to post a reply.