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

2017/5/23

use variable from specific instance of an actor

Trystar360 Trystar360

2017/5/23

#
I have a main actor in my world( and there will only be one) and that actor has an updating variable. how do i use that variable. I tried
Actor actor = new Actor();
but it doesn't update. any ideas?
danpost danpost

2017/5/23

#
Trystar360 wrote...
I have a main actor in my world( and there will only be one) and that actor has an updating variable. how do i use that variable. I tried
Actor actor = new Actor();
but it doesn't update. any ideas?
What variable is it and what is it used for? where is the class code of that actor?
Trystar360 Trystar360

2017/5/25

#
I have an actor and i have 4 booleans telling if there are walls on the left, right, up or down. then i need to use that in a different actor to know if it can move or not. the actor i need the booleans from.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
/**
 * Write a description of class Pman here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pman extends Actor
{
    private GreenfootImage img;
    public Pman()
    {
        img = new GreenfootImage(200,200);
        img.setColor(Color.YELLOW);
        img.drawOval(0,0,0,0);
        img.fillOval(0,0,200,200);
        setImage(img);
        
    } 
    
    public void act()
    {
        check();
    }
  
    public boolean up, down, left, right;
    
    public void check()
    {
        if(getOneObjectAtOffset(-1,0, Wall.class) != null)
        {
            left = true;
        }
        if(getOneObjectAtOffset(1,0, Wall.class) != null)
        {
            right = true;
        }
        if(getOneObjectAtOffset(0,1, Actor.class) != null)
        {
            down = true;
        }
        if(getOneObjectAtOffset(0,-1, Wall.class) != null)
        {
            up = true;
        }
    }
    
    public void move()
    {
        if(k("up") == true)
        setLocation(getX(),getY() - 1);
        if(k("down") == true)
        setLocation(getX(),getY() + 1);
        if(k("left") == true)
        setLocation(getX()-1,getY());
        if(k("right") == true)
        setLocation(getX()+1,getY());
    }
    
    
     
    public boolean k(java.lang.String key)
    {
        boolean x = false;
        if(Greenfoot.isKeyDown(key))
        x = true;
        return x;
    }
    }    
the actor that needs the booleans
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Thing here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Thing extends Actor
{
    /**
     * Act - do whatever the Thing wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
    
    private int c1 = 5;
    public void work()
    {
        c1 --;
        if(c1 <= 0)
        {
            if(k("up") == true)
            {  
                setLocation(getX(), getY() +1);
                c1 = 10;
            }
            if(k("down") == true)
            {  
                setLocation(getX(), getY() -1);
                c1 = 10;
            }
            if(k("left") == true)
            {  
                setLocation(getX() + 1, getY());
                c1 = 10;
            }
            if(k("right") == true)
            {  
                setLocation(getX() - 1 , getY());
                c1 = 10;
            }
            c1 = 5;
        }
    }
    
    public boolean k(java.lang.String key)
    {
        boolean x = false;
        if(Greenfoot.isKeyDown(key))
        x = true;
        return x;
    }
}
You need to login to post a reply.