By rescue86k
The Counter class: If you completed the 12 part base series, then you know what .as this is. I use the Counter class to show my hero’s health and ammo.
I found the class lacking a few basic functions so I added them.
Here’s the original class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package { 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 setValue( amount:Number ):void { currentValue = amount; updateDisplay(); } public function reset():void { currentValue = 0; updateDisplay(); } public function updateDisplay():void { } } } |
The new one’s here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package { 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 removeToValue( amountToremove:Number ):void { currentValue = currentValue - amountToremove; updateDisplay(); } public function reset():void { currentValue = 0; updateDisplay(); } public function updateDisplay():void { } public function setValue( amount:Number ):void { currentValue = amount; updateDisplay(); } public function get CurrentValue():Number { return currentValue; } } } |
So I added the functions CurrentValue(), and removeToValue():
- removeToValue- used to remove X amount from “Score” for example.
- CurrentValue- returns the current value of the subclass(such as gameScore in avoider game)
Example use for CurrentValue():
if (gameScore.CurrentValue >= 100){ //You win the game! (if goal was to score 100 pts) } |
Put this code in your avoidergame.as.
Keep in mind my game is way past the original version of avoidergame, so if the code puts out some funny errors such as “Can’t find the method/ or property of a null object reference” then dig in deep into the heart of OOP. I mean spend time understanding how to correctly reference objects. Is gameScore defined as a variable in your .as? Is it a child of that class? Learn your game and how you built it.

(1 votes, average: 4.00 out of 5)
[...] first, Win the Game Based On Score, is a brief tutorial explaining how to modify the Counter class from the base to add a few new [...]