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

2014/6/27

Animated objects

OlSom OlSom

2014/6/27

#
I'm creating a game with a tank, and so far I have at least three animated effects in mind: muzzle blast, exhaust and the shell's impact. Now obviously these effects won't do much, they'll go through a couple of images then remove themselves. I wanted to create an Effects class that would do just that, with a MuzzleBlast, Exhaust, and Impact subclass. I wanted to load images in the subclass into a GreenfootImage array and send it to the Effects class. This was my first attempt at it: Effects:
private boolean moving;
    private int timerDone;
    private int timer = 0;
    private GreenfootImage[] imageArray;
    
    public Effects(int rotation, boolean _moving, GreenfootImage[] _imageArray)
    {
        setRotation(rotation);
        moving = _moving;
        imageArray = _imageArray;
        timerDone = imageArray.length*10;
    }
    
    public void act() 
    {
        //animation code here
    }    
MuzzleBlast:
private GreenfootImage[] imageArray = {
    new GreenfootImage("fire1.png"),
    new GreenfootImage("fire2.png"),
    new GreenfootImage("fire3.png"),
    new GreenfootImage("fire4.png"),
    };
        
    public MuzzleBlast(int rotation, boolean moves)
    {
        super(rotation, moves, imageArray);
    }
MuzzleBlast won't compile: "cannot reference imageArray before supertype constructor has been called" Am I making a simple mistake here or this cannot be done through the constructor and I'll have to call another function after super() to send the images?
danpost danpost

2014/6/27

#
You can make the imageArray field 'static' (and 'final') without causing any problems.
OlSom OlSom

2014/6/27

#
Thanks. Meanwhile I also realized that I was overcomplicating things with the subclasses. I can simply load the images in the objects that create the effects (tank's hull, turret, and projectile) and just create a new Effects with those. If I understand correctly that should also make things more efficient since the images are only loaded once, when the tank is created, not every time the effect is appears.
danpost danpost

2014/6/27

#
OlSom wrote...
If I understand correctly that should also make things more efficient since the images are only loaded once, when the tank is created, not every time the effect is appears.
'static' fields that are assigned their values when declared are set during compilation; when a tank is created is of no consequence.
You need to login to post a reply.