I'm trying to make a car object that can drive around my world using the arrow keys for direction. I have the code below, but my car object isn't appearing within the world
World:
Car:
ControlledCar (subclass of car):
public class MyWorld extends World
{
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
ControlledCar player = new ControlledCar("null",10,100);
}
}public class Car extends Actor
{
//setter vars
String licencePlate;
double speed;
double maxSpeed;
//setter methods
void setLicence(String licencePlate) { //setter method for licence plate
this.licencePlate = licencePlate; //licence plate changes to input
}
void setSpeed(double speed) { //setter method for speed
this.speed = speed; //speed changes to input
}
void setMaxSpeed(double maxSpeed) { //setter method for max speed
this.maxSpeed = maxSpeed; //maxspeed changes to input
}
//getter methods
String getLicence() { //getter method for licence plate
return this.licencePlate; //return this instances licence plate
}
double getSpeed() { //getter method for speed
return this.speed; //return this instances speed
}
double getMaxSpeed() { //getter method for max speed
return this.maxSpeed; //return this instances maxspeed
}
//extra methods
//'floor' it method
void floorIt() {
this.speed = this.maxSpeed; //set speed to maxspeed
}
//accelerate method
void accelerate(double velocity) {
this.speed += velocity; //change speed based on change in velocity
if (this.speed >= this.maxSpeed) { //test if new speed is greater
this.speed = this.maxSpeed; //if it is, speed = maxspeed
}
if (this.speed < 0.0) { //test if speed is less than 0
this.speed = 0.0; //if it is, speed = 0
}
}
public void act()
{
}
//creation of Car constructor
public Car(String licenceIn, double speedIn, double maxSpeedIn) { //take input of licence plate, speed, and max speed
licencePlate = licenceIn; //licencePlate var will always be the licenceIn
if (speedIn >= 0) { //check if speedIn is greater than 0
speed = speedIn; //if greater, speed = speedIn
} else {
speed = 0.0; //otherwise speed = 0
}
if (maxSpeedIn >= 0) { //check if maxspeed is greater than 0
maxSpeed = maxSpeedIn; //if greater, maxspeed = maxspeedin
} else {
maxSpeed = 0.0; //otherwise, maxspeed = 0
}
}
//wrap method to change location of object to other side of world
void wrap() {
if(getX() == getWorld().getWidth()-1) { //check if at edge of world width
setLocation(1, getY()); //set location to other edge
}
if(getY() == getWorld().getHeight()-1) { //check if at edge of world height
setLocation(getX(), 1); //set location to other edge
}
if(getX() == 0) { //check if at start of widths edge
setLocation(getWorld().getWidth()-1, getY()); //set location to other edge
}
if(getY() == 0) { //check if at start of heights edge
setLocation(getX(), getWorld().getHeight()-1); //set location to other edge
}
}
}public class ControlledCar extends Car
{
/**
* Act - do whatever the ControlledCar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
setImage("car01.png");
String key = Greenfoot.getKey();
if (key != null) {
switch(key) {
case "up":
setRotation(90);
wrap();
move(5);
break;
case "right":
setRotation(0);
wrap();
move(5);
break;
case "left":
setRotation(180);
wrap();
move(5);
break;
case "down":
setRotation(270);
wrap();
move(5);
break;
}
}
}
ControlledCar(String licenceIn, double speedIn, double maxSpeedIn) {
super(licenceIn, speedIn, maxSpeedIn);
}
}
