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

2014/4/3

How to get variables from different Objects from a Class for a Text-Class

Midas97 Midas97

2014/4/3

#
Hi everyone, It seems to be more difficult than I thought: How you can show a variable (Energy) from different objects that are in a robot-class? I have done it so far, but when I want to tell what the text should represent (->Energy) it comes a failure: The Text-Class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class CounterEnergy extends Actor
{
    private int Energy_c3p0;
    private int Energy_r2d2;
    private int Energy_no5;
    public CounterEnergy()
    {
        Energy_c3p0 = Informator(pName("c3p0")).energiestand; //c3p0 should be the name of the Object that is in the Informator-Class
        Energy_r2d2 = Informator(pName("r2d2")).energiestand; //r2d2 should be the name of the Object that is in the Informator-Class
        Energy_no5 =  Informator(pName("no5")).energiestand; //no5 should be the name of the Object that is in the Informator-Class
    }
The Informator-Class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Informator extends Roboter
{
    private String Name;
    public int energiestand;
    private boolean kannBewegen;
    private int anzahlSchrauben;
    public Informator(String pName, int pEnergiestand)
    {
        Name = pName;
        energiestand = pEnergiestand;
        kannBewegen = true;
        anzahlSchrauben = 5;
    }
    
    public void act()
    {
        World myWorld = getWorld();
        World world = (World)myWorld;
        CounterEnergy CE = world.getCounter();
    }
The World-Class:
   import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class RoboterWelt extends World
{
    private CounterEnergy CE;
    private Informator c3p0;
    private Informator r2d2;
    private Informator no5;
 public RoboterWelt()
    {
        super(14, 10, zellenGroesse);
        setBackground("images/Bodenplatte.png");
        setPaintOrder(Roboter.class, Schraube.class, Akku.class,Wand.class);
        Greenfoot.setSpeed(20); 
        prepare();
    }
public CounterEnergy getCounter()
    {
        return CE;
    }
public void prepare()
    {
        CE = new CounterEnergy();
        addObject(CE,0,0);
        c3p0 = new Informator("c3po", 120);
        addObject(c3p0,6,2);
        c3p0.setRotation(180);
        r2d2 = new Informator("r2d2", 80);
        addObject(r2d2,1,5);
        no5 = new Informator("no5", 20);
        addObject(no5,3,3);
}
I'll be really grateful if someone knows the answer
erdelf erdelf

2014/4/3

#
in the Informator Class, change line 19-20 with this
RoboterWelt world = (RoboterWelt) getWorld();
you tried to use a method in the RoboterWelt class but u wrote that the method should be in the normal World class
You need to login to post a reply.