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

2018/4/23

Help

hosfeldli hosfeldli

2018/4/23

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

/**
 * Write a description of class Terrorist here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Duck extends Actor
{
    private int i;
    public int life;
    /**
     * Act - do whatever the Terrorist wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Duck()
    {
        //getWorld().showText("Health: " + health, 100, 100);
    }
    public void act() 
    {
      Moving();  
      Damage();  
     
      
    }  
    public void Moving()
    {
        
        if(getY() == 400) 
        {
            while (i < 30)
            {
                setLocation(getX()-1, getY());
                i++;
            }
        }
    
    }
    
    public void Damage()
    {
         int number = getWorld().numberOfObjects();   
         
        if(Greenfoot.getRandomNumber(7000) < number)
        {
            getWorld().addObject(new Damage(), 500, 250); 
            life--;
            getWorld().showText("Health: " + life, 100, 100);
        } 
    }
}
every time it subtracts health it also sets it back to 96 no idea why.
hosfeldli hosfeldli

2018/4/23

#
it actually goes to -1 forgot to give number to variable
hosfeldli hosfeldli

2018/4/23

#
Isn't fixed though
danpost danpost

2018/4/24

#
hosfeldli wrote...
Isn't fixed though
Okay -- show the Damage class code (since it is being added to the world within the same cycle as losing health).
hosfeldli hosfeldli

2018/4/24

#
public class Damage extends Actor
{
    int time = 0;
    
    /**
     * Act - do whatever the Damage wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        time--; 
       if(time == 0)
       {
           getWorld().removeObject(this);
        }
        
    }    
    
    public Damage()
    {

        getImage().setTransparency(80);
        time = time + 10;  
       
        }
    }
danpost danpost

2018/4/24

#
hosfeldli wrote...
<< Code Omitted >>
Okay, nothing that could cause the issue there. Now, your world class codes.
hosfeldli hosfeldli

2018/4/24

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

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    //int turned = 0;
    Hands hands = new Hands();
    int difficulty = 1;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 563, 1); 
        addObject(hands, 600, 563);
        addObject(new TestWall(), 386, 396);
        addObject(new TestWall1(), 469, 312);
        addObject(new TestWall2(), 491, 269);
        addObject(new TestWall3(), 442, 271);
        addObject(new TestWall4(), 840, 398);
        setPaintOrder(Duck.class);
        setPaintOrder(TestWall.class);
        setPaintOrder(TestWall1.class);
        setPaintOrder(TestWall2.class);
        setPaintOrder(TestWall3.class);
        setPaintOrder(Hands.class);
        setPaintOrder(Damage.class);
        
    }
    
    
    
    public void act()
    {
        if(Greenfoot.getRandomNumber(300) < difficulty)
        {
            addObject(new Duck(), 370, 400);
            addObject(new TestWall(), 386, 396);
        }
        if(Greenfoot.getRandomNumber(300) < difficulty)
        {
            addObject(new Duck(), 473, 283);
            addObject(new TestWall1(), 469, 312);
            addObject(new TestWall2(), 491, 269);
            addObject(new TestWall3(), 442, 271);
        }
        if(Greenfoot.getRandomNumber(300) < difficulty)
        {
            addObject(new Duck(), 840, 380);
            addObject(new TestWall4(), 840, 398);
            
        }
        if(Greenfoot.getRandomNumber(30000) < 1)
        {
            difficulty++;
            
        }
    }
    
    
    
}
danpost danpost

2018/4/25

#
You end up with multiple Duck objects being added into the world. Each one will have its own value for life.
You need to login to post a reply.