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

2015/11/7

Unable to add enemies once enemy gets killed

pintoo pintoo

2015/11/7

#
As I scroll up , i generate insects after a time delay. Once the hero shoots the enemy, enemy is removed.But after that i am unable to generate insects.Also when the hero collides with insects there is a NullPointerException. public class Insect extends Generator { int counter = 0; public void act() { checkHealth(); move(); } public void createEnemy(Generator g,World w) { System.out.println("The insect is created..."); w.addObject(g, Greenfoot.getRandomNumber(50), Greenfoot.getRandomNumber(100)); move(); } public void checkHealth() { if((getWorld().getObjects(Insect.class)) == null) { System.out.println("Insects are not present"+ getWorld().getObjects(Insect.class)); getWorld().addObject(new Insect(),Greenfoot.getRandomNumber(50),Greenfoot.getRandomNumber(100)); } } public void move() { if(((doodleWorld) getWorld()).scroll==true) { System.out.println("insect is moving"); removeRecreate(); } if(((doodleWorld) getWorld()).fall==true) { System.out.println("Falling"); } if( this.getOneIntersectingObject(ammo.class) != null) { System.out.println("Removing the insect" + this.getOneIntersectingObject(ammo.class)); getWorld().removeObject(this); } else if(this.getOneIntersectingObject(doodler.class) != null) { System.out.println("Removing the doodler" + this.getOneIntersectingObject(ammo.class)); this.removeTouching(doodler.class); getWorld().removeObject(this); checkHealth(); } } public void removeRecreate() { counter ++; System.out.println(counter); if (counter == 200) { System.out.println("Recreating...."); getWorld().addObject(new Insect(),Greenfoot.getRandomNumber(50),Greenfoot.getRandomNumber(100)); this.turnTowards(doodler.currentX,doodler.currentY); this.move(1); counter = 0; } } }
danpost danpost

2015/11/7

#
You should post the error message (the entire terminal error printout produced by this error). Also, you need to use 'code' tags when posting code (see the 'Posting code? read this!' link below the reply box) -- and then set proper the indentation and post the entire class (including 'import' statements at the beginning).
pintoo pintoo

2015/11/7

#
/**
 * Write a description of class Insect here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import greenfoot.*; 
//import java.awt.*;
import java.util.*;
import java.awt.Color;
public class Insect extends Generator
{
    // instance variables - replace the example below with your own
    boolean removed;
    GreenfootImage myImage;
    World myWorld = getWorld();
    
      int counter = 0;  
      
   public void act()
   {    
      checkHealth();
       move();
      if (removed){return;}
      
   }
   
   
   public void createEnemy(Generator g,World w)
   {
       System.out.println("The insect is created...");
       w.addObject(g, Greenfoot.getRandomNumber(50), Greenfoot.getRandomNumber(100));
        move();
       checkHealth();
        removed = false;
    }
    public void checkHealth()
    {   
    
        if((removed == true))
        {
           System.out.println("Insects are not present");
           //getWorld().addObject(new Insect(), Greenfoot.getRandomNumber(50), Greenfoot.getRandomNumber(100));
        }
        
    }
   public void move()
   {
       if(((doodleWorld) getWorld()).scroll==true)
        {
            System.out.println("insect is moving");
            removeRecreate();
        }
             

        if(((doodleWorld) getWorld()).fall==true)
        {
           System.out.println("Falling");
        }
        
       

        if( this.getOneIntersectingObject(ammo.class) != null)
        {
            
            System.out.println("Removing the insect" + this.getOneIntersectingObject(ammo.class));
            //this.setLocation(0,-200);
            getWorld().removeObject(this);
            // System.out.println(getWorld().getObjects(Insect.class));
            removed = true;
             checkHealth();
            
        }
                
       else if(this.getOneIntersectingObject(doodler.class) != null)
       {
           
           System.out.println("Removing the doodler" + this.getOneIntersectingObject(ammo.class));
            
            
            checkHealth();
            
        } 
        
    }
    
      public void removeRecreate()
      {
          
          counter ++;
          System.out.println(counter);
        if (counter == 100)
        { 
            System.out.println("Recreating....");
        getWorld().addObject(new Insect(),Greenfoot.getRandomNumber(50),Greenfoot.getRandomNumber(100));
        this.turnTowards(doodler.currentX,doodler.currentY);
        this.move(1);
        counter = 0;
        
    }
    
       
    }
}   
    
            
     //Removing and recreating the enemies
    
        
   
//    public void shoot()
//    {
//        if(((doodleWorld) getWorld()).scroll==true)
//         {
//            getWorld().addObject(new ammo(), getX(), getY());
//            setLocation(getX()+90,getY());
// 
//         }
//     }
pintoo pintoo

2015/11/7

#
No such error.I added Insects at the beginning but once they are killed(when Hero shoot the Insects) I remove them the world. But generation of Insects needs to be continuous and should not stop.
danpost danpost

2015/11/7

#
I am a bit confused as to what an Insect class object is. It appears to be an Actor object in the world that eats whatever doodlers are and can be killed by ammo objects. But then, you seem to want it to spawn other Insect objects. The spawning of insects should be in your subclass of World if you want that to be continuous (the act method of the Insect class is only executed as long as an Insect object is in the world and each cycle there are insects in the world the act method will execute once for each one of them that remain in the world). What code do you have in the Generation class?
pintoo pintoo

2015/11/7

#
Sorry for the confusion.I was not clear in the first place. Used the scenario http://www.greenfoot.org/scenarios/2089. Just trying to generate enemies(Insects in my case) continuously even when the doodler kills the enemy.
pintoo pintoo

2015/11/7

#
Thanks a lot dan . I got the code working after a little change
You need to login to post a reply.