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
NikZ NikZ

2014/8/2

#
You don't need the Boolean isInvincible in the (). isInvincible should be private. Why don't you put it in act()?
NikZ NikZ

2014/8/2

#
What is the function that isn't perfect?
ddemarco06 ddemarco06

2014/8/2

#
On second thought, the invincibility still doesn't really work. Sometimes, but I'm still losing all my lives if I'm killed at the edge.
ddemarco06 ddemarco06

2014/8/2

#
Same one, it kind of works, but not really.
    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(); 
            }
        }
        else
        {
            isInvincible = false;
        }
    }
NikZ NikZ

2014/8/2

#
If I'm right, if the hero is not intersecting the boss, isInvincible is false?
ddemarco06 ddemarco06

2014/8/2

#
Yes, i have boolean isInvincible = false; at the top of my class.
NikZ NikZ

2014/8/2

#
NikZ wrote...
If I'm right, if the hero is not intersecting the boss, isInvincible is false?
This applies to lines 27 - 30.
ddemarco06 ddemarco06

2014/8/2

#
I don't know, I am very new to this and perhaps this kind of code is a bit advanced for me. Alternately, I'm trying to make my Hero respawn in the same place (65, 413) and making the enemies repel from this area in the world, but I can't get that to work either. I am pretty stumped right now. /** * Avoid corner where hero spawns. */ public void avoidCorner() { if (getX() <= 60 && getY() >=400) { setLocation(getX()+50, getY()-50); } }
NikZ NikZ

2014/8/2

#
How about moving the boss from hero?
NikZ NikZ

2014/8/2

#
You can use getNeighbours() to check nearby bosses.
ddemarco06 ddemarco06

2014/8/2

#
Wouldn't that move the Boss every time it intersects with the Hero?
NikZ NikZ

2014/8/2

#
Oh, only do that when the hero reappears, so it happens once.
ddemarco06 ddemarco06

2014/8/2

#
I'm sorry I'm not sure how I would do that.
danpost danpost

2014/8/2

#
ddemarco06 wrote...
On second thought, the invincibility still doesn't really work. Sometimes, but I'm still losing all my lives if I'm killed at the edge.
You must have code elsewhere that is subtracting lives if this is happening. The code at present should only subtract one live, then not subtract another until the hero is off the boss and then contacts it again. If you have any code in your Boss class that detects the Hero, then it should be removed.
ddemarco06 ddemarco06

2014/8/2

#
I don't have any other code that references lives except what's below. I have two types of enemies, spacedogs and Boss. If my Hero (it is a cat) respawns in a place where the enemy currently is, it will lose all its lives.
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(); 
            }
        }
        else
        {
            isInvincible = false;
        }
    }
here is the code for the spacedogs
    private void checkCollision() 
    {
          if (getWorld() == null) return;
        
        Actor a = getOneIntersectingObject(Spacedog.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(); 
            }
        }
        else
        {
            isInvincible = false;
        }
    }
There are more replies on the next page.
1
2
3
4
5