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

2014/3/14

counter help

1
2
hugtvy hugtvy

2014/3/14

#
ahh, this gonna sound like a really simple question but i have a scenario where i have a class that gets "attacked2 by zombies and ive made it so that if the zombie "hits the survivor" the survivor eats the zombie and some of his health is taken away(1/3) however i cant figure out how to subtract this health by twenty also when i "eat" a healthkit i need to add 20, any help?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Survivor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Survivor extends Players
{
    private int counter = 0;
    private int counter1 = 0;
    private int health = 60;
    
    /**
     * Act - do whatever the Survivor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    

    public void act() 
    {
       
        shoot();
        move();
        AssaultClass();
        TntClass();
        Helper();
        Healer();
        shooter();
        HealthKit();
        ZombieHit();
        
        MouseInfo m = Greenfoot.getMouseInfo();  
            if(m != null)
                turnTowards(m);
       
           
    }
   
    
    public void shooter()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, PreHelper3.class);
        if (zombie != null)
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            getWorld().addObject(new Shooter(), getX(), getY());
        }
    }
    
    public void HealthKit()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, HealthKit.class);
        if (zombie != null && (health  < 41))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 60;
        }
    }
    
    public void ZombieHit()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, Zombie.class);
        if (zombie != null)
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 0;
            if(health.getValue()==0) Greenfoot.stop(); 
        }
    }
hugtvy hugtvy

2014/3/14

#
lines 78 and 65 are the ones that need changing to add 20/subtract 20, cheers
danpost danpost

2014/3/14

#
Please refer to the Java tutorial page Assignment, Arithmetic, and Unary Operators.
hugtvy hugtvy

2014/3/14

#
cheers pal, I think i fixed it, could you just tell me if this is correct? also why is my "getValue" not working? (line 43 i think). Comes up saying "int cannot be deferenced? Not entirely sure what that means, again kinda new to this.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Survivor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Survivor extends Players
{
    private int counter = 0;
    private int counter1 = 0;
    private int health = 60;
    
    /**
     * Act - do whatever the Survivor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    

    public void act() 
    {
        stop();
        shoot();
        move();
        AssaultClass();
        TntClass();
        Helper();
        Healer();
        shooter();
        HealthKit();
        ZombieHit();
        
        MouseInfo m = Greenfoot.getMouseInfo();  
            if(m != null)
                turnTowards(m);
       
           
    }
   
    public void stop()
    {
    if(health.getValue()==0) Greenfoot.stop();
    }   
    
    public void shooter()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, PreHelper3.class);
        if (zombie != null)
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            getWorld().addObject(new Shooter(), getX(), getY());
        }
    }
    
    public void HealthKit()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, HealthKit.class);
        if (zombie != null && (health  < 41))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = + 20;
        }
    }
    
    public void ZombieHit()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, Zombie.class);
        if (zombie != null)
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = - 20; 
        }
    }
hugtvy hugtvy

2014/3/14

#
Dont worry figured it out on my own made it really complicated but think i got it haha
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Survivor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Survivor extends Players
{
    private int counter = 0;
    private int counter1 = 0;
    private int health = 60;
    
    /**
     * Act - do whatever the Survivor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    

    public void act() 
    {
        shoot();
        move();
        AssaultClass();
        TntClass();
        Helper();
        Healer();
        shooter();
        HealthKit();
        HealthKit1();
        ZombieHit();
        ZombieHit1();
        ZombieHit2();
        
        MouseInfo m = Greenfoot.getMouseInfo();  
            if(m != null)
                turnTowards(m);
       
           
    }
   
     
    
    public void shooter()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, PreHelper3.class);
        if (zombie != null)
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            getWorld().addObject(new Shooter(), getX(), getY());
        }
    }
    
    public void HealthKit()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, HealthKit.class);
        if (zombie != null && (health  == 40))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 40 + 20;
        }
    }
    
    public void HealthKit1()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, HealthKit.class);
        if (zombie != null && (health  == 20))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 20 + 20;
        }
    }
    
    public void ZombieHit()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, Zombie.class);
        if (zombie != null && (health == 60))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 60 - 20;
        }
    }
    
    public void ZombieHit1()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, Zombie.class);
        if (zombie != null && (health == 40))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 40 - 20;
        }
    }
    
    public void ZombieHit2()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, Zombie.class);
        if (zombie != null && (health == 20))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 20 - 20;
            if (health == 0)
            {
                getWorld().removeObject(this);
            }
        }
    }
danpost danpost

2014/3/14

#
Yeah, awfully complicated. You could have just used: health = health - 20; or health -= 20; Line 37 uses a 'turnTowards' method with only one int argument. Unless you have overridden the method, the compiler will not like that call. Also, if ZombieHit2 removes the object from the world, 'turnTowards' may cause an IllegalStateException.
hugtvy hugtvy

2014/3/14

#
It does cause an IllegalStateException, anyway i can fix this? this is my mouse code
public void act() 
    {
        shoot();
        move();
        AssaultClass();
        TntClass();
        Helper();
        Healer();
        shooter();
        HealthKit();
        HealthKit1();
        ZombieHit();
        ZombieHit1();
        ZombieHit2();
        
        MouseInfo m = Greenfoot.getMouseInfo();  
            if(m != null)
                turnTowards(m);
       
             
    }

public void turnTowards (int x, int y)
    {
       double dx = x - getX();
       double dy = y - getY();
       double angle = Math.atan2(dy,dx)*180.0/Math.PI;
       setRotation( (int)angle );
    }

    public void turnTowards (MouseInfo mi)
    {   
       turnTowards(mi.getX(), mi.getY());
    }
danpost danpost

2014/3/14

#
The Actor class within greenfoot already has a 'turnTowards(int, int)' method. You can remove lines 23 through 30 and it will still work. You could remove lines 31 through 34 and change line 18 to this:
turnTowards(m.getX(), m.getY());
You can put lines 16 through 18 in a 'turn' method and call it before 'move' in the 'act' method.
hugtvy hugtvy

2014/3/14

#
wow cheers that help alot! Could you tell me how to make greenfoot stop spawning and allowing things to move once ive died, i know where to put it just dont know the method to make it stop, cheers
danpost danpost

2014/3/14

#
You tried to use it in a previous version, but had trouble with the condition. You were trying to call 'getValue' on an int (a primitive data type), which you cannot do ( 'if (health <= 0)' would have worked ). The command 'Greenfoot.stop();' was correct to stop the scenario.
hugtvy hugtvy

2014/3/14

#
ahh cheers haha thank you so much, one last thing, how can i then make it so that when the game stops the amount on my counter is projected onto the screen? my counters a simple edit>Import Class>Counter
danpost danpost

2014/3/14

#
Do you hold a reference to the counter object in a field in your subclass of World? Show any code in your subclass of World that deals with this counter object.
hugtvy hugtvy

2014/3/14

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

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Gun
{
   /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int direction, speed;
    private int counter = 0;
    
    public Bullet(int dir)
    {
        direction = dir;
        speed = 15;
    }
    
    private void bumpCounter(int amount)
    {
        ZombieLand world;
        world = (ZombieLand)getWorld();
        Counter counter;
        counter = (Counter)world.getObjects(Counter.class).get(0);
        counter.add(amount);
    }
    
    public void act() 
    {
        eat1();
        eat2();
        move(speed);
        setRotation(direction);
        disappear();
    } 
     
    public void eat1()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, Enemies.class);
        if (zombie != null)
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            bumpCounter(1);
        }
    }  
    
     public void eat2()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, Tnt.class);
        if (zombie != null)
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            getWorld().addObject(new Explosion(), getX(), getY());
        }
    }  
    
    public void disappear()
    {       
        Actor tnt = getOneIntersectingObject(Explosion.class);
         if (getX() <=1 || getX() >= getWorld().getWidth() -1)
        {
            getWorld().removeObject(this);
        }
         else if(tnt != null)
        {
            getWorld().removeObject(this);
        }
               
    }
}
and my counter
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A Counter class that allows you to display a numerical value on screen.
 * 
 * The Counter is an actor, so you will need to create it, and then add it to
 * the world in Greenfoot.  If you keep a reference to the Counter then you
 * can adjust its value.  Here's an example of a world class that
 * displays a counter with the number of act cycles that have occurred:
 * 
 * <pre>
 * class CountingWorld
 * {
 *     private Counter actCounter;
 *     
 *     public CountingWorld()
 *     {
 *         super(600, 400, 1);
 *         actCounter = new Counter("Act Cycles: ");
 *         addObject(actCounter, 100, 100);
 *     }
 *     
 *     public void act()
 *     {
 *         actCounter.setValue(actCounter.getValue() + 1);
 *     }
 * }
 * </pre>
 * 
 * @author Neil Brown and Michael Kölling 
 * @version 1.0
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private String prefix;
    
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }

    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
        
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
        
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
danpost danpost

2014/3/14

#
Neither class given is a subclass of World. Also, a bullet is not a type of gun and therefore Bullet should not extend Gun.
hugtvy hugtvy

2014/3/14

#
ahh miss read haha, i know but there are a lot of classes so i try to seperate them out a bit, im OCD like that
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ZombieLand here.
 * 
 * @author (your name) http://www.greenfoot.org/topics/156 
 * @version (a version number or a date)
 */
public class ZombieLand extends World
{
    private int counter = 0;
    private int counter1 = 0;
    private int counter2 = 0;
    private int counter3 = 0;
    private int counter4 = 0;
    private int counter5 = 0;
    
    
    /**
     * Constructor for objects of class ZombieLand.
     * 
     */
    public ZombieLand()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 800, 1); 

        prepare();
    }

    public void act()
    {
        counter++;
        if (counter > 300) 
        {
            addObject(new Zombie(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            addObject(new Zombie(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            addObject(new Zombie(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            counter = 0;
            
        }
        
        counter5++;
        if(counter5 > 400)
        {
            addObject(new Zombie(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            addObject(new Zombie(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            addObject(new Zombie(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            addObject(new Zombie(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            addObject(new Zombie(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            counter5 = 0;
        }
        counter1++;
        if (counter1 > 300) 
        {
            addObject(new Tnt(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            addObject(new Tnt(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            addObject(new Tnt(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            counter1 = 0;
        }
        
        counter2++;
        if (counter2 > 2000)
        {
            addObject(new PreHelper(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            counter2 = 0;
        }
        
        counter3++;
        if (counter3 > 2500)
        {
            addObject(new PreHelper2(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            counter3 = 0;
        }
        
        counter4++;
        if (counter4 > 3000)
        {
            addObject(new PreHelper3(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
            counter4 = 0;
        }
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Survivor survivor = new Survivor();
        addObject(survivor, 416, 401);
        survivor.setLocation(503, 383);
        Counter counter = new Counter();
        addObject(counter, 44, 19);
        counter.setLocation(52, 44);
        Tnt tnt = new Tnt();
        addObject(tnt, 646, 639);
        removeObject(tnt);
        Helper helper = new Helper();
        addObject(helper, 785, 570);
        removeObject(helper);
        counter.setLocation(42, 57);
        counter.setLocation(44, 59);
        counter.setLocation(44, 54);
        counter.setLocation(44, 50);
        counter.setLocation(45, 49);
        counter.setLocation(44, 49);
    }
}

There are more replies on the next page.
1
2