new object problems
in
Programming Questions
•
10 months ago
So I setup the code so that when the zombie is hit by the bullet it dies and it should spawn another zombie. However when the zombie dies a new one appears for half a second and then is gone. I don't know what i did wrong can anyone help?
import java.awt.Rectangle;
PVector pos_player;
Bullet aBullet;
Zombie zombie;
Zombie zombie2;
String[] tileMap;
int TILE_SIZE = 100;
PImage[] tiles = new PImage[10];
float e = 0;
int Z;
boolean death;
void setup() {
size(1200, 800);
smooth();
tileMap = loadStrings("Level.txt");
size(TILE_SIZE*tileMap[0].length(),TILE_SIZE*tileMap.length);
tiles[0] = loadImage("roofLogo.png");
tiles[1] = loadImage("roof.png");
tiles[2] = loadImage("lot.png");
tiles[3] = loadImage("spaces.png");
tiles[4] = loadImage("roofNcorner.png");
tiles[5] = loadImage("roofScorner.png");
tiles[6] = loadImage("roof2.png");
tiles[7] = loadImage("roof3.png");
tiles[8] = loadImage("roof4.png");
tiles[9] = loadImage("spaces2.png");
pos_player = new PVector( -100, -100, 0);
aBullet = new Bullet();
pos_player = new PVector( 100, 400, 0 );
zombie = new Zombie();
zombie2 = new Zombie();
}
void draw() {
for (int i = 0; i < tileMap.length; i++) {
for (int j = 0; j < tileMap[i].length(); j++) {
int tileValue = Character.getNumericValue(tileMap[i].charAt(j));
image(tiles[tileValue],j*TILE_SIZE,i*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
}
stroke(0);
fill(0,255,0);
rect( pos_player.x, pos_player.y, 50, 50);
aBullet.simulate();
aBullet.draw();
zombie.simulate();
zombie.draw();
if(zombie.hitbox.contains(aBullet.hitbox)) {
zombie.death();
if(zombie.death() == true){
zombie2.draw();
zombie2.simulate();
}
}
if(pos_player.y<0 ) {
pos_player.y++;
}
if(pos_player.y>750) {
pos_player.y--;
}
if( key == CODED ){
if( keyCode == UP && keyPressed ){
pos_player.y--;
}
if( keyCode == DOWN && keyPressed ){
pos_player.y++;
}
}
}
void keyPressed(){
if( key == ' ' || key == 'f' ){
aBullet = new Bullet();
}
}
class Bullet{
PVector p,v;
Rectangle hitbox;
Bullet(){
p = new PVector( pos_player.x+50, pos_player.y+25, 0 );
v = new PVector(20,0,0);
hitbox = new Rectangle(int(p.x),int(p.y), 25, 25);
}
void simulate(){
p.x+=v.x;
p.y+=v.y;
hitbox = new Rectangle(round(p.x),round(p.y), 25, 25);
}
void draw(){
pushStyle();
fill(255,0,0);
rectMode(CENTER);
rect( p.x, p.y, 25, 25);
popStyle();
//Hitbox();
}
}
class Zombie {
PVector p,v;
boolean death;
Rectangle hitbox;
Zombie(){
p = new PVector( 1100,random(800),0);
v = new PVector(-1,0,0);
hitbox = new Rectangle(int(p.x),int(p.y),50,50);
}
void simulate(){
p.x+=v.x;
p.y+=v.y;
hitbox = new Rectangle(round(p.x),round(p.y), 50,50);
}
boolean death(){
v = new PVector(1,0,0);
return true;
}
void draw(){
pushStyle();
fill(0,0,255);
rectMode(CENTER);
rect( p.x, p.y, 50, 50);
popStyle();
if(p.x<200){
v = new PVector(0,0,0);
background(0);
}
//Hitbox();
}
}
import java.awt.Rectangle;
PVector pos_player;
Bullet aBullet;
Zombie zombie;
Zombie zombie2;
String[] tileMap;
int TILE_SIZE = 100;
PImage[] tiles = new PImage[10];
float e = 0;
int Z;
boolean death;
void setup() {
size(1200, 800);
smooth();
tileMap = loadStrings("Level.txt");
size(TILE_SIZE*tileMap[0].length(),TILE_SIZE*tileMap.length);
tiles[0] = loadImage("roofLogo.png");
tiles[1] = loadImage("roof.png");
tiles[2] = loadImage("lot.png");
tiles[3] = loadImage("spaces.png");
tiles[4] = loadImage("roofNcorner.png");
tiles[5] = loadImage("roofScorner.png");
tiles[6] = loadImage("roof2.png");
tiles[7] = loadImage("roof3.png");
tiles[8] = loadImage("roof4.png");
tiles[9] = loadImage("spaces2.png");
pos_player = new PVector( -100, -100, 0);
aBullet = new Bullet();
pos_player = new PVector( 100, 400, 0 );
zombie = new Zombie();
zombie2 = new Zombie();
}
void draw() {
for (int i = 0; i < tileMap.length; i++) {
for (int j = 0; j < tileMap[i].length(); j++) {
int tileValue = Character.getNumericValue(tileMap[i].charAt(j));
image(tiles[tileValue],j*TILE_SIZE,i*TILE_SIZE,TILE_SIZE,TILE_SIZE);
}
}
stroke(0);
fill(0,255,0);
rect( pos_player.x, pos_player.y, 50, 50);
aBullet.simulate();
aBullet.draw();
zombie.simulate();
zombie.draw();
if(zombie.hitbox.contains(aBullet.hitbox)) {
zombie.death();
if(zombie.death() == true){
zombie2.draw();
zombie2.simulate();
}
}
if(pos_player.y<0 ) {
pos_player.y++;
}
if(pos_player.y>750) {
pos_player.y--;
}
if( key == CODED ){
if( keyCode == UP && keyPressed ){
pos_player.y--;
}
if( keyCode == DOWN && keyPressed ){
pos_player.y++;
}
}
}
void keyPressed(){
if( key == ' ' || key == 'f' ){
aBullet = new Bullet();
}
}
class Bullet{
PVector p,v;
Rectangle hitbox;
Bullet(){
p = new PVector( pos_player.x+50, pos_player.y+25, 0 );
v = new PVector(20,0,0);
hitbox = new Rectangle(int(p.x),int(p.y), 25, 25);
}
void simulate(){
p.x+=v.x;
p.y+=v.y;
hitbox = new Rectangle(round(p.x),round(p.y), 25, 25);
}
void draw(){
pushStyle();
fill(255,0,0);
rectMode(CENTER);
rect( p.x, p.y, 25, 25);
popStyle();
//Hitbox();
}
}
class Zombie {
PVector p,v;
boolean death;
Rectangle hitbox;
Zombie(){
p = new PVector( 1100,random(800),0);
v = new PVector(-1,0,0);
hitbox = new Rectangle(int(p.x),int(p.y),50,50);
}
void simulate(){
p.x+=v.x;
p.y+=v.y;
hitbox = new Rectangle(round(p.x),round(p.y), 50,50);
}
boolean death(){
v = new PVector(1,0,0);
return true;
}
void draw(){
pushStyle();
fill(0,0,255);
rectMode(CENTER);
rect( p.x, p.y, 50, 50);
popStyle();
if(p.x<200){
v = new PVector(0,0,0);
background(0);
}
//Hitbox();
}
}
1