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

2022/11/12

How can I remove a specific actor, depending on the location of it and another actor (shooting with penetration)?

1
2
danpost danpost

2022/11/22

#
Super_Hippo wrote...
I think it would be the best to reduce the health of both by the lower health so there is no need to loop.
That's not a bad way to go. Here is a quick coding (trying to keep it simple):
e1.e1Health -= this.bulletHP;
if (e1.e1Health <= 0)
{
    this.getWorld().removeObject(e1);
}
if (e1.e1Health < 0)
{
    this.bulletHP = -e1.e1Health;
}
else
{
    this.getWorld().removeObject(this);
}
As you can see, the checking of which value is lower is not done directly. It is determined by the result of the initial subtraction of the health of e1. Minimal checks; minimal code -- doing exactly what needs to be done.
Tidali Tidali

2022/11/22

#
IT WORKS THANKS SO MUCH IT HELPED ME OUT SO MUCH!!!!!!!!
Tidali Tidali

2022/11/23

#
Okay it works not perfectly, because as soon as my ball deals >= 4 damage, I oneshot any amount of e1?
public class ball extends Actor{
    private boolean toBeDeleted=false;
    private int bulletMoveSpeed=25;
    public int initialBulletHP=1,bulletHP;
    private int hitboxE1=40;
    int i=1;
    
    public void act()
    {   
        for(int i=1;i==1;i++){
            this.bulletHP=((MyWorld)getWorld()).buDa;
        }
        if(((MyWorld)getWorld()).wIP==false){
            move(bulletMoveSpeed);
        }
        if(this.isAtEdge()&&this!=null)this.toBeDeleted=true;
        e1 E1 = (e1) getOneIntersectingObject(e1.class);
        if(E1!=null){
            if(this.bulletHP>E1.e1Health){
                this.bulletHP -= E1.e1Health;
                E1.toBeDeleted=true;
            }else if(this.bulletHP<E1.e1Health){
                E1.e1Health -= this.bulletHP;
                this.bulletHP=0;
            }else if(this.bulletHP==E1.e1Health){
                E1.e1Health=0;
                this.bulletHP=0;
            }
        }   
        if(this.bulletHP<=0)this.toBeDeleted=true;
        if(this.toBeDeleted==true){ 
            getWorld().removeObject(this);
        }
    }
}
I am assuming this is the problem:
for(int i=1;i==1;i++){
      this.bulletHP=((MyWorld)getWorld()).buDa;
}
Is there a better way to set a variable to every instance itself?
Tidali Tidali

2022/11/23

#
Tidali wrote...
Okay it works not perfectly
Okay never mind I fixed it by replacing
for(int i=1;i==1;i++){
   this.bulletHP=((MyWorld)getWorld()).buDa;
}
with
if(i==1){
   this.bulletHP=((MyWorld)getWorld()).buDa;
   i++;
}
You need to login to post a reply.
1
2