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

2015/2/27

Help spawning into world of selected stages

1
2
RagingAsian RagingAsian

2015/2/27

#
I need help getting my Creeps spawning into my world. I have tried a lot of things and cannot figure it out. here is the code: Creepers super Class:
public static int type;
    int[]WayX;
    int[]WayY;
    int index=0;
    double distance=150;
    private int max=20;
    private int spawn=0;
    private static int hp;
    private static int speed;
    static Counter counter;
    
    public Creepers(int hp,int speed)
    {
        this.hp=hp;
        this.speed=speed;
        setRotation(90);        
        if (IntroScreen.level==3)
        {
            setRotation(180);
        }
        
        
        
    }
    
    public void act() 
    {
        spawn(hp,speed);
        Waypoints();
    }  
    
    public void Waypoints()
    {

        if(IntroScreen.level == 1)
        {
           
            
           WayX= new int[]{94,94,188,188,281,281,374,374,467,467,563,563,599};
           WayY= new int[]{108,223,223,28,28,372,372,83,83,314,314,140,140};
           move();
        }else if (IntroScreen.level == 2){
           WayX= new int[] {478,487,272,272,599};
           WayY= new int[]{171,380,380,19,19};
           move();
        }else if (IntroScreen.level == 3){
           WayX=new int[] {355,246,246,463,463,134,134,572,572,27,27};
           WayY=new int[] {248,248,115,115,314,314,49,49,381,381,10};
           move();
        }else if (IntroScreen.level == 4){
           WayX= new int[]{319,319,56,56,431,431,525,525,599};
           WayY= new int[]{376,21,21,288,288,21,21,375,375};
           move();
        }
    }
    
    public void spawn(int hp, int speed)
    {
        Creepers creeper = new Creepers(hp,speed);       
        int random= (int)((Math.random()*3));
        random=type;
          
        if( getWorld().setBackground(Stage1.class) )
        {
           if(random==1)getWorld().addObject(creeper,0,108);
           if(random==2)getWorld().addObject(creeper,0,108);
           if(random==3)getWorld().addObject(creeper,0,108);
        }
        if (IntroScreen.level ==2)
        {
           if(random==1)getWorld().addObject(creeper,0,171);
           if(random==2)getWorld().addObject(creeper,0,171);
           if(random==3)getWorld().addObject(creeper,0,171);
        }
        if (IntroScreen.level == 3)
        {
           if(random==1)getWorld().addObject(creeper,0,248);
           if(random==2)getWorld().addObject(creeper,0,248);
           if(random==3)getWorld().addObject(creeper,0,248);
        }
        if (IntroScreen.level == 4)
        {
           if(random==1)getWorld().addObject(creeper,0,376);
           if(random==2)getWorld().addObject(creeper,0,376);
           if(random==3)getWorld().addObject(creeper,0,376);           
        }
        System.out.println(random);
    }
    
    public void move()
    {
        move(1);        
        double theDistance = (int)(Math.hypot(WayX[index] - getX(), WayY[index] - getY()));   
        if (theDistance < .01  )
        {
            
             for (int i=0;i<theDistance;i++)
             {
                 distance=theDistance;
                 index++;
             }
             
                  
        } 
        turnTowards(WayX[index], WayY[index]);
        if(count>=425&&spawn>=445)
        {
            getWorld().removeObject(this);
        }
    }
    
    //     public void death()
    //     {
    //         World world = getWorld();
    //         if ((getWorld!=null)&&())
    //         {
    //             
    //         }
    //     }
    
 }
danpost danpost

2015/2/27

#
You do not, I repeat, you do not want to create new Creeper objects from the Creeper class. If you ever get a Creeper object to spawn into the world, the number of them will increase exponentially and eventually throw a JavaHeapSpaceException error (this will happen in a relatively short amount of time). The reason is this: if you have no Creeper objects in the world, then the act method of the Creeper class has no reason to execute and no Creeper will be added into the world; if you have one or more Creepers in the world, then the act method will execute for each one, and the number of Creeper objects in the world will double every act cycle. You should have your World subclass act method execute that action.
RagingAsian RagingAsian

2015/3/3

#
I know what your talking about, but I am a having trouble writing the code. Should I post my code for the World?
danpost danpost

2015/3/3

#
RagingAsian wrote...
I know what your talking about, but I am a having trouble writing the code. Should I post my code for the World?
Not just what you have in your World; but, also what you tried. By showing your attempt, it gives a better idea of what you want to accomplish and the fixing of it will help you to not make the same mistakes over and over again.
RagingAsian RagingAsian

2015/3/3

#
This is my attempt so far. World Stage 1 Code:
    public void act(int hp,int speed)
    {
        spawn(hp,speed);
    }
    
    public void spawn(int hp, int speed)
    {
        Creepers creeper = new Creepers(hp,speed);       
        int random= (int)((Math.random()*3));
        random=Creepers.type;
          
        
           if(random==1)addObject(creeper,0,108);
           if(random==2)addObject(creeper,0,108);
           if(random==3)addObject(creeper,0,108);
        
    }
RagingAsian RagingAsian

2015/3/3

#
i didn't post the prepare method if thats alright.
RagingAsian RagingAsian

2015/3/3

#
So, the creepers wont spawn into the world, but when i manually input them, an error immediately pops up. And when I comment out the code of error, they will not move on to the next waypoint.
fejfo fejfo

2015/3/3

#
please show the error message
fejfo fejfo

2015/3/3

#
and if the new code is in creeper Creepers still spawn it self I would make a Creeper spawner with makes creepers (the creeper spawner can be an inviseble object)
RagingAsian RagingAsian

2015/3/3

#
IT would be error messages like this with other classes as well(Creepers and Tower Classes): java.lang.NullPointerException at Towers.turnClosest(Towers.java:56) at Towers.Act(Towers.java:36) at Bomb.act(Bomb.java:23) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
fejfo fejfo

2015/3/3

#
the error happens in Tower so u need to show that code to ( in turnClosest
RagingAsian RagingAsian

2015/3/3

#
I already posted the Creeper super class earlier. Here's the Tower's super class as well as one subclass of each one: Towers Super Class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
import java.util.List;
/**
 * Write a description of class Towers here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Towers extends Actor
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    private boolean placed = false;
    
    public static int kind;
    private List<Creepers> enemies;
    private Actor closestEnemy;
    private static int damage;
    private static int fireRate;
    private static int range; 
    int count=0; 
    private int canShoot;
    int posX = 10;
    int posY= 10;
    public Towers(int dmg, int rng, int fr)
    {        
        this.damage=dmg;
        this.range=rng;
        this.fireRate=fr;
    }
    
    public void Act()
    {        
        enemies = getObjectsInRange(100,Creepers.class);
        checkPlaced();
        turnClosest(enemies);
    }
    
    public void turnClosest(List<Creepers>enemies)
    {
        Actor closest = null;
        int distance=100;
        if(enemies.size()>0)
        {
            for(int i=0;i<enemies.size();i++)
            {
                Actor creep = enemies.get(i);
                range=(int)(Math.hypot(creep.getX() - getX(), creep.getY()-getY()));
                if((range<distance))
                {
                    closest=creep;
                    distance=range;
                    
                }
            }
            System.out.println("x = " + closest.getX() + "    (//X axis)" );
            int targetX=closest.getX();
            int targetY=closest.getY();
            closestEnemy = closest;
            turnTowards(targetX,targetY);
            
        }
    }
    
    public void checkPlaced()
    {
        
        if (!placed)
        {
            mouse = Greenfoot.getMouseInfo();
         if (mouse != null)
         {
             posY=mouse.getY();
             posX=mouse.getX();
             setLocation (posX, posY);
             if(Greenfoot.isKeyDown("esc")) getWorld().removeObject(this);           
         }
         if (Greenfoot.mouseClicked(this))
         {
            setLocation(posX,posY);
            placed=true;          
           
              
         }            
        }       
    }
    
}    

Towers Subclass(Bomb Tower);
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bomb here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bomb extends Towers
{
    public Bomb(Counter2 money)
    {
       super(30,55,60);

    }
    
    /**
     * Act - do whatever the Bomb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        super.Act();
        kind=5;
    }    
}

Creepers Subclass(Small):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Small here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Small extends Creepers
{
    public Small()
    {
        super(10,9);
    }
    
    /**
     * Act - do whatever the Small wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        super.Waypoints();
        super.Move();
        super.Death();
        
        type=1;
    }    
}

The subclass codes are the same for the other subclasses.
fejfo fejfo

2015/3/3

#
if in tornClosed() distance is never bigger then range closest will be null and thats coused a nullpointer exeptiob
RagingAsian RagingAsian

2015/3/3

#
Im now getting this error. I increased the value of distance as well. java.lang.NullPointerException at Towers.turnClosest(Towers.java:57) at Towers.Act(Towers.java:36) at Bomb.act(Bomb.java:23) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
fejfo fejfo

2015/3/3

#
did u change annything ?
There are more replies on the next page.
1
2