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

2012/12/13

Help with spawning

1
2
avatarcolin avatarcolin

2012/12/13

#
Hi, i am making a project for school and it's due this Friday, I am trying to make it when i kill a humans it with spawn another humans, but when I do the method it always spawn and enormous amount of humans, here is the code I used: public void checkWave() { if (Zombie.score == 4) { getWorld().addObject(new Human(),200,100); } can someone help me to make it only spawn 1 human instead of thousands!
Kyle273 Kyle273

2012/12/13

#
What's happening here is that once Zombie.score is equal to 4, the game will continually spawn humans until your score no longer equals 4. When's checkWave() getting called? If it's in the act method, that explains the getting thousands. One place you could put the getWorld().addObject line would be in the code that removes your humans. That way, once one human dies, another spawns instantly.
avatarcolin avatarcolin

2012/12/13

#
well i want it so that when four are killed 6 more spawn
danpost danpost

2012/12/13

#
In the place where you increment the Zombie.score (in the immediate lines after it) put the following code
if(Zombie.score%4==0)spawnHumans();
where 'spawnHumans()' will be coded to create 6 new humans.
avatarcolin avatarcolin

2012/12/13

#
doesn't work
danpost danpost

2012/12/13

#
I would like to know what class you placed the code in and where. Please post the pertinent code.
avatarcolin avatarcolin

2012/12/13

#
import greenfoot.*; import java.awt.Color; import java.awt.Font; public class Waves extends Actor { public static int Wave = 0; public static int Human = 0; public void act() { Waves(); checkWave(); } public void Waves() { GreenfootImage image = new GreenfootImage(145, 35); Font font = new Font ("Arial", Font.PLAIN, 15); image.setFont(font); image.setColor(Color.WHITE); image.drawString("Wave: " + Wave, 5, 27); setImage(image); } public void checkWave() { if (Zombie.score == 4) { //code for wave 2 goes here } if (Zombie.score == 10) { //code for wave 3 goes here } if (Zombie.score == 18) { //code for wave 4 goes here } if (Zombie.score >= 28) { this is when you win getWorld().removeObjects(getWorld().getObjects(HumanDead.class)); getWorld().removeObjects(getWorld().getObjects(Zombie.class)); getWorld().removeObjects(getWorld().getObjects(Bar.class)); getWorld().removeObjects(getWorld().getObjects(Waves.class)); getWorld().removeObjects(getWorld().getObjects(Health.class)); Greenfoot.delay(1); getWorld().setBackground("youWin.png"); getWorld().addObject(new Button1(), 223,384); Wave = 0; Zombie.score = 0; Greenfoot.delay(1); getWorld().removeObject(this); } } }
avatarcolin avatarcolin

2012/12/13

#
oops wrong code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class HumanDead here. * * @author (your name) * @version (a version number or a date) */ public class HumanDead extends Actor { private boolean flag = false; /** * Act - do whatever the HumanDead wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Zombie theZombie = null; theZombie = (Zombie)getOneIntersectingObject(Zombie.class); if(theZombie != null) { if (Greenfoot.isKeyDown("space")) { Zombie.score++; if(Zombie.score%4==0)spawnHumans(); Greenfoot.playSound("Bite.mp3"); getWorld().addObject(new Blood(), getX(),getY()); getWorld().removeObject(this); { } } }
danpost danpost

2012/12/13

#
Since you already have the code set up in the Waves class, go ahead and remove 'if(Zombie.score%4==0)spawnHumans();' from the HumanDead class. In the Waves class, change the following lines:
if (Zombie.score == 4)
{
    //code for wave 2 goes here
}
if (Zombie.score == 10)
{
    //code for wave 3 goes here
}
if (Zombie.score == 18)
{
    //code for wave 4 goes here
}
if (Zombie.score >= 28)
to the following
if (Wave<3 && (Wave+2)*(Wave+2)+Wave==Zombie.score)
{
    for(int i=0; i<2*Wave+4; i++) getWorld().addObject(new Human(), 200, 100);
    Wave++;
}
if (Wave==3 && (Wave+2)*(Wave+2)+Wave==Zombie.score)
This should add the correct number of Humans at the correct time, since the value of Wave changes in the code. This code adds all the new Humans at the same location in the world; you can make a block for the 'for' loop and add the size Humans randomly or just at different places in you wish.
avatarcolin avatarcolin

2012/12/13

#
okay so now i have this in wave class:
import greenfoot.*; 
import java.awt.Color;
import java.awt.Font;
public class Waves  extends Actor
{
    public static int Wave = 0;
    public static int Human = 0;
    public void act() 
    {
        Waves();
        checkWave();
    }
    public void Waves()
    {
        GreenfootImage image = new GreenfootImage(145, 35);
        Font font = new Font ("Arial", Font.PLAIN, 15);
        image.setFont(font);
        image.setColor(Color.WHITE);
        image.drawString("Wave: " + Wave, 5, 27);
        setImage(image);
    }
    public void checkWave()
    {
      if (Wave<3 && (Wave+2)*(Wave+2)+Wave==Zombie.score)
     {
        for(int i=0; i<2*Wave+4; i++) getWorld().addObject(new Human(), 200, 100);
        Wave++;
     }
     if (Wave==3 && (Wave+2)*(Wave+2)+Wave==Zombie.score
     {
         
     }
    } 
}  
and it does nothing...
vonmeth vonmeth

2012/12/13

#
Edit: Nm.
danpost danpost

2012/12/13

#
I would like to see the class that has the Zombie.score variable in it (the Zombie class).
avatarcolin avatarcolin

2012/12/13

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Zombie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Zombie extends Actor
{
    public static int Health = 100;
    public static int score = 0;
    public static int cycle = 0;
    public static int cycleTimer = 0;
    /**
     * Act - do whatever the Zombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        cycleTimer++;
        if (Health == 0)
        {
            setImage("ZombieDead.png");
            Greenfoot.playSound("ZombieMoan.mp3");
            getWorld().addObject(new ZombieDead(), getX(),getY());
            getWorld().removeObject(this);
        }
        if (Greenfoot.isKeyDown("l"))
        {
            setImage("ZombieDead.png");
            Greenfoot.playSound("ZombieMoan.mp3");
            getWorld().addObject(new ZombieDead(), getX(),getY());
            getWorld().removeObject(this);
        }
            if (Greenfoot.isKeyDown("a"))
        {
            if (cycleTimer > 10)//Checks cycles 
            {
                cycle++;
                if (cycle == 4)
                {
                    cycle = 0;
                }
                animate(2, cycle);
                cycleTimer = 0;
            }
        }
        if (Greenfoot.isKeyDown("d"))
        {
            if (cycleTimer > 10)
            {
                cycle++;
                if (cycle == 4)
                {
                    cycle = 0;
                }
                animate(3, cycle);
                cycleTimer = 0;
            }
        }
        if (Greenfoot.isKeyDown("s"))
        {
            if (cycleTimer > 10)
            {
                cycle++;
                if (cycle == 4)
                {
                    cycle = 0;
                }
                animate(0, cycle);
                cycleTimer = 0;
                if (Greenfoot.isKeyDown("a"))
            {
                turn(1);
            }
            if (Greenfoot.isKeyDown("d"))
            {
                turn(-1);
            }
            }
        }
        if (Greenfoot.isKeyDown("w"))
        {
            if (cycleTimer > 10)
            {
                cycle++;
                if (cycle == 4)
                {
                    cycle = 0;
                }
                animate(1, cycle);
                cycleTimer = 0;
                if (Greenfoot.isKeyDown("a"))
            {
                turn(-1);
            }
            if (Greenfoot.isKeyDown("d"))
            {
                turn(1);
            }
            }
        
        }
    }    
    private void animate(int dir, int cy)//Animate the zombie
    {
        switch(dir)
        {
            case 0://Forward
                this.setImage(new GreenfootImage("zombieDown" + cy + ".png"));
                this.setLocation(this.getX(), this.getY() + 10);
                break;
            case 1://Backward
                this.setImage(new GreenfootImage("zombieUp" + cy + ".png"));
                this.setLocation(this.getX(), this.getY() - 10);
                break;
            case 2://Left
                this.setImage(new GreenfootImage("zombieLeft" + cy + ".png"));
                this.setLocation(this.getX() - 10, this.getY());
                break;
            case 3://Right
                this.setImage(new GreenfootImage("zombieRight" + cy + ".png"));
                this.setLocation(this.getX() + 10, this.getY());
                break;
        }
    }
    
}
danpost danpost

2012/12/13

#
If there is a Waves object in the world, then its 'act' method should call the 'checkWave' method continuously. Therefore, as long as your Zombie.score variable is increasing in value, it should add more Human objects at the right times. Do this: add the following method to the Zombie class
public static void bumpScore()
{
    score++;
    System.out.println("Score: "+score);
}
Then, in the HumanDead 'act' method, change 'Zombie.score++;' to 'Zombie.bumpScore();'. Then run the scenario and note the terminal output. Inform on next post.
avatarcolin avatarcolin

2012/12/13

#
score = 1
There are more replies on the next page.
1
2