We are about to switch to a new forum software. Until then we have removed the registration on this forum.
so far, i've searched up many ways to make an impact detector, but none of them work. code is down here. how to make a code run when a bullet hits a zombie.
ArrayList <Bullet> bullets = new ArrayList <Bullet> ();
PVector me, speed,zombiespeed,Bullet;
float maxSpeed = 2.5;
float zombiex;
float zombiey;
zombie zombie1;
zombie zombie2;
void setup() {
rectMode(CENTER);
size(600, 600);
me = new PVector();
speed = new PVector();
zombiespeed = new PVector();
Bullet = new PVector();
noCursor();
noStroke();
me.x = 300;
me.y = 300;
}
void draw(){
background(255);
//walls
if (me.x < 0){me.x = 0;}
if (me.x > width){me.x = width;}
if (me.y < 0){me.y = 0;}
if (me.y > height){me.y = height;}
//zombie movement
if (zombiex> me.x){zombiex-= random(1,2.5);}
if (zombiey< me.y){zombiey += random(1,2.5);}
if (zombiex< me.x){zombiex += random(1,2.5);}
if (zombiey > me.y){zombiey -= random(1,2.5);}
//player
me.add(speed);
fill(255, 0, 0);
ellipse(me.x, me.y, 20, 20);
//zombie
fill(0,150,50);
zombie1 = new zombie();
zombie2 = new zombie();
//mouse
PVector mouse = new PVector(mouseX, mouseY);
fill(0);
ellipse(mouse.x, mouse.y, 5, 5);
//shoot
if (frameCount%20==0 && mousePressed) {PVector dir = PVector.sub(mouse,me);
dir.normalize();
dir.mult(9);
Bullet b = new Bullet(me, dir);
bullets.add(b); }
//bullet movement
for (Bullet b : bullets) {b.bullup();
b.bulldis();}
}
//bullet class
class Bullet extends PVector { PVector vel;
Bullet(PVector loc, PVector vel) { super(loc.x, loc.y);
this.vel = vel; }
void bullup() {add(vel);}
void bulldis() {fill(0, 150, 55);
ellipse(x, y, 5, 5);}
}
class zombie{PVector pos;
zombie(PVector pos,PVector zom){super (zom.x,zom.y);
this.pos = pos;}
void zomup(){add(pos);}
void zomdis(){fill 0,150,50)
rect(x,y,25,25);}
}
}
//move
void keyPressed() {
if (key == 'w'){ speed.y = -maxSpeed; }
if (key == 's'){ speed.y = maxSpeed; }
if (key == 'a'){ speed.x = -maxSpeed; }
if (key == 'd'){ speed.x = maxSpeed; }
}
//stop
void keyReleased() {
if (key == 'w' || key == 's'){ speed.y = 0; }
if (key == 'a' || key == 'd'){ speed.x = 0; }
}
Answers
Please edit your post (gear icon in the top right corner of your post), select your code and hit ctrl+o to format it. Make sure there is an empty line above and below your code.
Kf
Please read the collision detection faq. Circle / circle collision specifically.
Basically check each bullet in a for loop against each zombie
This gives you a nested for loop- all bullets are checked against all zombies
For collision use if(dist(......
See reference
thanks christir, but I get don't know how to remove the zombie if it's hit.
In theory have in the class a boolean isAlive
Set it to false on collision
Display Zombie only when it's alive
The thing is that I don't know what the bullet's x and y position is, this program is not 100% made by me
and I don't know how to add multiple zombies, sorry if I seem like i'm really bad at processing
EDIT removed -- response to wrong post.
my current code is here
It works, but I have to be a certain distance to be able to "hit" him, and I still can't find a good solution to make the zombie dissapear
isAlive must be inside the class because it can be different for both zombies -- remove it from outside the class
In the class don't draw the zombie if it's not alive
Use ctrl-t in processing to auto-format, you get better indents
You wrote certain distance to hit him --- what do you mean? The bullet is checked via dist() against the zombie? Where? I think you forgot that.
Read my first post again.
I don't understand lines 8,9, 25
You need to check every bullet against every zombie in line 101 above
I think you are checking the distance in the constructor- this is wrong because this is at the start of the path the bullet is flying on.
Instead check it throughout the flight - line 101 above.
thank you, I am making an improved version
how do you check it throuout the flight?
Hello !
start by using ctrl-t in processing.
Better Auto-format
Woe, you haven't implemented your class Zombie fully.
Since you have line 8
zombie = new PVector();
.You are using this PVector too often. This leads to the situation that your 2 zombies appear always in the same spot.
Look at these lines from your constructor
here instead setting variables inside your class, you are setting a global variable PVector zombie. This leads to the situation that your 2 zombies appear always in the same spot.
Also look at your method
Here you are using PVector zombie. This leads to the situation that your 2 zombies appear always in the same spot.
Bad.
(similar to
PVector zombie
you still hadPVector Bullet
as global variables. You need to do house cleaning in your code more often to get rid of such artifacts of older versions)Also note that what you do with your zombie in draw you only do for one of them. To do this for both, move the code lines into the class Zombie (new methods in the class) and call them twice for each zombie - similar as you do for bullets.
Minimal improved, but still a lot to do for you:
But you will learn it quick, I am sure.
Best, Chrisir ;-)
you wrote
Basically check each bullet in a for loop against each zombie
This gives you a nested for loop-all bullets are checked against all zombies
For collision use if(dist(......
See reference
thank you!
I made an improved version of the game with your help
You should move the zombies in an ArrayList too
Can you show your entire code...?
Btw as fas as I see you don't remove bullets from the ArrayList- you should do so when they hit a zombie or when they leave the screen
Otherwise your list will be very long and slow things down
this is my current code, but I don't know how to make it so that the zombies die when they get hit, I try the dist function, it works, but I can't really get the bullet's location, when I put it in for(Bullet b: bullets)" I can't get the zombie's location help please
Why?
In draw you loop over the bullets
Here just insert
if( dist(b.x,b.y, zombie1.x, zombie1.y) < 33) zombie1.isalive=false;
in zomdis display it only when isalive is true
Show your attempt
Entire code
Thank you
the problem is I have multiple zombies, your code is what I used to do, but now I want more than a few zombies,so I have a few problems
I see
Sorry, I overlooked that
As I said:
As I said:
If you would have done this you would have seen that this:
has a wrong bracket.
Did you spot it? No, because you haven't used ctrl-t ....
Here is the code with ctrl.t
Bullets also should have a boolean isAlive
To remove bullets and zombies where isAlive is false from the ArrayLists you need to loop backward through arraylists
Multiple bullets example here: https://forum.processing.org/two/discussion/comment/92284/#Comment_92284
Kf
I fixed spme problems,although there still are more, here are a few of them:
when a zombie gets killed, I cant really remove it, it stops getting displayed, but its location is still there, i made it so that it sets itself to -1, but if you hit -1 with your bullets, you still get score
problem with health, I think that 1 health is not enough, especially because i'm making more than one type of zombie,the code looks like it would work, but it doesn't. thank you for helping!
Check this:
https://processing.org/reference/ArrayList.html
Now in draw(), replace:
with this:
Also, isAlive is easier to read than isalive.
Kf
Alternatively, simply alive is OK too! :P
what is the class rzombie?
rzombie is a red zombie, a new type of zombie i'm going to add later
Maybe not a good idea to start a new class
Can't you add color and other properties to the old class/ make different types of zombies there?
how do you add health?
and the zombies have different abilities, so I have to add a new class
you mean for the zombies?
You did that already.
health = 2;
etc. etc. .....ah, I see.
This is your mistake:
No
;
is allowed directly afterif(...)
.The
;
ends a command and so it says "if theif
-clause applies, execute everything betweenif(...)
and the;
" - which is nothing in this case. Bad.Delete the
;
and it will read: "if theif
-clause applies, execute everything after theif(...)
in the{.....}
block" - much better.maybe.
It's ok.
Permission granted. ;-)
Remark
Your code still is hard to read because you use no empty lines to separate sections.
I use e.g.
//-----
between sections of code (mostly multiple functions) and//======
before and after a class.Also empty lines between functions and between methods within a class and between sections within a function.
Here is my version:
much better, uh?
Your draw() is still too long.
You could make sub-functions here like
bulletManager()
zombieManager()
playerManager()
Best, Chrisir ;-)
thank you
you can use remove
that was not actually my problem
the problem was that the bullet kept touching the zombie and it gets checked twice, and the zombie loses 2 health. the solution is to remove the bullet whenever it gets used
Well done !
I have another problem, I want the red zombie to be able to place fire, but it says that add(fire) does not exist
I don't know why it keeps happening
never mind syntax error
fixed it it's add fires not add fire
i'm ending this discussion it's way too long
Some discussions continue on the next page even....
Fire is like bullets but with fire images?