We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have a shooting game where there are 2 types enemies (rectangles and ellipses) which are both made with arrays and classes and are quite similar. The problem is that when I run the code, I can hit the rectangle enemies as expected but not the ellipses. Whenever I try to hit the ellipses, an error appears saying "ClassCastException".
//Main Function
void setup () {
size(960, 640);
background(0);
player1 = new Player();
bullets = new ArrayList();
enemies0 = new ArrayList();
enemies4 = new ArrayList();
generateEnemyShip0();
generateEnemyShip4();
}
void draw() {
background(0);
levelOne();
}
//-------------------------------------------------------
//Enemyships function
class EnemyShip0 {
float x, y;
float velocity;
EnemyShip0(float x, float y) {
this.x = random(60, width-60);
this.y = 39;
this.velocity = random(1, 2);
}
void display() {
rectMode(CENTER);
fill(255);
rect (this.x, this.y, 30, 30);
}
void move() {
this.y += this.velocity;
if (this.y>height-45) {
this.x = random(60, width-60);
this.y = 39;
}
}
void shoot() {
Bullet b = new Bullet(this.x, this.y, random(3, 5));
bullets.add(b);
}
void hitCheck() {
for (int i = 0; i < bullets.size(); i++) {
Bullet b = (Bullet) bullets.get(i);
float distBetween = dist(b.x, b.y, this.x, this.y);
if (distBetween < 55 && b.velocity < 0) {
enemies0.remove(this);
enemies0.add(new EnemyShip0(x, y));
bullets.remove(b);
score +=random(2, 5);
}
}
}
}
class EnemyShip4 {
float x, y;
float velocity;
EnemyShip4(float x, float y) {
this.x = random(100, width-100);
this.y = 20;
this.velocity = random(1, 2);
}
void display() {
fill(100);
ellipse (this.x, this.y, 40, 40);
}
void move() {
this.y += this.velocity;
if (this.y>height-45) {
this.x = random(100, width-100);
this.y = 20;
}
}
void shoot() {
Bullet b = new Bullet(this.x, this.y, random(3, 5));
bullets.add(b);
}
void hitCheck() {
for (int i = 0; i < bullets.size(); i++) {
Bullet b = (Bullet) bullets.get(i);
float distBetween = dist(b.x, b.y, this.x, this.y);
if (distBetween < 55 && b.velocity < 0) {
enemies4.remove(this);
enemies4.add(new EnemyShip0(x, y));
bullets.remove(b);
score +=random(3, 5);
}
}
}
}
//---------------------------------------
//Player function
class Player {
float x, y;
int lives = 10;
int score;
boolean canShoot;
Player() {
this.x = width/2;
this.y = height-50;
}
void display() {
fill(255);
triangle (this.x, this.y, this.x + 40, this.y + 80, this.x - 40, this.y +80);
textAlign(LEFT);
fill(0);
text(lives, 55, 75);
}
void shoot() {
Bullet bullet = new Bullet(this.x, this.y, -8);
bullets.add(bullet);
}
void hitCheck() {
for (int i = 0; i < bullets.size(); i++) {
Bullet b = (Bullet) bullets.get(i);
float distBetween = dist(b.x, b.y, this.x, this.y);
if (distBetween < 30 && b.velocity > 0) {
lives = lives -1;
bullets.remove(b);
}
if (lives == 0) {
this.die();
}
}
}
void die() {
this.x = width/0.1;
textSize(14);
text("You Died", 10, 30);
fill(0, 102, 153);
}
}
//---------------------------------------------------------------
//Bullet function
class Bullet {
float x, y;
float velocity;
Bullet(float x, float y, float velocity) {
this.x = x;
this.y = y;
this.velocity = velocity;
}
void displayForPlayer() {
fill(0, 255, 0);
ellipse(this.x, this.y, 10, 10);
}
void displayForShip0 () {
fill(255);
ellipse(this.x, this.y, 15, 15);
}
void move() {
this.y+=this.velocity;
}
}
//-------------------------------------------------
//levelone function
ArrayList bullets;
ArrayList enemies0;
ArrayList enemies4;
int score;
int level = 1;
int totalships = 5;
int numCol = 10;
Player player1;
EnemyShip0 testEnemyShip0;
EnemyShip4 testEnemyShip4;
boolean keyAPressed = false, keyDPressed = false;
void levelOne() {
handleBullets();
movePlayer1();
player1.hitCheck();
handleEnemyShip0();
handleEnemyShip4();
textSize(10);
fill(#FFF92E);
noStroke();
quad(-10, 30, 100, 30, 90, 50, -10, 50);
fill(#2F7AF7);
quad(970, 30, 870, 30, 880, 50, 970, 50);
fill(#33F51B);
quad(-10, 60, 80, 60, 70, 80, -10, 80);
fill (0);
textAlign(LEFT);
text("SCORE:", 10, 45);
text(score, 60, 45);
text("LEVEL:", 900, 45);
text(level, 940, 45);
text("LIVES:", 10, 75);
player1.display();
}
void generateEnemyShip0() {
for (int i = 0; i < totalships; i++) {
float x = width*.1 + i%numCol*50;
float y = 25+ int(i/numCol)*60 ;
enemies0.add(new EnemyShip0(x, y));
}
}
void generateEnemyShip4() {
for (int i = 0; i < totalships; i++) {
float x = width*.1 + i%numCol*50;
float y = 25+ int(i/numCol)*60 ;
enemies4.add(new EnemyShip4(x, y));
}
}
void handleEnemyShip0() {
for (int i = 0; i < enemies0.size(); i++) {
EnemyShip0 ship0 = (EnemyShip0) enemies0.get(i);
ship0.move();
ship0.hitCheck();
if (random(1) > .995) { //ang random() kay ang speed sa bullet sa kontra
ship0.shoot();
}
ship0.display();
}
}
void handleEnemyShip4() {
for (int i = 0; i < enemies4.size(); i++) {
EnemyShip4 ship4 = (EnemyShip4) enemies4.get(i);
ship4.move();
ship4.hitCheck();
if (random(1) > .995) {
ship4.shoot();
}
ship4.display();
}
}
void handleBullets() {
for (int i = 0; i < bullets.size(); i++) {
Bullet b = (Bullet) bullets.get(i);
b.move();
b.displayForPlayer();
}
}
void movePlayer1() {
if (keyAPressed) {
player1.x -=30;
}
if (keyDPressed) {
player1.x +=30;
}
}
void keyPressed() {
if (key == CODED) {
if (keyCode == LEFT) {
player1.x -=30;
}
}
if (key == CODED) {
if (keyCode == RIGHT) {
player1.x +=30;
}
} else {
if (key == ' ') {
player1.shoot();
}
}
}
Answers
Which line number
The problem is line 89 you are adding the wrong type of EnemyShip, it should be
enemies4.add(new EnemyShip4(x, y));
The solution is to use generics so when you declare and create the array lists you use
Declarations
Creation code
If you do this you do not have to cast the object returned by
get()
so for instanceBullet b = (Bullet) bullets.get(i);
becomes
Bullet b = bullets.get(i);
Not only will this save you typing but the current error would have been picked up by the compiler rather than at run time.
@quark I'm such an idiot. I didn't notice the cause of the problem is just a simple mistake. Thanks for pointing it out.
Yes it is a simple mistake but I still recommend you make the changes I suggest and use generics. If you had your program wouldn't have run, instead the compiler would have got a helpful error message.