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

#
Here is my act method: public void act() { move(); checkKeys(); checkCollision(); checkBossCollision(); reloadDelayCount++; if(delay == 10) { isInvincible = false; } else { delay++; } } And the collision method: private void checkBossCollision() { if (getWorld() == null) return; Actor a = getOneIntersectingObject(Boss.class); if (a != null) { if (isInvincible=false) { int x = Greenfoot.getRandomNumber(340); lives--; if (lives <= 0){ Space space = (Space) getWorld(); space.addObject(new Explosion(), getX(), getY()); space.removeObject(this); GameOver gameover = new GameOver(); space.addObject(gameover, space.getWidth()/2, space.getHeight()/2); } counter.loseLife(); Greenfoot.playSound("catscreech2.wav"); Space space = (Space) getWorld(); space.addObject(new Explosion(), getX(), getY()); space.removeObject(this); isInvincible = true; space.addObject(new Rocket(), 65, x); space.addObject(this, 65, x); movement.setNeutral(); } } } At the top I put the private int delay = 0; But now the collision method doesn't seem to be working at all?
NikZ NikZ

2014/8/2

#
Oh yes, when you reset isInvinsible, make sure that you reset delay, too.
NikZ NikZ

2014/8/2

#
isInvinsible=false should be instead !isInvinsible or isInvinsible == false (double equals because you are comparing two values, I believe).
ddemarco06 ddemarco06

2014/8/2

#
private void checkBossCollision() 
    {
        if (getWorld() == null) return;
        
        Actor a = getOneIntersectingObject(Boss.class);
        if (a != null) 
        {
                if (!isInvincible)
                {
                int x = Greenfoot.getRandomNumber(340);
                lives--;
                    if (lives <= 0){
                        Space space = (Space) getWorld();
                        space.addObject(new Explosion(), getX(), getY());
                        space.removeObject(this);
                        GameOver gameover = new GameOver();
                        space.addObject(gameover, space.getWidth()/2, space.getHeight()/2);
                    }
                counter.loseLife();
                Greenfoot.playSound("catscreech2.wav");
                Space space = (Space) getWorld();
                space.addObject(new Explosion(), getX(), getY());
                space.removeObject(this);
                space.addObject(new Rocket(), 65, x);
                space.addObject(this, 65, x);
                isInvincible = true;
                int delay = 0;
                movement.setNeutral(); 
            }
            }
I am getting a run time error. Actor not in this world.
danpost danpost

2014/8/2

#
I meant somewhere after the decrementing of lives, not necessarily immediately after it. Also some adjustments in the code will need to be done. Any duplicate code inside and outside the 'if' block should be done outside the 'if' block only and an 'else' will need added to separate those things that should be done when 'lives' is not less than or equal to zero. Something like the following:
private void checkBossCollision() 
{
    Space space = (Space) getWorld();
    if (space == null) return;
    Actor a = getOneIntersectingObject(Boss.class);
    if (a != 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
        {
            int x = Greenfoot.getRandomNumber(340);
            space.addObject(new Rocket(), 65, x);
            space.addObject(this, 65, x);
            isInvincible = true;
            int delay = 0;
            movement.setNeutral(); 
        }
    }
    else
    {
        isInvincible = false;
    }
}
NikZ NikZ

2014/8/2

#
Ah, do you want a random x or a random y? Your First and second posts with code suggest x, but your latest suggests y.
NikZ NikZ

2014/8/2

#
ddemarco06 wrote...
if(delay == 10) { isInvincible = false; } else { delay++; }
I notice delay is running even if isInvinsible is false. In later stages, the delay can be shorter and your hero might die like before. Use:
if (isInvinsible) {
    if(delay == 10) {  
        isInvincible = false;  
    }  
    else {  
        delay++;  
    }  
}
NikZ NikZ

2014/8/2

#
Line 3 should have delay = 0; after, sorry.
ddemarco06 ddemarco06

2014/8/2

#
Everything compiles OK with no error, but the collision detection doesn't seem to be working anymore, now the Boss doesn't harm the Hero at all.
    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){
                        space.addObject(new Explosion(), getX(), getY());
                        space.removeObject(this);
                        GameOver gameover = new GameOver();
                        space.addObject(gameover, space.getWidth()/2, space.getHeight()/2);
                    }
                    else {
                int x = Greenfoot.getRandomNumber(340);
                space.addObject(new Rocket(), 65, x);
                space.addObject(this, 65, x);
                isInvincible = true;
                int delay = 0;
                movement.setNeutral(); 
            }
        }
        else
        {
            isInvincible = false;
        }
    }
NikZ NikZ

2014/8/2

#
What is isInvincible's initial value?
ddemarco06 ddemarco06

2014/8/2

#
Like this?
if (isInvincible) {  
    if(delay == 10) {    
        isInvincible = false; 
        delay = 0;
    }    
    else {    
        delay++;    
    }    
}  
isInvicible's initial value is false. I used a random X to make my Hero spawn on randomly on the left side of the screen, same y coordinate though. I thought if he didn't respawn in the same place it might fix the issue but it didn't
danpost danpost

2014/8/2

#
My line 8 is not what you have as line 8. And my line 24 can be removed ('int delay = 0;'). Also, your lines 13 and 14 do not need to be duplicated with the 'if' block.
ddemarco06 ddemarco06

2014/8/2

#
Hmm. Collision is working now, but the invincibility is not. Here's all my code: boolean isInvincible = false;
  public void act()
    {
        move();
        checkKeys();
        checkCollision();
        checkBossCollision();
        reloadDelayCount++;
                if(delay == 10) 
                {  
                    isInvincible = false; 
                    delay = 0;
                }  
                    else {  
                        delay++;  
                    }  
        }
And the collision method:
    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, x);
                space.addObject(this, 65, x);
                isInvincible = true;
                movement.setNeutral(); 
            }
        }
        else
        {
            isInvincible = false;
        }
    }
danpost danpost

2014/8/2

#
Remove the 'delay' field and lines 8 through 15 of your act method.
ddemarco06 ddemarco06

2014/8/2

#
Wow! Bravo! It's not perfect but it works MUCH better now. Thank you both for all your help. One final question, if I want to set transparency while invincible. I have a method
    private void makeTransparent(boolean isInvincible)
    {
        if (isInvincible)
        {
         getImage().setTransparency(175);   
        }
    }
Do I need to put a call to this method anywhere?
There are more replies on the next page.
1
2
3
4
5