I am looking for a solution for my problem. I want to have a class, which shows the points. But the point-int-variable is in another class.
can someone help me?
thanks
//in the counter class;
public void act() {
if (!getWorld().getObjects(theOtherClass.class).isEmpty()) {
int points = getWorld().getObjects(theOtherClass.class).get(0).getPoints();//int points is the variable in this class where the points are saved;
}
}
//in the other class;
...//some other stuff;
public int getPoints() {
return points;//or however your variable is called;
}
public class Robson extends Roboter
{
public int points = 0;
int SPEED = 1;
int MaxPoints = 153;
int Bombs = 2;
public void act()
{
win();
checkKeys();
freeze();
}
public int getPoints()
{
return points;
}
.............
}
public class Counter extends Robson
{
/**
* Act - do whatever the Counter wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (!getWorld().getObjects(Robson.class).isEmpty())
{
int points = getWorld().getObjects(Robson.class).get(0).getPoints();
}
}
}
public void act()
{
if (!getWorld().getObjects(Robson.class).isEmpty())
{
List<Robson> robsons = getWorld().getObjects(Robson.class);
int points = robsons.get(0).getPoints();
}
}
} int points = ((Robson) getWorld().getObjects(Robson.class).get(0)).getPoints();
int points = ((Robson) getWorld().getObjects(Robson.class).
import greenfoot.*;
import java.awt.*;
public class lobstercounter extends lobster
{
private String text;
private GreenfootImage myImage;
public int wormlobstervalue;
public void act() {
myImage = new GreenfootImage(30,30);
setImage(myImage);
myImage.setFont(new Font("SansSerif",Font.BOLD,14));
myImage.setColor(Color.BLACK);
if (!getWorld().getObjects(lobster.class).isEmpty())
{
int wormlobstervalue = ((lobster) getWorld().getObjects(lobster.class).get(0)).getPoints();
}
text = "" + wormlobstervalue;
myImage.drawString(text,5,18);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class lobster extends Animal
{
public int wormlobster;
/**
* Act - do whatever the lobster wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
...
...
else if (canSee(worm.class))
{
eat(worm.class);
wormlobster++;
}
}
public int getPoints() {
return wormlobster;
}
}