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

2011/12/17

Question: Is there any API that can create a timer in Greenfoot?

1
2
b321234 b321234

2011/12/18

#
@dordor, it's no problem. Thanks for your reply and patience. I really appreciate that!
dordor dordor

2011/12/18

#
You're welcome!
b321234 b321234

2011/12/19

#
One more question... code--
public class Bullet extends Actor
{
    public int zombieCounter =0;
 
    public void act() 
    {
        setLocation(getX(),getY()-10);
        boolean removed = checkEdge(getY());
        if (!removed)
        {
            boolean killed = checkKill();
            if(killed)
            zombieCounter++;
        }
    }    

    public boolean checkKill()
    {
        boolean killed = false;
        Actor zombie;
        zombie = getOneObjectAtOffset(0,0,Zombie.class);
        if(zombie!=null)
        {
            World world;
            world = getWorld();
            world.removeObject(zombie);
            int sound = Greenfoot.getRandomNumber(2);
            if(sound==0)
                Greenfoot.playSound("zombieDeath.wav");
            else
                Greenfoot.playSound("zombieDeath2.wav");
            getWorld().removeObject(this);
            killed=true;
        }           
        return killed;
    }    
Don't care about checkEdge because it has no problem The only problem is private int zombieCounter For some reason it doesn't work. I made the program print the results after the game is over but it's always ZERO. Can anyone help me fix that, many thanks!!
danpost danpost

2011/12/19

#
zombieCounter cannot be put in the Bullet class, as each instance of bullet will get one, track any zombie it kills and then its zombieCounter disappears when the bullet is removed from the world. Move it to the Score class, if you have one. If not, move it the World class and make it a public variable.
b321234 b321234

2011/12/19

#
@danpost, I made some changes but it's still 0. In Bullet class I wrote
ZombieWorld zombieWorld=new ZombieWorld();
 
    public void act() 
    {
        setLocation(getX(),getY()-10);
        boolean removed = checkEdge(getY());
        if (!removed)
        {
            boolean killed = checkKill();
            if(killed)
            zombieWorld.addKilledAmount();
        }
    }    
1. And in the world class, act method:
 boolean nextLevel = nextLevel();
        if(nextLevel){
            zombie.addZombieLevel();
            gameLevel++;
            setBackground("bathroom-tile.jpg");
        }
}
2. World class
 public boolean nextLevel(){
        boolean nextLevel = false;
        if (zombieCounter>10){
            nextLevel=true;
        }else
            nextLevel=false;
        return nextLevel;
    }
3. Also in world class:
 public void addKilledAmount(){
        zombieCounter++;
    }
Thanks * 1000000 times!
dordor dordor

2011/12/19

#
I think I know what's the problem. You created a new world instead of getting the current one. Try to replace
new ZombieWorld()
in line 1 with
(ZombieWorld)getWorld()
danpost danpost

2011/12/19

#
In 2. World class you could just write
public boolean nextLevel()
{
    return (zombiesCounter > 10);
}
or, you could (since you only call this once from within act) eliminate this method altogether and in 1. And in the world class, act method, eliminate line 1 and change line 2 to
if (zombieCounter > 10) {
This does not change to functionality, but makes the code easier to read and follow.
b321234 b321234

2011/12/19

#
Guys, I thank you from the bottom of my heart. Really help me a lot
You need to login to post a reply.
1
2