views:

204

answers:

2

How do I reset my numbers after they count? I want something like an onComplete function.

DESCRIPTION
My animation advances 120 pixels from it's current position, then flys off the stage. It was looping, and would yoyo to the bottom before advancing. I don't want my numbers yoyoing or flying off the stage. My numbers must move 120 pixels forward each count, then return.

alt text

NumbersView.as 'the code works, but in a messed up way as described'

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(); 
   //Tweener.init();

            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(""); 
            //trace("$number = " + $number);
            for (var i:Number = 0; i < nums.length; i++) { 
                if (nums[i] == previousNums[i]) continue; 
                Tweener.removeTweens(_listItems[i]);    

                //newY:int = -numHeight;
    var newY:int = int(nums[i]) * -numHeight;
    trace("newY = " + newY);
                trace("currY = " + _listItems[i].y);

    /*----------------------PROBLEM AREA, RIGHT HERE------------------------*/
                //if (_listItems[i].y < 0) _listItems[i].y = numHeight;//
                //Tweener.addTween(_listItems[i], { y:newY, time:3 } );//
    Tweener.addTween(_listItems[i], { y:_listItems[i].y+newY, time:3 } );//
            } 
            previousNums = nums; 
        } 
    } 
} 

Tweener Example
http://hosted.zeh.com.br/tweener/docs/en-us/parameters/onComplete.html

**oopse!
//crap code was' s note not to use it', not a comment against anyone's code


DOCUMENT CLASS
Publish Settings/Flash/Settings 'Advanced ActionScript 3.0 Settings'/Document class:NumbersView

CLASS Symbol 70x1080, numbers 70x120
Library/'right-click' Properties/Class:NumberImage

A: 

The most logical solution I can think of from the top of my head is to extend the image that contains all your numbers so that it has a 9 at each end, i.e.: after the 8 there's a 9 at the top end of the image, but before the 0 there's also a 9, at the bottom of the image.

Then you just need to do some tests to find out what positions on the stage your image will be in at each point of it's cycle. You need to know the position it will be in when displaying either of the 9s, at either end of the image. Then you simply put in an if-statement to test when the object reaches the position which means it's displaying the top-most 9, and when that equals true, make the image's position equal to the bottom-most 9. When you do this change, don't use Tweener, as you want it to snap down to the other 9 position in one frame, so the viewer isn't aware of it.

Hope you see what I'm saying. Your code confuses me, so I'm not really sure (without spending too long trying to understand it) where you'd put this logic in, but I assume you'd know.

debu
+1  A: 

It took me a little while to figure out what you're really trying to get as an end result, but I think I figured it out. I split this functionality into 2 classes. The NumbersView class is still the document class, but I also made a NumberImage class that you can attach to your 0-9 numbers movieclip in your library. Here's the code:

//NumbersView.as - Your Document Class
package {

    import flash.display.MovieClip;

    public class NumbersView extends MovieClip {

        private var _listItems:Array; 
        private const numHeight:int = 120; 

        public function NumbersView()  
        { 
            _listItems = 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); 
            }

            setTime('123456789');

        }

        public function setTime($number:String):void { 
            var nums:Array = $number.split(""); 
            trace(nums);

            for (var i:Number = 0; i < nums.length; i++) { 

                _listItems[i].start( int(nums[i]) );

            } 

        } 


    }
}

The next class is the NumberImage.as class that you can hook up to your movieclip by right-clicking the item in the library, "Properties...", Check "Export for ActionScript", and put in NumberImage for the Class name.

//NumberImage.as - Linked to the NumberImage movieclip in the Library    
package {

    import flash.display.MovieClip;
    import caurina.transitions.Tweener;

    public class NumberImage extends MovieClip {

        public var count:int;

        public function NumberImage() {
            count = 0;
        }

        public function start( num:int = 0 ):void {
            this.y = -120*num;
            count = num;
            moveNumbers();
        }

        public function moveNumbers():void {
            count++;
            if(count % 10 == 0) {
                Tweener.addTween( this, {y: this.y + 1080, time:1, onComplete:moveNumbers});
            } else {
                Tweener.addTween( this, {y: this.y - 120, time:1, onComplete:moveNumbers});
            }
        }

    }
}

Hopefully this makes more sense and it's easier to decipher now that the functionality of the NumberImage objects are handled by themselves rather than by their containing class.

walpolea
Also, Thanks for the help with the XML.
VideoDnd
Good idea. The classes are well put together. Ya got me thinking! Anyway, the 'document class' advances, and 'class' scrolls the numbers. My animations will need to extend my classes for complex motions. Thanks.
VideoDnd