Hello, I tried to write the code which separates the movements of two cars implementing a new constractor calling Key. But it give me a error.
I want car2 to move using "w", "s", "a", "d", as car1 moves "up", "down" ,"left", "right".
Somehow on MyWorld class
Give me an error, so as there are runtime errors for Car class
Here is I've got;
Car car2 = new Car(new String("car3.png"), new String("car4.png"), new String(Key));import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Class that models objects of type "car"
*
*
* @1.0
*/
public class Car extends Actor
{
// Attributes of the class Car
private static int numberWheels = 4; // Number of wheels
private boolean areHeadlightsOn = false; // State of headlights (on => true; off => false)
private String Key;
private String image1;
private String image2;
private boolean isKeyDown = false;
/**
* Constructor
*/
public Car(String file1, String file2, String Key){
setImage1(file1); // Sets the attribute image1 to file1
setImage2(file2); // Sets the attribute image2 to file2
setKey(Key);
setImage(file1);
// Sets the image that appears in the screen at the beginning to the one in file1
}
/**
* Method that is being always invoked when the program is running
*/
public void act(){
moveForward();
moveBack();
turnLeft();
turnRight();
turnHeadlightsOn();
turnHeadlightsOff();
}
/**
* Move the car forward every time the key "up" is pressed
*/
public void moveForward(){
if (Greenfoot.isKeyDown(getKey())){
move(5);
}
}
/**
* Move the car back every time the key "down" is pressed
*/
public void moveBack(){
if (Greenfoot.isKeyDown(getKey())){
move(-1);
}
}
/**
* Turn the car to the left every time the key "left" is pressed
*/
public void turnLeft(){
if (Greenfoot.isKeyDown(getKey())){
turn(-4);
}
}
/**
* Turn the car to the right every time the key "right" is pressed
*/
public void turnRight(){
if (Greenfoot.isKeyDown(getKey())){
turn(4);
}
}
/**
* Turn the headlights on when the key "space" is pressed.
* The attribute isKeyDown is used to prevent setting the headlights on and off when the key "space" is pressed (and remains pressed for some time)
*/
public void turnHeadlightsOn(){
if (Greenfoot.isKeyDown("space") && !getIsKeyDown()){
if (getAreHeadlightsOn() == false){
setAreHeadlightsOn(true);
setImage(getImage2());
setIsKeyDown(true);
}
}
if (!Greenfoot.isKeyDown("space") && getIsKeyDown()){
setIsKeyDown(false);
}
}
/**
* Turn the headlights off when the key "space" is pressed.
* The attribute isKeyDown is used to prevent setting the headlights off and on when the key "space" is pressed (and remains pressed for some time)
*/
public void turnHeadlightsOff(){
if (Greenfoot.isKeyDown("space") && !getIsKeyDown()){
if (getAreHeadlightsOn() == true){
setAreHeadlightsOn(false);
setImage(getImage1());
setIsKeyDown(true);
}
}
if (!Greenfoot.isKeyDown("space") && getIsKeyDown()){
setIsKeyDown(false);
}
}
public void setAreHeadlightsOn(boolean areHeadlightsOn){
this.areHeadlightsOn = areHeadlightsOn;
}
public boolean getAreHeadlightsOn(){
return this.areHeadlightsOn;
}
public void setImage1(String image1){
this.image1 = image1;
}
public String getImage1(){
return this.image1;
}
public void setImage2(String image2){
this.image2 = image2;
}
public String getImage2(){
return this.image2;
}
public void setIsKeyDown(boolean isKeyDown){
this.isKeyDown = isKeyDown;
}
public boolean getIsKeyDown(){
return this.isKeyDown;
}
public void setKey(String Key){
if (this.Key == "w") {this.Key = "up";}
if (this.Key == "s") {this.Key = "down";}
if (this.Key =="a") {this.Key = "left";}
if (this.Key =="d") {this.Key = "right";}
}
public String getKey(){
return this.Key;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Class that creates a new world: MyWorld
*
* @Java MOOC team, UC3M
* @1.0
*/
public class MyWorld extends World
{
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
super(800, 600, 1);
setBackground("road.jpg");
populateWorld();
String Key;
}
public void populateWorld(){
Car car1 = new Car(new String("car1.png"), new String("car2.png"), new String("up"));
Car car2 = new Car(new String("car3.png"), new String("car4.png"), new String(Key));
addObject(car1, 120, 70);
addObject(car2, 580, 30);
}
}
