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

2020/3/14

removing an actor after being hit by another actor for 3 times

bangtan bangtan

2020/3/14

#
what code will I use to check if the actor was hit 3 times and remove itself
danpost danpost

2020/3/14

#
You would use an int hitCount field:
private int hitCount;
When hit:
hitCount++;
if (hitCount == 3) getWorld().removeObject(this);
bangtan bangtan

2020/3/15

#
do I need to put some code in the shot
danpost danpost

2020/3/15

#
bangtan wrote...
do I need to put some code in the shot
Not as far as the actor being hit is concerned.
bangtan bangtan

2020/3/15

#
private int counter = 0 ;

public void count(){
Actor bullet = getOneObjectAtOffset(0,0, bullet.class);
        if (bullet != null) {
            counter = counter + 1;
            dead();
      }
    }

public void dead(){
if (counter == 3){
            getWorld().removeObject(this);
        }
    }
bangtan bangtan

2020/3/15

#
this is my code but the shot actor wasn't removed
danpost danpost

2020/3/15

#
bangtan wrote...
this is my code but the shot actor wasn't removed
Is this code in the class of the shot actor? Is the shot being removed?
bangtan bangtan

2020/3/15

#
that's the code of the object that will be dead yes, the shot is being removed
danpost danpost

2020/3/15

#
bangtan wrote...
that's the code of the object that will be dead yes, the shot is being removed
Remove code in class of shot that removes shot when hitting actor and have shot actor remove the shot when adjusting counter.
bangtan bangtan

2020/3/15

#
I made my code from this to this and the bullet won't remove after hitting the object anymore
private int counter = 0 ;
 
public void count(){
Actor bullet = getOneObjectAtOffset(0,0, bullet.class);
        if (bullet != null) {
            getWorld().removeObject(bullet);
            counter = counter + 1;
            dead();
      }
    }
 
public void dead(){
if (counter == 3){
            getWorld().removeObject(this);
        }
    }
danpost danpost

2020/3/15

#
Show complete class codes.
You need to login to post a reply.