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

2014/8/1

Collision Detection Issues

2
3
4
5
danpost danpost

2014/8/2

#
Maybe the value of 'isInvincible' should be governed by both whether intersecting a spacedog or a boss. That is, instead of two different collision methods (one for spacedog and one for boss -- where the second one will always determine the final value of 'isInvincible'), combine them into one method:
private void checkCollision() 
{
    Space space = (Space) getWorld();
    if (space == null) return;
    Actor a = getOneIntersectingObject(Boss.class);
    Actor b = getOneIntersectingObject(Spacedog.class);
    if (a != null || b != null) 
    {
        if (isInvincible) return;
        lives--;
        counter.loseLife();
        Greenfoot.playSound("catscreech2.wav");
        space.addObject(new Explosion(), getX(), getY());
        space.removeObject(this);
        if (lives <= 0)
        {
            space.addObject(new GameOver(), space.getWidth()/2, space.getHeight()/2);
        }
        else
        {
            space.addObject(new Rocket(), 65, 413);
            space.addObject(this, 65, 413);
            isInvincible = true;
            movement.setNeutral(); 
        }
    }
    else
    {
        isInvincible = false;
    }
}
NikZ NikZ

2014/8/2

#
Why don't you move the hero to a place where the boss can't get to (like the original platform idea)?
ddemarco06 ddemarco06

2014/8/2

#
OH MY GOD IT WORKS!!1!!
ddemarco06 ddemarco06

2014/8/2

#
@danpost you are a genius, sir @NikZ thanks so much for sticking with me and trying to help me figure it out. You both rock! I was just about ready to give up! My assignment is due Monday and I was just about ready to pass it in broken. No longer! Thanks for putting up with my ignorance! Many thanks!
danpost danpost

2014/8/2

#
Since, there is no need to have references to the intersecting objects (you just need to know whether any are intersecting or not), lines 5 through 7 above could be written this way:
if (isTouching(Boss.class) || isTouching(Spacedog.class))
NikZ NikZ

2014/8/2

#
I don't see space doing anything... Can't you use getWorld() (would save a line of code)?
danpost danpost

2014/8/2

#
NikZ wrote...
I don't see space doing anything... Can't you use getWorld() (would save a line of code)?
It is true that no methods or fields in the Space class are being utilized in the code and line 3 could be coded as:
World space = getWorld();
saving the world as a World object instead of a Space object. However, the line itself (getting a reference to the world in the local variable 'space', cast either way) is necessary as the world would be lost had that not have been done before removing 'this' from the world.
ddemarco06 ddemarco06

2014/8/18

#
Maybe you guys can help with one more thing while I'm abusing all of your time. My Boss character has a health bar that shows up when it appears. When you win, there is a "Congratulations! Retry? Y/N" screen. The problem is when you retry the game a second time (without resetting) the Boss is without his health because I am using referencing a health bar in my space world. You actually helped me build it a few days ago...http://www.greenfoot.org/topics/find/44061 I tried using a replenishHealth method that would replenish the health bar after it reaches 0 but would still remove the boss object but it didn't seem to work.
danpost danpost

2014/8/18

#
Maybe you should show what code you are using in your space world and your congratulations screen class.
You need to login to post a reply.
2
3
4
5