fox-handler
fox-handler

Reputation: 131

questions about supers and subclasses

I'm new to actionscript so please bear with me.

I just started working with supers and subclasses.

My question is basically this: Why is it that when I use the line scoreDisplay.text = currentValue.toString();

it is run with the line reset();

Why cant it be run with say current value = 0 instead?

here is the super

import flash.display.MovieClip;
public class Counter extends MovieClip
{
    public var currentValue:Number;

    public function Counter()
    {
        reset();
    }

    public function addToValue( amountToAdd:Number ):void
    {
        currentValue = currentValue + amountToAdd;
        updateDisplay();
    }

    public function reset():void
    {
        currentValue = 0;
        updateDisplay();
    }

    public function updateDisplay():void
    {

    }
 }
}

and the subclass

import flash.text.TextField;
public class Score extends Counter
{
    public function Score()
    {
        super();
    }

    override public function updateDisplay():void
    {
        super.updateDisplay()
        scoreDisplay.text = currentValue.toString();
    }
}

}

Upvotes: 1

Views: 53

Answers (1)

Zhafur
Zhafur

Reputation: 1624

To answer your question, if you only put currentValue = 0; in your Counter class' constructor, then you don't update the display list. Your reset function however does call the updateDisplay function. So if you call super(); from a subclass which overrides the updateDisplay function, then the super class will call the reset which will call the updateDisplay OF your subclass.
Also, in your current example, there is no meaning of calling the super's updateDisplay function because it contains nothing.
The way it is at the moment, is actually a better solution than just resetting the values in the constructor, because you can call that reset function anytime without calling the constructor again which could contain other elements aswell. I would never use the constructor as a "resetter".

Upvotes: 1

Related Questions