1

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...)

JTsusan
  • 11
  • 2
  • 1
    You need to define what "Does not work" means. – smac89 Aug 27 '18 at 16:04
  • Code looks equivalent - are you sure getCharRec() is working right? If it isn't returning the right rectangle it would explain your issue. – Elemental Aug 27 '18 at 16:26
  • I thought that might be the case but I checked it out a few times and it is returning the right rectangle so I remain quite stumped and hence why I am asking here. – JTsusan Aug 27 '18 at 16:31

0 Answers0