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

2013/5/26

Why level doesen't increase?

Miikku Miikku

2013/5/26

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

public class Zombie extends Actor
{
    private GreenfootImage zombieAnim1 = new GreenfootImage("Zombieanim.png");
    private GreenfootImage zombieAnim2 = new GreenfootImage("Zombieanim2.png");
    private GreenfootImage zombieAnim3 = new GreenfootImage("Zombieanim3.png");
    private int frame = 1;
    private int animationCount = 0;
    private int health = 30;
    private static int level = 1;

    private Counter counter;
    
    public Zombie(Counter pointCounter)
    {
       counter = pointCounter; 
    }    
    
    public void act() 
    {
        AI();
        
        if(animationCount % 12 == 0)
        animationWalk();
        
        animationCount ++;
        
        eat();
        death();
        level();
    }    
    
    public void level()
    {
        if(level == 1)
        {
            move(1);
        }
        else if(level >= 1)
        {
            move(1);
        } 
        else if(level >= 10)
        {
            move(2);
        }
        else if(level >= 20)
        {
            move(3);
        }
        else if(level >= 40)
        {
            move(5);
        }
        else if(level >= 80)
        {
            move(8);
        }
        else if(level >= 120)
        {
            move(12);
        }
          
    }
    
    
    public void AI()
    {
        turnTowards(Soldier.currentX, Soldier.currentY);
    }
    
    public void animationWalk()
    {
        if(frame == 1)
        {
        setImage(zombieAnim1);
        frame = 2;
        }
        else if(frame == 2)
        {
        setImage(zombieAnim2);
        frame = 3;
        }
        else if(frame == 3)
        {
        setImage(zombieAnim3);
        frame = 1;
        }
    }
    
    public void death()
    {
        Actor bullet = getOneIntersectingObject(Bullet.class);
    
        if (bullet != null)
        {
            health = health - 10;
            getWorld().removeObject(bullet);
        }
        if (health <= 0)
        {
            level = level ++;
            ((Counter)getWorld().getObjects(Counter.class).get(0)).add(1);
            getWorld().removeObject(this);
        }  
    }
    
    public void eat()
    {
        Actor soldier;
        soldier = getOneObjectAtOffset(0, 0, Soldier.class);
        if (soldier != null)
        {
            World ZombieWorld;
            ZombieWorld = getWorld();
            ZombieWorld.removeObject(soldier);
            setImage("gameoverr.png");
            Greenfoot.stop();
            Greenfoot.stop();
        }
    }
}
Gevater_Tod4711 Gevater_Tod4711

2013/5/26

#
Your level variale does count up. The problem is your level method. First you check whether the value of the variable level is 1; If this isn't the case you check whether the value is greater or equal one; If also this isn't the case you check whether the value is greater or equal 10... But every number that is greater that 10 also is greater than 1. And so the if clause that checks whether the value is greater than 1 is always true and the other checks are never executed. To fix the problem you should change the order of the if clauses (start with if (level >= 120) and end with if (level == 1)). Some more improvements: in line 103: only level++ would also work. in line 120: stoping greenfoot only once would also be enought.
Miikku Miikku

2013/5/26

#
Thanks
You need to login to post a reply.