0
votes

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; 
        } 
    } 
} 
1

1 Answers

0
votes

I'm a little confused about what you're trying to do, but my guess is you want to recreate what's happening in numbers_works in your own file, numbers_broke. To do that, put this code on your first frame:

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!");
}

Now, the example you're trying to recreate uses external class files. One of them is called 'NumbersView.as'. This needs to be in the same directory as your fla file. If you copy this in over your code, and put NumbersView.as in the right place, your file should do what the other does - I can only assume that's what you're trying to do.. Which leads me onto a few other pointers:

Your questions, this one especially, are often incredibly difficult to decipher. Like with this, what you are even trying to do, and where is the problem? Secondly, your code is awful, and I mean in a visual sense. It's really important to write well structured, clearly formatted code. You'll find it easier to work with, easier to come back to, and other people will find it much easier to understand in the first place. Right now your formatting is all over the place. Thirdly, when you say you're getting errors, paste the whole error, not just the beginning - nobody knows what the real problem is with:

1023: Incompatible override. NumbersView...

So if you want helpful answers, I'd say starting with basics like those would be really useful to you.

Finally, in your original code inside numbers_broken, you wrote at the top that this was the Document Class. The Document Class in Flash is a specific class, which is implemented in the correct version of the numbers files you supplied. It's specified in the properties panel of the fla file, where you see 'Class:'. It needs to start with a capital, and once you've specified it you need to go and create that file externally, with the same name you put in that box, but appended with the .as (ActionScript) file type. You then need certain elements, like a class declaration and a constructor; a generic Document class might look like this:

package
{
    public class Main extends Sprite
    {
    //Property variables go here, eg:
    var someVariable:String = "Hello";

         //This is the constructor
         public function Main()
         {
              //Code goes here, which will be the first execution of your Flash file.
         }
}

From the posts of yours that I've answered, I'm getting the impression that you haven't yet fully grasped the concept of Object Oriented Programming, and where Classes come into your coding. If you'd like I can give you a more thorough explanation. In the mean time, I hope this solves your current issues.