We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm making a game where the enemies attack each other, how do I do that?
ArrayList <enemy> enemies = new ArrayList <enemy> ();
ArrayList <wall> walls = new ArrayList <wall> ();
void setup() {
size(1000, 700);
frameRate(60);
}
void draw() {
background(0);
enemy e = new enemy();
if (frameCount %100==0) {
enemies.add(e);
}
for (int i = enemies.size() - 1; i >= 0; i-=1) {
enemy zz = enemies.get(i);
if (zz.isalive==false) {
enemies.remove(i);
} else {
zz.display();
zz.move(e);
}
}
}
//================================================================
class enemy {
boolean isalive = true, detected = false;
float x = -10000, y = -10000, size = 20,lock = -1,identity = random(99999);
String names = "hi";
enemy() {
x = random(width);
y = random(height);
}
void display() {
fill(200, 0, 0);
rect(x, y, size, size);
}
void move(enemy other) {
if (dist(other.x, other.y, x, y)<other.size + size + 100) {
detected = true;
lock = other.identity;
}
if (other.isalive ==false) {
detected = false;
lock = -1;
}
if (detected && other.identity == lock) {
if (x > other.x) {
x -= 5;
}
if (x < other.x) {
x += 5;
}
if (y > other.y) {
y -= 5;
}
if (y < other.y) {
y += 5;
}
}
if (dist(x,y,other.x,other.y)<20){
other.isalive = true;
}
}
}
//==============================================================
class me {
}
//============================================================
class wall {
}
Answers
In draw you want a nested for loop
This means two for loops the outer is A the inner B
A is looping over all enemies and B is looping over all other enemies
But avoid that each pair is checked twice
I suggest
for (enemy_i=0.......
for (other_i=enemy_i+1......
Thus you only check the other enemies above the enemy for loop A
Then say something like
enemies.get(enemy_i).move( enemies.get(other_i) ) ;
code please?
I gave you a lot of code- show your attempt
I am also not at home and can't do it here
https://forum.Processing.org/two/discussion/20124/collision-between-objects-in-classes
Line 16 : you want to start at a=i-1
to avoid double checking of pairs
This
same as
The ! means not
Does it work?
Well done!
Remember to use ctrl-t in processing to auto format your code
class names like Wall are written with capital first letter Wall, not wall
Objects like enemies small first letter
it's okay, but once the enemy kills the other enemy, it doesn't do anything, i'm guessing that it has no other enemies in its move. here is my current code:
http://Studio.ProcessingTogether.com/sp/pad/export/ro.9K-kjhuONcJDZ
there is a problem, the enemies go too fast, and they change speed, i dont know why that's happening
this code works a little bit better, none of the enemies are idle, but there are still 2 problems, the speed differs when there are more enemies, and one of the enemies manage to kill all of the other enemies wothout losing. I don't know why that happens, please help, there is also a minor problem, the squares get blurry, I don't know why. thanks!
what are these bits doing?
what remainder do you get when you divide anything by 1?
I don't know, I thought it would help scince the move function is in a loop
The rest of division by 1 is always 0
So this if clause doesn't work
okay then
can someone help me woth the speed? it changes for some reason
anybody?
Which lines should we look at...?
they moved so fast because each enemy was checking its position against all others and every time (meaning like 20 times in one draw!) adding a value to x or y (either randir or espeed).
To change this, I changed the nested (double for loop) in draw().
I also made two different methods in the class enemy, one is for determine the values for espeedX and espeedY, one is for adding it to x and y. Since the latter is called only once, we only add speed once (and not like 20 times). The former is still run 20 times but this means that only the last other enemy determines the direction (espeedX and espeedY). Alternatively you could add up all those 20 directions and take the average.
But the question arises of courses which behavior you want? Follow the closest enemy or .... ?
Chrisir
what you did was basically remove the follow or move function, and thus the enemies rely on the random movement,that is why none of them actually follow anybody, and also why when I commented out the random movements they did not move. but I get what you're trying to do... hopefully I can improve it:)
I know why, but I don't know how to fix it. the problem's because the're in a double loop, your's worked because the move function was not in a nested loop
it moves once every time the a loop gets activated
fixed! I had to add a boolean that checks if it has moved.. you will see all that in the code. does need some improvements though... like the wall function, I don't know how to make it so that the enemies keep moving after they hit the wall(as you can see I commented it out).
I kind of fixed it, but its stull a little buggy... definitely better than before
Well done!
okay..
thanks!
Question: Why do enemies kill each other?
Is that a game or just a play of enemies you can look at?
But the question arises of course which behavior you want?
It would be best if one enemy only follows the closest enemy I think.
When they touch, both should die, no?
nah, the enemies have a 50/50 chance of winning when they touch and yes, I want them to follow the closest enemy
Thank you!