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

2018/3/31

Need help with shooting things

Xonthrias Xonthrias

2018/3/31

#
I have a problem where if I have an actor called Marine shoot another actor called the Hydralisk who has the function to remove the marine when he reaches him it will give me all kinds of java error when the bullet reaches the Hydralisk , but if i remove the removeObject method of the Hydralisk when he reaches the marine it works just fine. Can i get some help? Here is the code: public void act() { move(); DamageTake(); DamageCause(); } public void move() { move(-2); } public void DamageTake() { Actor hydralisk = getOneIntersectingObject(Bullet.class); if(hydralisk != null) { World world; world = getWorld(); world.removeObject(this); } } public void DamageCause() { Actor marine; marine = getOneIntersectingObject(Marine.class); if(marine != null) { getWorld().removeObjects(getWorld().getObjects(Marine.class)); } } }
danpost danpost

2018/3/31

#
Switch the two lines -- damageTake and damageCause so that you try not to check for world things after removing the actor from the world.
Xonthrias Xonthrias

2018/4/1

#
Mean like this ?
 public void DamageCause()
    {
        Actor marine;
        marine = getOneIntersectingObject(Marine.class);
        if(marine != null)
        {
          getWorld().removeObjects(getWorld().getObjects(Marine.class));
        }
    }
     public void DamageTake()
    {
        
    
        Actor hydralisk = getOneIntersectingObject(Bullet.class);
        if(hydralisk != null)
        {
            World world;
            world = getWorld();
            world.removeObject(this);
            
        }
    }
danpost danpost

2018/4/1

#
Xonthrias wrote...
Mean like this ? << Code Omitted >>
No -- in the act method (I wrote lines, not methods).
Xonthrias Xonthrias

2018/4/1

#
Works now ty very much :)
You need to login to post a reply.