I am currently working on a school project where I am creating a side scrolling game. I am at the stage where I require collision detection. I would like my character sprite's visibility to be set to false when it intersects the evil character sprite.
Rectangle d = character.getCharRec();
for (EvilCharacter eChar1 : eChar) {
EvilCharacter m = (EvilCharacter) eChar1;
Rectangle wolfRec = m.getEvilCharRec();
if (d.intersects(wolfRec)) {
System.out.println("WASTED");
character.setAlive(false);
gameOver = true;
}
}
The above code unfortunately does not work (when the character sprite intersects the evil character sprite nothing happens) but strangely enough the code below does (when the rock sprite intersects the evil character sprite both sprites visibility's are set to false). If anyone is able to provide assistance it would be much appreciated.
ArrayList rocks = character.getRocks();
for (Object rock : rocks) {
Rock t = (Rock) rock;
Rectangle t1 = t.getRockRec();
for (EvilCharacter eChar1 : eChar) {
EvilCharacter m = (EvilCharacter) eChar1;
Rectangle wolfRec = m.getEvilCharRec();
if (t1.intersects(wolfRec) && m.Living()) {
t.setVisible(false);
m.setDead(false);
score = score + 10;
}
}
}
(Apologies for the simplistic coding, we don't go over complex or efficient coding in school all that much...)