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

2014/4/10

Help with accessing variables between actors

Volatile_Matter Volatile_Matter

2014/4/10

#
I want my "MeanBird" to decrease int variable "oneup" by one each time it eats a turtle. This is the bird's code:
{
/**
     * Act - do whatever the MeanBird wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
  
    public void act() 
    { move(5);  
        Actor TURDLE;
        TURDLE = getOneObjectAtOffset(0, 0, TURDLE.class);
        if (TURDLE != null)
        {
                World world;
                world= getWorld();
                world.removeObject(TURDLE);
                oneup = oneup - 1;
            }
    if (TURDLE != null)
    {
        move(-10);
       
    }
    else 
    {TURDLE player = (TURDLE) getWorld().getObjects(TURDLE.class).get(0);    
    turnTowards(player.getX(), player.getY()); 
    }
}  
   }
And this is the world code:
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TurdleWurdle extends World
{
    public int score = 15;
     public int oneup = 0;
    /**
     * Constructor for objects of class TurdleWurdle.
     * 
     */
    public TurdleWurdle()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
         addObject(new TURDLE(), 50, 200);
         addObject(new TURDLE(), 50, 175);
         addObject(new TURDLE(), 50, 150);
         addObject(new TURDLE(), 50, 125);
         addObject(new TURDLE(), 50, 100);
         addObject(new TURDLE(), 50, 75);
         addObject(new TURDLE(), 50, 50);
         addObject(new TURDLE(), 50, 25);
         addObject(new TURDLE(), 50, 225);
         addObject(new TURDLE(), 50, 250);
         addObject(new TURDLE(), 50, 275);
         addObject(new TURDLE(), 50, 300);
         addObject(new TURDLE(), 50, 325);
         addObject(new TURDLE(), 50, 350);
         addObject(new TURDLE(), 50, 375);
         addObject(new MeanBird(), 200, 50);
         addObject(new MeanBird(), 100, 50);
         addObject(new You(), 200, 200);
         addObject(new Indic(), 500, 100);
         int counter = 0; 
        } 
public void act()
{
     if ( Greenfoot.getRandomNumber(100) < 10 )
    {
        addObject(new TURDLE(), 50, 200);
       score = score + 1; 
    } 
    if ( Greenfoot.getRandomNumber(100) < 5 )
    {
        addObject(new MeanBird(), 50, 200);
    }  
        }
}
and this is the "Indic" indicator code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Indic here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Indic extends Actor
{
    /**
     * Act - do whatever the Indic wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
  
    private GreenfootImage smiley1;
    private GreenfootImage smiley2;
    private GreenfootImage smiley3;
    private GreenfootImage smiley4;
    

    
    public void act()
    {
    if(oneup > 0) {
            updateImageBad();
        }
    if(oneup > 16) {
            updateImageOkay();
        }
    if(oneup > 24) {
            updateImageGood();
        }
    
    {
       //why are you not working???
    }    
}
    private void updateImageGood()
    {
        setImage(smiley2);
    }
    private void updateImageOkay()
    {
        setImage(smiley4);
    }
    private void updateImageBad()
    {
        setImage(smiley3);
    }
}
---- I need the Indic to change to smiley or sad based upon how many "Turdle"s there are in the world. Every time a MeanBird eats a Turdle, it should drop by one, and every time a Turdle is spawned, it should grow by one.
danpost danpost

2014/4/11

#
Here is a how-to on that. Couple of things, however:
  • I do not see where your smiley images are being assigned values in the Indic class
  • There is no use for line 35 in the TurdleWurdle class ('int counter = 0;')
SIDENOTE: could you not just use 'getWorld().getObjects(TURDLE.class).size()' to get the number of TURDLE objects in the world (instead of using the 'oneup' field; or, to assign it, as a variable, the value while being declared in the method locally)?
You need to login to post a reply.