A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: [CS4]Timer inside function

  1. #1
    Member
    Join Date
    Dec 2009
    Posts
    84

    Question [CS4]Timer inside function

    This is probably a simple fix, but I haven't used timers much. What I need is basically a pause inside of a function. I was hoping the following would work, but sadly it did not.
    Actionscript Code:
    import flash.utils.Timer;

    var myDelay:Timer = new Timer(100);
    myDelay.start();
    for(var i:uint = 0;i<100;i++){
        myDelay.start();
        trace("Hello");
    }

    If it isn't clear what I am trying to do here, I just want it to trace "Hello" 100 times with a set amount of time between each trace. Any suggestions? Thanks

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Use the timer and TimerEvent instead of a loop.
    Code:
    var myDelay:Timer = new Timer(100, 100);
    myDelay.addEventListener(TimerEvent.TIMER, tick);
    
    function tick(e:TimerEvent):void{
      trace("hello");
    }
    You mention wanting to pause a function. You can't do that.

  3. #3
    Member
    Join Date
    Dec 2009
    Posts
    84
    Is there a way to set it up a little differently? The way I am actually needing to use it is inside a function. I have a function that is malfunctioning and I would like to see what it is doing. I think it is working right the first time, but then for some reason it runs through the function many more times and screwing things up. I want to try to put a timer inside the function so that it doesn't do everything so lightning fast.

    If you are curious as to what the function is, here is a dumbed down version of the program that is malfunctioning the same way.

    Actionscript Code:
    var totalWidth:uint;
    var maxWidth:uint = 100;
    var maxHeight:uint = 20;
    var myStr:String;
    var myArray:Array;
    var arrayLength:uint;
    var letter:TextField;
    var container:Sprite = new Sprite();
    var myFormat:TextFormat = new TextFormat;
    myFormat.font = "Arial";

    addChild(container);

    input.addEventListener(Event.CHANGE,create);
    function create(event:Event):void{
        myFormat.size = 30;
        creator(myFormat.size);
    }
    function checkWidth(totalWidth,letterHeight):Boolean{
        trace("Checking Font Size...");
        var size:Number = Number(myFormat.size);
        if(totalWidth>maxWidth){
            size-=1;
            trace("New Font Size is: "+size);
            creator(size);
            return false;
        }else if(letterHeight>maxHeight){
            size-=1;
            trace("New Font Size is: "+size);
            creator(size);
            return false;
        }else{
            return true;
        }
    }

    function creator(size):void{
        totalWidth = 0;
        myFormat.size = size;
        if(container.numChildren>0){
            trace("If Loop #1");
            for(var a:uint=container.numChildren; a>0; a--){
                container.removeChildAt(a-1);
            }
        }
        myStr = input.text;
        myArray = myStr.split("");
        arrayLength = myArray.length;
        for (var i:uint=0; i<arrayLength; i++){
            trace("For Loop #1");
            letter = new TextField();
            letter.autoSize = TextFieldAutoSize.LEFT;
            letter.text = myArray[i];
            letter.border = true;
            letter.setTextFormat(myFormat);
            letter.x = 100+totalWidth;
            letter.y = 100;
            totalWidth+=letter.width;
            if(checkWidth(totalWidth,letter.height)){
                container.addChild(letter);
            }else{
                trace("Else...");
                break;
            }
        }
        if(container.numChildren>0){
            trace("If Loop #2");
            for(var b:uint=0;b<container.numChildren;b++){
                //------ I would like to add a timer to delay this for loop
                trace("For #Loop2");
            }
        }
        trace("--------------");
    }

    Basically, what the program does is take a TextInput, splits it into an array, then places each letter into its own TextField on the stage. But before it places each TextField on the stage, it checks to see if the combined width of all of the letters is greater that a variable named maxWidth. If it is greater than maxWidth, then the font size is decreased until it is no longer greater than the maxWidth. The letters are then added to the stage.

    The program works fine until it needs to change the font size. Then it appears to work fine, but if you look at the output it is not working correctly. Once the letters are added to the stage via "container.addChild(letter);" it should drop down and perform the "if(container.numChildren>0){" function and then be done. Well once the font size changes it completes the if loop and returns to the else statement a few lines above and redoes the if loop. This happens quite a few times and I have no idea why. Any advice?

    However, the timer would help me for now allowing me to slow down the process and see if it is actually working the first time through.

    Thanks.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You don't want a timer, you want the debugger. Just step through the code.

    I'll take a look at the actual code and behavior a bit later.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm not sure I understand the intended output. Should it just fit a String into a certain physical size as individual letter textfields? Or should the individual letters get smaller, or should it produce multiple rows, or...?

    Can you attach a mockup of the output you would expect from the String "Well, that's big"?

  6. #6
    Member
    Join Date
    Dec 2009
    Posts
    84
    The intended output is just a line of text whose font size decreases to fit inside an area of a certain width (maxWidth).

    This is just a proof-of-concept program. The actual program takes those letters and does formatting with them like arching them or making them follow a line. When they are arched, I need them to decrease in size as the totalWidth reaches its limits.

    Here is a quick example of the arching program. If you enter all capital X's, you will see that once it gets to the 11th X, it needs to decrease in size and then it freaks out because of the previously stated problem.

    http://www.purelycustom.com/headset_start4.html

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well, one thing I see in the example code posted above is that there is no lower bound. Since each textfield has a minimum size, at some point, you just can't fit any more in. When that happens, the code above just goes into an infinite recursion continually making the font size smaller. If you put in a check for font size <= 0, you can stop the recursion.

    I'm not seeing a freakout similar to the one in your rounded example. I'm not sure where that's coming from.

    Instead of doing this recursively where creator calls checkwidth calls creator until things fit, you can place things, then try to fit them. Here's a non-recursive version. There's still a lot of things I'd change about this, but they aren't important to this issue.
    Code:
    	public class Main extends Sprite 
    	{
    		private var totalWidth:uint;
    		private var maxWidth:uint = 100;
    		private var maxHeight:uint = 20;
    		private var myStr:String;
    		private var letters:Array;
    		private var arrayLength:uint;
    		private var letter:TextField;
    		private var container:Sprite;
    		private var myFormat:TextFormat;
    		private var input:TextField;
    		
    		public function Main():void 
    		{
    			if (stage) init();
    			else addEventListener(Event.ADDED_TO_STAGE, init);
    		}
    		
    		private function init(e:Event = null):void 
    		{
    			removeEventListener(Event.ADDED_TO_STAGE, init);
    			// entry point
    			container = new Sprite();
    			container.y = 100;
    			container.graphics.lineStyle(0.1, 0x0);
    			container.graphics.drawRect(100, 100, maxWidth, maxHeight);
    			myFormat = new TextFormat();
    			myFormat.font = "Arial";
    
    			addChild(container);
    			input = new TextField();
    			input.type = TextFieldType.INPUT;
    			addChild(input);
    
    			input.addEventListener(Event.CHANGE,create);
    		}
    
    		private function create(event:Event):void{
    			creator(30);
    		}
    		
    		private function checkWidth(totalWidth:Number, letterHeight:Number):Boolean{
    			trace("Checking Font Size..."+totalWidth+', '+letterHeight);
    			if((totalWidth>maxWidth) || (letterHeight>maxHeight)){
    				return false;
    			}else{
    				return true;
    			}
    		}
    
    		private function creator(size:int):void {
    			if (size < 0) {
    				throw new ArgumentError("can't have negative size!");
    			}
    			makeEmpty(container);
    			letters = [];
    			totalWidth = 0;
    			myFormat.size = size;
    			myStr = input.text; 
    			var myArray:Array = myStr.split("");
    			arrayLength = myArray.length;
    			for (var i:uint=0; i<arrayLength; i++){
    				trace("For Loop #1");
    				letter = new TextField();
    				letter.autoSize = TextFieldAutoSize.LEFT;
    				letter.text = myArray[i];
    				letter.border = true;
    				letter.setTextFormat(myFormat);
    				letter.x = 100+totalWidth;
    				letter.y = 100;
    				totalWidth+=letter.width;
    				container.addChild(letter);
    				letters.push(letter);
    			}
    			while(size > 0 && !checkWidth(totalWidth, letter.height)){
    				size--;
    				myFormat.size = size;
    				trace("didn't fit, adjusting to new size: "+size);
    				adjustLetters();
    			}
    			if (size <= 0) {
    				trace("couldn't fit.");
    			}
    			if(container.numChildren>0){
    				trace("If Loop #2");
    				for(var b:uint=0;b<container.numChildren;b++){
    					//------ I would like to add a timer to delay this for loop
    					trace("For #Loop2");
    				}
    			}
    			trace("--------------");
    		}
    		
    		private function adjustLetters():void {
    			trace("adjustLetters");
    			totalWidth = 0;
    			for (var i:int = 0; i < letters.length; i++) {
    				var l:TextField = letters[i];
    				l.x = 100 + totalWidth;
    				l.setTextFormat(myFormat);
    				totalWidth += l.width;
    			}
    		}
    		
    		private function makeEmpty(d:DisplayObjectContainer):void {
    			while (d.numChildren > 0) {
    				d.removeChildAt(0);
    			}
    		}
    
    	}
    Obviously, you can take that and turn it into frame script.

  8. #8
    Member
    Join Date
    Dec 2009
    Posts
    84
    Well if you can think of a better way of doing this, I am totally open to help. Everything I know about flash I have pretty much just found myself. I am pretty sure that a lot of the stuff I do is not the easiest or the best way of doing it. If you have the time I would love to see how you would suggest doing it. Thanks.

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Um... I did post a different way of doing it.

    I don't know how you're encoding your path for the circular example you posted. If you want it to be absolutely generic, you'd have some sort of parametric equation and evenly space the letters on it. But that wouldn't fit with the left justified behavior you seem to be going for. If you're just trying to do a circular path, you don't have to be perfectly generic and can cheat a bit, by assuming that the circumference is large compared to letter height and just place letters one letter width apart from each other on the circumference.

    Or were you referring to trying to 'pause' the function to see what it's doing? If so, just use the built in debugger. I don't use the Flash IDE unless I have to, but I know that it has one. You should be able to step through code and monitor values.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center