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


1 2 3 4 5 6 7 8 9 10 11 12 13 | //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; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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(); } } } |
1 2 3 4 5 6 7 8 9 | public void act() { if (!getWorld().getObjects(Robson. class ).isEmpty()) { List<Robson> robsons = getWorld().getObjects(Robson. class ); int points = robsons.get( 0 ).getPoints(); } } } |
1 | int points = ((Robson) getWorld().getObjects(Robson. class ).get( 0 )).getPoints(); |
1 | int points = ((Robson) getWorld().getObjects(Robson. class ). |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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; } } |