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

2015/2/26

Code needs comments; anyone know what the lines say?

ValeroDeniro ValeroDeniro

2015/2/26

#
Hi everyone, i need some help understanding what each line says. would really appreicate it if anyone could help me, thanks so much.
*/
public class HighScore extends Actor
{
    private Actor alignLeftWith;

    public HighScore(Actor alignLeftWith)
    {
        this.alignLeftWith = alignLeftWith;
        setImage(new GreenfootImage(1, 1));
        if (UserInfo.isStorageAvailable())
        {
            UserInfo me = UserInfo.getMyInfo();
            if (me != null)
            {
                setImage(new GreenfootImage("Your Best: " + me.getScore(), 25, java.awt.Color.WHITE, new java.awt.Color(0, true)));
            }
        }
    }
    
    public void addedToWorld(World world)
    {
        setLocation(alignLeftWith.getX() - alignLeftWith.getImage().getWidth()/2 + getImage().getWidth()/2, getY());
    }
}
danpost danpost

2015/2/26

#
import greenfoot.*; // makes the classes of the greenfoot package accessible to the class

/**
 * creates a text image with text "Your Best: ####" that aligns the left edge of its
 * image with the left edge of the image of the actor supplied by way of the parameter
 * of the constructor
 */
public class HighScore extends Actor
{
    /**
     * instance object reference field to hold the actor this object is to align with;
     * the left edges of the image of this new HighScore actor will align with the
     * left edge of the image of the actor assigned to this field
     */
    private Actor alignLeftWith;
 
    /**
     * initializes this a new HighScore object
     * @param alignLeftWith the actor to align with
     */
    public HighScore(Actor alignLeftWith)
    {
        this.alignLeftWith = alignLeftWith; // save the actor to align with
        setImage(new GreenfootImage(1, 1)); // give actor an invisible image of size (1, 1)
        if (UserInfo.isStorageAvailable()) // is user logged in and UserInfo storage accessible
        {
            UserInfo me = UserInfo.getMyInfo(); // get local reference to data of user
            if (me != null) // does the data for this user exist
            {
                // the following statement sets the image of this HighScore object to display the proper text
                setImage(new GreenfootImage("Your Best: " + me.getScore(), 25, java.awt.Color.WHITE, new java.awt.Color(0, true)));
            }
        }
    }
     
    /**
     * aligns this HighScore object with the actor supplied through the constructor;
     * (this method is called automatically by the greenfoot framework when a HighScore object is placed into a world)
     */
    public void addedToWorld(World world)
    {
        setLocation(alignLeftWith.getX() - alignLeftWith.getImage().getWidth()/2 + getImage().getWidth()/2, getY());
    }
}
Notes: * line 24 is a 'preemptive else' for the following 'if' block (the statement could be moved to an 'else' block for the 'if' statement); * the code does not address the possibility that the actor given to align with may not be in the world at the time the new HighScore actor is placed into the world (it may not need to, in your particular case; but to make the class re-usable, it might be good to address the possibility); * the 'if' condition on line 28, 'if (me != null)' should never be 'false' if the 'if' condition on line 25 was 'true'; the UserInfo object returned will be a default object for the user if one was not previously saved for that user.
ValeroDeniro ValeroDeniro

2015/2/26

#
You're amazing. Thank you. @danspot and everyone else. I'll defo come back for more!
You need to login to post a reply.