This site requires JavaScript, please enable it in your browser!
Greenfoot back
ddemarco06
ddemarco06 wrote ...

2014/8/1

Collision Detection Issues

1
2
3
4
5
ddemarco06 ddemarco06

2014/8/2

#
I also removed the isInvincible call in my act method completely if that matters.
NikZ NikZ

2014/8/2

#
How does this work?
    private void checkBossCollision() 
    {
        if (getWorld() == null) return;

        Actor a = getOneIntersectingObject(Boss.class);
        if (a != null) 
        {
            if (isInvincible) return;
            lives--;
            counter.loseLife();
            Greenfoot.playSound("catscreech2.wav");
            Space space = (Space) getWorld();
            space.addObject(new Explosion(), getX(), getY());
            space.removeObject(this);
            if (lives <= 0){
                GameOver gameover = new GameOver();
                space.addObject(gameover, space.getWidth()/2, space.getHeight()/2);
            }
            else {
                int x = Greenfoot.getRandomNumber(340);
                space.addObject(new Rocket(), 65, 413);

                space.addObject(this, 65, 413);
                isInvincible = true;
                movement.setNeutral(); 

                List<Boss> bosses = getNeighBours(something, true, Boss.class);
                for (int i = 0; i < bosses.size(); i ++) {
                    Boss boss = (Boss)bosses.get(i);
                    if (boss.getX() > getX()) {
                        boss.setLocation(boss.getX() + something, boss.getY());
                    }
                    else {
                        boss.setLocation(boss.getX() - something, boss.getY());
                    }
                    if (boss.getY() > getY()) {
                        boss.setLocation(boss.getX(), boss.getY() + something);
                    }
                    else {
                        boss.setLocation(boss.getX(), boss.getY() - something);
                    }
                }
            }
        }
        else
        {
            isInvincible = false;
        }
NikZ NikZ

2014/8/2

#
Replace "something" with your own numbers.
ddemarco06 ddemarco06

2014/8/2

#
It says "cannot find symbol - method getNeighbors(int, boolean, java.lang.Class<Boss>)
    private void checkBossCollision() 
    {
        if (getWorld() == null) return;
        
        Actor a = getOneIntersectingObject(Boss.class);
        if (a != null) 
        {
                if (isInvincible) return;
                lives--;
                counter.loseLife();
                Greenfoot.playSound("catscreech2.wav");
                Space space = (Space) getWorld();
                space.addObject(new Explosion(), getX(), getY());
                space.removeObject(this);
                if (lives <= 0){
                        GameOver gameover = new GameOver();
                        space.addObject(gameover, space.getWidth()/2, space.getHeight()/2);
                    }
                    else {
                int x = Greenfoot.getRandomNumber(340);
                space.addObject(new Rocket(), 65, 413);
                
                space.addObject(this, 65, 413);
                isInvincible = true;
                movement.setNeutral(); 
                 List<Boss> bosses = getNeighbors(25, true, Boss.class);  
            for (int i = 0; i < bosses.size(); i ++) {  
                Boss boss = (Boss)bosses.get(i);  
                if (boss.getX() > getX()) {  
                    boss.setLocation(boss.getX() + 25, boss.getY());  
                }  
                else {  
                    boss.setLocation(boss.getX() - 25, boss.getY());  
                }  
                if (boss.getY() > getY()) {  
                    boss.setLocation(boss.getX(), boss.getY() + 25);  
                }  
                else {  
                    boss.setLocation(boss.getX(), boss.getY() - 25);  
                }  
            }  
        }  
            
    }
        else
        {
            isInvincible = false;
        }
    }
NikZ NikZ

2014/8/2

#
It's getNeighbours -- with an u.
ddemarco06 ddemarco06

2014/8/2

#
Sorry I'm a dumb American. It compiled okay but didn't seem to work.
NikZ NikZ

2014/8/2

#
Hmm, is it giving run time errors?
ddemarco06 ddemarco06

2014/8/2

#
No errors. Same thing, if I get hit once on the left side of the world and respawn on the enemy I die over and over until all my lives are gone.
NikZ NikZ

2014/8/2

#
How about putting the for loop before adding itself into the world? Is it possible?
ddemarco06 ddemarco06

2014/8/2

#
That time I got the runtime error. Actor not in the world.
NikZ NikZ

2014/8/2

#
Ah. How about you don't remove the hero and set its transparency to 0?
ddemarco06 ddemarco06

2014/8/2

#
Ha, that didn't work. It just kept losing lives until it was at -1000 lives then I stopped it.
NikZ NikZ

2014/8/2

#
That's because the boss is still touching it, I think. How about making isInvincible true right after the hero senses it's dead?
ddemarco06 ddemarco06

2014/8/2

#
    private void checkBossCollision() 
    {
        if (getWorld() == null) return;
        
        Actor a = getOneIntersectingObject(Boss.class);
        if (a != null) 
        {
                if (isInvincible) return;
                lives--;
                counter.loseLife();
                Greenfoot.playSound("catscreech2.wav");
                Space space = (Space) getWorld();
                space.addObject(new Explosion(), getX(), getY());
                getImage().setTransparency(0); 
                isInvincible = true;
                if (lives <= 0){
                        GameOver gameover = new GameOver();
                        space.addObject(gameover, space.getWidth()/2, space.getHeight()/2);
                    }
                    else {
                int x = Greenfoot.getRandomNumber(340);
                space.addObject(new Rocket(), 65, 413);
                getImage().setTransparency(255);  

                movement.setNeutral(); 
                 List<Boss> bosses = getNeighbours(25, true, Boss.class);  
            for (int i = 0; i < bosses.size(); i ++) {  
                Boss boss = (Boss)bosses.get(i);  
                if (boss.getX() > getX()) {  
                    boss.setLocation(boss.getX() + 25, boss.getY());  
                }  
                else {  
                    boss.setLocation(boss.getX() - 25, boss.getY());  
                }  
                if (boss.getY() > getY()) {  
                    boss.setLocation(boss.getX(), boss.getY() + 25);  
                }  
                else {  
                    boss.setLocation(boss.getX(), boss.getY() - 25);  
                }  
            }  
        }  
            
    }
        else
        {
            isInvincible = false;
        }
    }
ddemarco06 ddemarco06

2014/8/2

#
This does the same thing as previous.
There are more replies on the next page.
1
2
3
4
5