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

2018/3/10

Changing directions

Spicy0506 Spicy0506

2018/3/10

#
I'm trying to have the "Tron" bikes face a different direction when i press the arrow keys (also WASD for the other bike) but I need help figuring out why they wont turn when i press the "up" and "left". The bike don't turn but they work when i press the down and right.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Bike extends Actor
{
    private boolean isBikeBlue;
    private String up;
    private String down;
    private String left;
    private String right;
    public Bike(boolean isBikeBlue)
    {
        this.isBikeBlue =isBikeBlue;
        if (isBikeBlue == true)
        {
            setImage("bike1.png");
            up = "w";
            down = "s";
            left = "a";
            right = "d";
        }
        else
        {
            setImage("bike2.png");
            up = "up";
            down = "down";
            left = "left";
            right = "right";
        }
    }
    public void act() 
    {
        buildTrail();
        movement();
    }   
    public void buildTrail()
    {
        Trail trail = new Trail(isBikeBlue);
        getWorld().addObject (trail,getX(),getY());
    }
    public void movement()
    {
      move(5);
      if (Greenfoot.isKeyDown(up))
      {
          setRotation(360);
      }
      if (Greenfoot.isKeyDown(down))
      {
          setRotation(90);
      }
      if (Greenfoot.isKeyDown(left))
      {
          setRotation(180);
      }
      if (Greenfoot.isKeyDown(right))
      {
          setRotation(0);
      }
    }
}
danpost danpost

2018/3/10

#
Spicy0506 wrote...
I'm trying to have the "Tron" bikes face a different direction when i press the arrow keys (also WASD for the other bike) but I need help figuring out why they wont turn when i press the "up" and "left". The bike don't turn but they work when i press the down and right. << Code Omitted >>
You have a '0' and a '360', which are basically the same, and no '270' in the 'movement' method.
Spicy0506 Spicy0506

2018/3/10

#
danpost wrote...
Spicy0506 wrote...
I'm trying to have the "Tron" bikes face a different direction when i press the arrow keys (also WASD for the other bike) but I need help figuring out why they wont turn when i press the "up" and "left". The bike don't turn but they work when i press the down and right. << Code Omitted >>
You have a '0' and a '360', which are basically the same, and no '270' in the 'movement' method.
Ohhh Thank you!! Also I had another question, when the bikes are going in a direction (lets say right) and I press the left key the bike removes its self. Is there a way to not allow you to go the opposite direction at the specific time.
danpost danpost

2018/3/10

#
Spicy0506 wrote...
when the bikes are going in a direction (lets say right) and I press the left key the bike removes its self. Is there a way to not allow you to go the opposite direction at the specific time.
Add another condition along with the key detection for each direction. For example:
if (Greenfoot.isKeyDown(up) && getRotation() != 90)
You need to login to post a reply.