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

2012/1/6

Where's the Mistake?

TopInPut TopInPut

2012/1/6

#
I want my Player to set the Speed slower, if the key is not pressed. And on every field a bit slower. A bit like an accerleration. But my Player don't do it. He stops when i don't press the buttom. And when i press the buttom on, he gets faster. His maximum Speed should be 5 and without pressing it should be 0. The Code is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Mover
{
    int Speed;
    int bSpeed;
    int Blockade;
    int xOffset;
    int yOffset;
    int speed;

    public Player() {
        Speed = 1;
        bSpeed = 1;
        Blockade = -3;
        speed = Speed + bSpeed;
    }

    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.isKeyDown("up||down||left||right||a||s||d||w")) {
                Speed = 0;
                bSpeed = 0;
        }
    }   

    public void up() {
        setLocation( getX() , getY() - Speed );
        Speed = Speed + bSpeed;

    }

    public void down() {
        setLocation( getX() , getY() + Speed ) ;
        Speed = Speed + bSpeed;

    }

    public void right() {
        setLocation( getX() + Speed , getY()) ;
        Speed = Speed + bSpeed;

    }

    public void left() {
        setLocation( getX() - Speed , getY() ) ;
        Speed = Speed + bSpeed;

    }

    //public boolean onActor()
    //{
     //  Actor under = getOneObjectAtOffset ( 0,    //getImage().getHeight()/2, Player.class);
     //   return under != null;

   // }
TopInPut TopInPut

2012/1/6

#
{up, down, left, right, w, a, s, d are defined with setlocation(getX() , getY()) }
Morran Morran

2012/1/6

#
You want something that accelerates if you press a button, and slowly comes to a stop if you don't? Maybe this will work:
public class Player extends Mover
{
 //your instance variables that are not movement related

 //and here will go the variables for movement:
 private int maximumXSpeed; //the highest speed this Player can move sideways
 private double xAcceleration; //the speed it accelerates when you press the button
 private double xSpeed; //the current speed of this Player(it's a double and 
 //not an int so that it's very precise.

 private int maximumYSpeed; //the highest speed this Player can move vertically
 private double yAcceleration; //the speed it accelerates vertically when you press the button
 private double ySpeed; //the current speed of this Player(it's a double and 
 //not an int so that it's very precise.

 public Player()
 {
  maximumXSpeed = 5; //you want the maximum speed to be 5?
  xAcceleration = .75; //set this higher or lower as you like

  maximumYSpeed = 5; //what do you want the max Y speed to be?
  yAcceleration = .75; //set this higher or lower as you like also

  //this Player starts off not moving.
  xSpeed = 0;
  ySpeed = 0;
 }

 public void move()
 {
  //for the horizontal movement:
  if(Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a")) {
   if(xSpeed > -maximumXSpeed) //if Player is travelling slower than maxSpeed
     xSpeed -= xAcceleration; //if xSpeed is negative, the Player will move left.
   }
  else if(Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("d")) {
   if(xSpeed < maximumXSpeed) //if Player is travelling slower than maxSpeed
    xSpeed += xAcceleration; //if xSpeed is currently positive, the Player will move right.
  }
  else
   decelerateSideways();

  //for the vertical movement:
  if(Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("w")) {
   if(ySpeed > -maximumYSpeed) //if Player is travelling slower than maxSpeed
    ySpeed -= yAcceleration; //if ySpeed is negative, the Player will move left.
  }
  else if(Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("s")) {
   if(ySpeed > -maximumYSpeed) //if Player is travelling slower than maxSpeed
    ySpeed += yAcceleration; //if ySpeed is currently positive, the Player will move right.
  }
  else
   decelerateVertically();

  //update location, based upon your current speed:
  setLocation(getX() + (int) xSpeed, getY() + (int) ySpeed); //if xSpeed and ySpeed is very small, nothing happens.
 }

 public void deccelerateSideways()
 {
  xSpeed *= .7; //decrease the current speed considerably. (Adjust this to your liking)
 }
 public void deccelerateVertically()
 {
  ySpeed *= .7; //decrease the current speed considerably. (Adjust this to your liking also)
 }
  
  //the rest of your code
}
TopInPut TopInPut

2012/1/7

#
Thx. That's a nice and working solution. ;)
davmac davmac

2012/1/7

#
By the way, this: if(Greenfoot.isKeyDown("up||down||left||right||a||s||d||w")) { ... doesn't work. I guess you're trying to check if any of those keys are down; you'd need to do: if (Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("left") ... etc instead.
TopInPut TopInPut

2012/1/7

#
I know.;) Just wrote it like that.;)
You need to login to post a reply.