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

2016/6/16

Programming Best Practice in Animation: Which is better?

gmacias gmacias

2016/6/16

#
Hi. Just wondering what is the best way to declare and assign variables for animating objects. Both work but I not sure which is better. What is the difference? Why should I choose one over the other?
public class Plane extends Actor
{
   private GreenfootImage image1;
   private GreenfootImage image2;
   
   public Plane()
    {
       image1 = new GreenfootImage("plane1.png");
       image2 = new GreenfootImage("plane2.png");
    }
------------------------------------------------------------ OR ----------------------------------------------------------
public class Plane extends Actor
{
   private GreenfootImage image1 = new GreenfootImage("plane1.png");
   private GreenfootImage image2 = new GreenfootImage("plane2.png");
   
   public Plane()
    {

    }
danpost danpost

2016/6/16

#
gmacias wrote...
Hi. Just wondering what is the best way to declare and assign variables for animating objects. Both work but I not sure which is better. What is the difference? Why should I choose one over the other? < Code Omitted >
Actually, for what you gave, there is really little difference. However, if you are going to have more than one Plane object in a world at a time, you might consider this (especially if the images are large ones or if you allow a large number of Plane objects in a world):
private static GreenfootImage image1 = new GreenfootImage("plane1.png");
private static GreenfootImage image2 = new GreenfootImage("plane2.png");
or
private static GreenfootImage[] images =
{
    new GreenfootImage("plane1.png"),
    new GreenfootImage("plane2.png")
};
As 'static', only one set of images is created for ALL the Plane objects instead of each Plane object having its own two images to use. Using an array could be helpful as then the images are numbered by the position in the array.
gmacias gmacias

2016/6/17

#
Good to know. Thank you for your help! :)
You need to login to post a reply.