Search code examples
flashactionscript-3

"lost in strings" AS3


Can someone follow what doing, and show me how to do it correctly? I'm new with strings and arrays. Anything you can suggest would be appreciated.

GOAL
I want script to calling display objects to the stage, and the timer to move images up and down to appear like a scrolling numbers. I start getting sloppy, and mess up passing to Tweener.

THOUGHTS
- An explanation or strategy may be enough, "I've got pretty close on this one" - There was confusion regarding the Containers and addChildren...looked like arrays

THE CODE "get as far as error with NumbersView and numbers undefined etc"

 //-------------------------IMPORT METHODS---------------------------------------
        import flash.display.DisplayObject; 
        import flash.display.MovieClip; 
        import flash.utils.Dictionary; 
        import flash.events.Event; 
        import caurina.transitions.Tweener; 

//-----------------------TIMER---------------------------------------       
    var timer:Timer = new Timer(1000);//
    //var timer:Timer;  
    var count:int = 0; 
    var fcount:int = 0; 
    var _listItems:Array = new Array();
    var previousNums:Array;
    const numHeight:int = 120;
    //var numbers:NumbersView;
    timer.addEventListener(TimerEvent.TIMER, incrementCounter);  
    timer.start(); 

//-----------------------COUNTER-CONT-----------------------
    function incrementCounter(event:TimerEvent) {  
      count++;  
      fcount=int(count*count/1000);//starts out slow... then speeds up 
    //  mytext.text = formatCount(fcount);
    NumbersView(1);
    //}

//----------------------ZERO PLACE HOLDERS-----------------------
    }
    function formatCount(i:int):String { 
         var fraction:int = i % 100; 
         var whole:int = i / 100;  
      return ("000000000" + i).substr(-9, 9); 
       // return ("0000000" + whole).substr(-7, 7) + "." + (fraction < 10 ? "0" + fraction : fraction); 
    } 

    //

//----------------------DISPLAY for loop, integer to string "puts numbers on stage"
    function NumbersView($n:int):void {
     //function NumbersView()//
     //{
        _listItems = new Array();
        previousNums = new Array();
        var item:NumberImage;
        var offset:int = _listItems.length;
        //for (var i:int = 0; i < $n; i++)
     for (var i:Number = 0; i < 9; i++)
        {
           item = new NumberImage();
           //item.x = (i + offset) * 9;
           //item.y = (i + offset) * 9;
        item.x = i * item.width; 
           _listItems.push(item); 
           addChild(item);
        }

//----------------------SPLIT STRING "pass to Tweener or some other method"---------------
    //
       function setTime($number:String):void { 
                var nums:Array = $number.split(""); 
                for (var i:Number = 0; i < nums.length; i++) { 
                if (nums[i] == NumbersView[i]) continue; 
                Tweener.removeTweens(NumbersView[i]); 
       }

The methods were proved separately. "I got a earlier version of the document going, but I need to work through this to understand it."

SYMBOL PROPERTIES
CLASS ImageView

TWEENER
The "caurina" folder needs to be present

ROLLING NUMBERS "SUCCESS loading numbers and connecting to counter"

--------------------------------------------------------------------------------------------
What I did for Debu's example
Placed the movie clip object on stage with two dynamic text fields in it.
Note
Make sure the container is placed on stage, has the two text fileds in it, and everything is given a proper instance name. Also include the caurina folder.

seconds MovieClip symbol with instance name of seconds
firstDigit Dynamic text field with instance name of firstDigit, placed in seconds
secondDigit Dynamic text field with instance name of secondDigit, placed in seconds

Symbol
Un-tick 'Export for ActionScript'
Use 'Name, Class, and Instance' correctly


Solution

  • I've implemented a very simple example of what I believe you're trying to do. It only contains one digit, the seconds, and simply counts up to 9, then back to 0 and up to 9 again. Each time the Timer object I've created is fired (every 1000ms, or 1s) it runs a function which tweens in the new digit. I've pasted the code below, and commented each line; let me know if you don't understand any of it.

    import caurina.transitions.*;
    
    //Create a timer that will run for 1 second, and repeat
    var secondTimer:Timer = new Timer(1000);
    
    //A counter for seconds
    var secondsCount:int = 0;
    
    //The original position of the rolling numbers object on the stage
    var originalYPosition:Number = 0;
    //The position of the rolling numbers object on stage when it's displaying its second digit
    var upwardYPosition:Number = -127;
    //The speed you want the odometer to roll at. As this is the seconds counter it needs to be less than 1
    var rollSpeed:Number = 0.25; //seconds
    
    //Start the timer
    secondTimer.start();
    //Add an event listener to fire each time it reaches its limit
    secondTimer.addEventListener(TimerEvent.TIMER, updateOdometer);
    
    //Add a generic update loop, to test for when the odometer has been moved to its 'up' position
    addEventListener(Event.ENTER_FRAME, checkOdometerPosition);
    
    function updateOdometer(event:TimerEvent):void
    {
        //When the timer fires, check if secondsCount is less than 9; if it is, increment secondsCount
        if (secondsCount < 9)
        {
            secondsCount++;
        }
        else 
        {
            //Otherwise set secondsCount to 0, so the odometer will loop from 9 back to 0
            secondsCount = 0;
        }
    
        //Set the text inside seconds > secondDigit equal to the next digit it needs to display.
        seconds.secondDigit.text = secondsCount.toString();
    
        //Do the tween, so that the secondDigit changed above will become visible
        Tweener.addTween(seconds, { y: upwardYPosition, time: rollSpeed, transition:"easeOutQuad" } );
    }
    
    //This function runs all the time, to check when the rolling object has moved upwards
    function checkOdometerPosition(event:Event):void
    {
        //Do the check to see if it's in its upward position..
        if (seconds.y <= upwardYPosition)
        {
            //If it is, make the first digit equal to the current second digit
            seconds.firstDigit.text = secondsCount.toString();
            //Then move the odometer back into its original position..
            seconds.y = originalYPosition;
    
            //This is so that the odometer is ready to be moved upwards again when the timer fires again.
        }
    }
    

    All this code requires is a simple MovieClip object on the stage with two dynamic TextFields inside it; one placed exactly below the other. I've named the containing object 'seconds', the top-most TextField 'firstDigit', and the bottom TextField 'secondDigit'. Re-create this inside a Flash document, save it (making sure carina is in the same folder), paste the code in on frame 1, and it should work fine. Edit: I created an image to illustrate how the objects should be structured inside your .fla:

    alt text

    Oh, and as for that expert who told you to quote yourself, he doesn't sound like such an expert to me ;) Anyway, hope it helps!