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

2011/12/1

Accesing variable from another object

Yasuf Yasuf

2011/12/1

#
I just want to acces 1 variable from object goldy and use it in object robot. and now have no idea how to use it in robot, whatever i try i keep getting " non static method cannot be refferenced from a static content." All I really want to do is use. "int z=15" from object goldy in object robot can someone pls tell me how to do it
mjrb4 mjrb4

2011/12/1

#
Hi Yasuf, Have a look a the following tutorial which explains this in detail: http://www.greenfoot.org/doc/howto-1
Yasuf Yasuf

2011/12/1

#
I've been trying to work something out with that tutorial for like 3 hours now . It's too confusing for me. That's why I'm asking here
mjrb4 mjrb4

2011/12/1

#
...So what part of the tutorial have you tried successfully, what parts are you struggling with, where have you got so far? If you want to learn we need to know specifically what you're struggling with!
Yasuf Yasuf

2011/12/1

#
I tried to use it and still have no idea when do i use my variable. I'd say I'm struggling with everything cos nothing works. I'll try to explain my problem as good as I can. I don't give a thing about counting scores. I dont care about rockets and ships. All this stuff makes me confused. I am just trying to dig out the only code that is necessary to use variable from one object in another. And instead i'm getting:
void bumpCount(int amount)
{
    totalCount += amount;
    setImage(new GreenfootImage("" + totalCount));
}

void hitAnAsteroid()
{
    // What goes here????
    // We want to call the "bumpCount" method from the Counter class -
    // but how??!!
}


void hitAnAsteroid()
{
    bumpCount();
}


private Counter theCounter;

public Space()
{
    super(600, 400, 1);
    addObject(new Rocket(), 300, 200);
    theCounter = new Counter();
    addObject(theCounter, 5, 5);
}


public Counter getCounter()
{
    return theCounter;
}


void hitAnAsteroid()
{
    Space spaceWorld = (Space) getWorld();  // get a reference to the world
    Counter counter = spaceWorld.getCounter();  // get a reference to the counter
    counter.bumpCount(5);
}
to move 1 variable from one object in another.
davmac davmac

2011/12/1

#
You've posted the code from the tutorial, which isn't very helpful. We can already see what code is in the tutorial, because we can read the tutorial ourselves. What we don't know is what your code looks like, nor in what context (nor for what reason) you are trying to access the variable of one object from another. Without that, it's really hard to help you. The simplest answer I can give you is, if you have a variable "goldy" which references an actor of some kind, and you want to access the "z" variable in that actor and store its value into a local variable named "x", then you just do: int x = goldy.z;
Yasuf Yasuf

2011/12/1

#
okay what i'm trying to do is to make robot "dance" around gold ball (goldy) but to do that robot must know where the ball is positioned so i'm basicly trying to tell robot the position of the ball (which can be spawned anywhere) int x = goldy.z; - already tried this and theres an error "non static variable z cannot be refferenced from a static content." goldy:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class goldy extends Actor{
int z= getX();
public int nX()
    {
        int nnX;
        nnX= getX();
        return nnX;
        
    }
    public int nY()
    {
        int nnY;
        nnY=getY();
        return nnY;
    } 

    public void act() 
    {
        
    } 
    
    
}
robot (for now he's just trying to find out goldy's position)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class robot extends Actor
{
 
    public void act() 
    {

    } 
    public void countmove()
    {
        int nX = goldy.z;
        int rnX = getX();
        int rnY = getY();
        if (rnY>nY)
        {
            setLocation(rnX,rnY -1);
        } else if (rnY<nY)
        {
            setLocation(rnX,rnY +1);
        } else if (rnX>nX)
        {
            setLocation(rnX -1,rnY);
        } else if (rnX<nX)
        {
            setLocation(rnX +1, rnY);
        } 
        }
    }
}
davmac davmac

2011/12/1

#
So "goldy" is a class, not an object, which is why you're getting the "static context" error. First: in Java, the convention is to have class names start with a captital letter - so it should be Goldy and Robot, not goldy and robot. Variable names and method names should start with a lower-case letter. Second: your Robot needs a reference to the Goldy object in order to access it. The tutorial helps you here - in the same way the world in the tutorial stores a reference to the Counter, you can have your world store a reference to the Goldy. Then you add a "getGoldy()" method to your world. Your robot can call the world's getGoldy() method to get a reference to the Goldy object and can then access its variables / call its methods. Exactly as in the tutorial!
Yasuf Yasuf

2011/12/1

#
I know I'm probably pain in the certain place but I got another problem :D I got some strange
error wrote...
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getY(Actor.java:170) at Robot.Move(Robot.java:10) at Robot.act(Robot.java:41) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194)
world:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class world extends World
{


    private Goldy theGoldY;
    private Goldy theGoldX;
    public world()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        theGoldY= new Goldy();
        theGoldX = new Goldy();
        randomGoldy();
    }
    
    public Goldy getGoldy(){ return theGoldY; }
    public Goldy getGoldx(){ return theGoldX; }
    
    public void randomGoldy()
    {
        {
            Goldy goldee = new Goldy();
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(goldee, x, y);
        }
    }
}
Goldy:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Goldy extends Actor
{
       
       
    public void act() 
    {
      GiveX();
      
      GiveY();
        // Add your action code here.
    }   
    
    public int GiveX()
    {
        int nX = getX();
            return nX;
    }
    public int GiveY()
    {   
        int nY = getY();
        return nY;
  }
}
and Robot:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Robot extends Actor
{
    
     public void Move()
    {
        world worldy = (world) getWorld();
        Goldy ngoldY = worldy.getGoldy();
        int nY=ngoldY.getY();
       Goldy ngoldX = worldy.getGoldx();
        int nX=ngoldX.getX();
        
        
        int rnX = getX();
        int rnY = getY();
       if (rnY>nY)
        {
            setLocation(rnX,rnY -1);
        } else if (rnY<nY)
        {
            setLocation(rnX,rnY +1);
        } else if (rnX>nX)
        {
            setLocation(rnX -1,rnY);
        } else if (rnX<nX)
        {
            setLocation(rnX +1, rnY);
        }
    }
    
    public void act() 
    {
        Move();// Add your action code here.
    }    
   
}
bourne bourne

2011/12/1

#
The exception tells you what went wrong:
Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
You tried to use something like the getX() or getY() methods on an Actor that is not inside a World and so cannot be asked its location.
Yasuf Yasuf

2011/12/1

#
I spawn that actor by hand and using a code. Stops working when i try to act robot (who needs goldy's position)
bourne bourne

2011/12/1

#
I'm a little confused but nowhere do I see "theGoldY" being added to the World.
bourne bourne

2011/12/1

#
Maybe you meant to do something like this? : theGoldY= new Goldy(); theGoldX = new Goldy(); randomGoldy(theGoldY); randomGoldy(theGoldX); .. public void randomGoldy(Goldy g) { int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); addObject(g, x, y); }
Yasuf Yasuf

2011/12/1

#
Man ur great :D My mistake is so dumb I even won't try to comment it thank you everyone now its running :D
You need to login to post a reply.