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

2017/12/26

Need help with score/life

1
2
3
RevoltecX7 RevoltecX7

2018/1/25

#
So differences between:
private static Actor scoreText;
and
scoreText = getNewStillActor();
public static Actor getNewStillActor()
{
     return new Actor()
     {
         //public void setLocations(int x, int y){}
     };
}
are The 1. one is for created Actor(without greenfoot.Actor and java.lang.Object classes) and the 2. one is for inplement greenfoot.Actor and java.lang.Object classes?
danpost danpost

2018/1/25

#
This:
private static Actor scoreText;
declares a class field called 'scoreText' to reference an Actor object. Nothing more. It does not assign any value to it and its default value would be 'null'. The following line of code found in the constructor of the World subclass:
scoreText = getNewStillActor();
is what assigned the returned Actor to the field. This method (without comments):
public static Actor getNewStillActor()
{
     return new Actor()
     {
         public void setLocations(int x, int y){}
     };
}
is a class method called 'getNewStillActor' which returns an Actor object (line 1 declares this). Within the method an unmovable Actor is created and returned. Nothing more. I could have done the following, which does exactly the same thing (except that the actors would be named 'StillActor':
// with this Actor subclass
import greenfoot.*;

public class StillActor extends Actor
{
    public void setLocation(int x, int y)
    {
    }
}

// and a 'getNewStillActor' method as follows
public static Actor getNewStillActor()
{
    Actor actor = new StillActor();
    return actor;
}
RevoltecX7 RevoltecX7

2018/1/25

#
This is much better for understand(for me now). I will try repeat it in another program/game and I will see. But I think I get it. Thanks guys :)
You need to login to post a reply.
1
2
3