How do I get my counter to work? Please include explanation, and feel free to comment.
CONFUSION: I'm confused about the MovieClip container in as3. public class NumbersView extends MovieClip, so that seems to be a container. That may not be the problem, but that's where I got lost.
The working version of this code is fantastic. My
code bellow is an attempt at revising it. The source
has been adapted from various smart people. It's a work in progress
LIBRARY OBJECT
'vertical number column'
Name: mc-NumberImage
Class: NumberImage
OUTPUT ERRORS 'When I uncomment the code"
1023: Incompatible override. NumbersView...
1021: Duplicate function definition. NumbersView...
//NUMBER DOCUMENT CLASS
//IMPORT
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.utils.Dictionary;
import caurina.transitions.Tweener;
//COUNTER
var timer:Timer = new Timer(1000);
var count:int = 0;
var fcount:int = 0;
timer.addEventListener(TimerEvent.TIMER, incrementCounter);
timer.start();
function incrementCounter(event:TimerEvent) {
count++;
fcount=int(count*count/1000);
}
function formatCount(i:int):String {
var fraction:int = i % 100;
var whole:int = i / 100;
return ("000000000" + i).substr(-9, 9);
}
//------------------------------------------------------------------------
//PROBLEM AREA
function enterFrameHandler(e:Event):void
{
// numbers.setTime(formatCount(fcount));
}
var _listItems:Array = new Array();
var previousNums:Array;
const numHeight:int = 120;
var NumbersView:Sprite = new Sprite();
//var numbers:NumbersView = new NumbersView;
//NUMBERSVIEW
// function NumbersView($n:int):void {
_listItems = new Array();
previousNums = new Array();
var item:NumberImage;
var offset:int = _listItems.length;
for (var i:Number = 0; i < 9; i++) {
item = new NumberImage();
// }//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//------------------------------------------------------------------------
//PUSH ARRAY
addChild(item);
item.x = i * item.width;
_listItems.push(item);
}
//TWEENER 'Y SCROLLING'
function setTime($number:String):void {
var nums:Array = $number.split("");
for (var i:Number = 0; i < nums.length; i++) {
if (nums[i] == previousNums[i]) continue;
Tweener.removeTweens(_listItems[i]);
var newY:int = int(nums[i]) * -numHeight;
if (_listItems[i].y < 0) _listItems[i].y = numHeight;
Tweener.addTween(_listItems[i], { y:newY, time:3 } );
}
previousNums = nums;
}
D E S C R I P T I O N
COUNTER: formatCount needs to get passed to Tweener
STAGE: for loop, sets up children to be added to the stage NumbersView becomes item, item array adds children to stage
TWEENER: advances the vertical number column every time the count fires adds and removes
WHY
- Learning
- Benifit in simplifying the classes and putting it in one FLA
alt text http://www.ashcraftband.com/myspace/videodnd/bobnums.jpg
MORE ABOUT THE DOCUMENT CLASS
http://www.rubenswieringa.com/blog/class-syntax-in-actionscript30
http://www.heaveninteractive.com/weblog/2008/03/04/introduction-to-the-document-class-in-actionscript-30-tutorial
AFTER DEBU'S EXAMPLE
.FLA 'with symbol in library, Class: NumberImage'
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer;
var count:int = 0;
var fcount:int = 0;
var numbers:NumbersView;
trace("-----new NumberDocument created");
timer = new Timer(10);
timer.addEventListener(TimerEvent.TIMER, incrementCounter);
timer.start();
numbers = new NumbersView();
addChild(numbers);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
//addEventListener(Event.ADDED_TO_STAGE, traceMeOut);
function incrementCounter(event:TimerEvent)
{
count++;
fcount=int(count*count/1000);//starts out slow... then speeds up
}
function formatCount(i:int):String
{
return ("000000000" + i).substr(-9, 9);
}
function enterFrameHandler(e:Event):void
{
numbers.setTime(formatCount(fcount));
}
function traceMeOut()
{
trace("-----Im here on stage!");
}
NumbersView.as
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.events.Event;
import caurina.transitions.Tweener;
public class NumbersView extends MovieClip
{
private var _listItems:Array;
private var previousNums:Array;
private const numHeight:int = 120;
public function NumbersView()
{
_listItems = new Array();
previousNums = new Array();
var item:NumberImage;
for (var i:Number = 0; i < 9; i++) {
item = new NumberImage();
addChild(item);
item.x = i * item.width;
_listItems.push(item);
}
}
public function setTime($number:String):void {
var nums:Array = $number.split("");
for (var i:Number = 0; i < nums.length; i++) {
if (nums[i] == previousNums[i]) continue;
Tweener.removeTweens(_listItems[i]);
var newY:int = int(nums[i]) * -numHeight;
if (_listItems[i].y < 0) _listItems[i].y = numHeight;
Tweener.addTween(_listItems[i], { y:newY, time:3 } );
}
previousNums = nums;
}
}
}