using trace() in WonderFl

by bradsedito
All architectural.synthesis's idea and code.
Just sharing =)
♥1 | Line 67 | Modified 2012-06-24 08:49:35 | MIT License
play

ActionScript3 source code

/**
 * Copyright bradsedito ( http://wonderfl.net/user/bradsedito )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/uvPI
 */






 package 
 {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.text.TextField;
    
    public class TraceTest extends Sprite 
    {
        
        
        public function TraceTest() 
        {            
            //This is all the initialization you need in your main class
            inittrace(stage);
            
            // demonstration
            trace( "...Traced. Bam. ...That's how we roll on Wonderfl.net  ;)" )
            //trace(Math.atan2(1000,0))
        } 
        
                
    }
}



/////  WONDERFL TRACE /////
// forked from architectural.synthesis's flash on 2012-6-23


// Howto:
// 1. cut and paste everything bellow the "WONDERFL TRACE" line to the end of your script
// 2. insert inittrace(stage); to the constructor of your main class
// 3. done!
import flash.display.Sprite;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;


function inittrace(s:Stage):void
{
    WTrace.initTrace(s);
}

//global trace function
var trace:Function;

//wtreace class
class WTrace
{
        private static var FONT:String = "Fixedsys";
        private static var SIZE:Number = 12;
        private static var TextFields:Array = [];
        private static var trace_stage:Stage;
        
        public static function initTrace(stg:Stage):void
        {
            trace_stage = stg;
            trace = wtrace;
        }
        
        private static function scrollup():void
        {
            // maximum number of lines: 100
            if (TextFields.length > 100) 
            {
                var removeme:TextField = TextFields.shift();
                trace_stage.removeChild(removeme);
                removeme = null;
            }
            for(var x:Number=0;x<TextFields.length;x++)
            {
                (TextFields[x] as TextField).y -= SIZE*1.2;
            }
        }
    
        public static function wtrace(... args):void
        {
        
            var s:String="";
            var tracefield:TextField;
            
            for (var i:int;i < args.length;i++)
            {
                // imitating flash:
                // putting a space between the parameters
                if (i != 0) s+=" ";
                s+=args[i].toString();
            }
            

            tracefield= new TextField();
            tracefield.autoSize = "left";
            tracefield.text = s;
            tracefield.y = trace_stage.stageHeight - 20;

            var tf:TextFormat = new TextFormat(FONT, SIZE);
            tracefield.setTextFormat(tf);
            trace_stage.addChild(tracefield);
            scrollup();                      
            TextFields.push(tracefield);
            
        }
}