flash on 2012-4-27

by tepe
♥0 | Line 116 | Modified 2012-04-28 12:31:39 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;
    
    public class Main extends Sprite{
        private var st:scrollText = new scrollText();
        public function Main(){
            graphics.beginFill(0xaaffaa);
            graphics.drawRect(100,100,40,40);
            graphics.endFill();
            stage.addEventListener(MouseEvent.MOUSE_DOWN,function():void{ 
                addChild(st);
                st.setTextField(466,50);
                st.speed = 3;
                st.insert("bb");
                
                
            });
            
            st.addEventListener(Event.COMPLETE,function():void{
                st.insert("aaaaa");
            });

        }       
    }
}

//-------------------
import flash.utils.*;
    import flash.filters.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    class scrollText extends Sprite{
        private var m:Sprite = new Sprite();
        private var tf:TextField = new TextField();
        public var speed:Number = 1;
        private var list:Array = new Array();

        public function scrollText(){
            m.graphics.beginFill(0xff0000,1);
            m.graphics.drawRect(40,0,100,20);
            m.graphics.endFill();
            addChild(m);
            tf.mask = m;


            addEventListener(Event.ENTER_FRAME,onEnter);
        }


        //待ち行列に追加
        public function add(str:String,col:uint=0x000000):int{
            var str2:String;
            if(col != 0x000000){
                str2 = new String();
                str2 += '<font color="#'+col.toString(16)+'">';
                str2 += str;
                str2 += '</font>';

                str = str2;
            }

            list.push(str);
            if(list.length==1){ 
                addEventListener(Event.ENTER_FRAME,onEnter);
                tf.htmlText = list[0];
                tf.width = tf.textWidth;
                tf.x = m.width;
            }
            return list.length;
        }
        //待ち行列削除
        public function clear():void{
            list.length = 0;
        }
        //今流れているテロップをキャンセルし新しいテロップを表示する。キャンセルしたテロップは次に流れる。
        public function insert(str:String,col:uint=0x000000):void{
            var str2:String;
            if(col != 0x000000){
                str2=new String();
                //str2 = strA;
                str2 += '<font size="7" color="#'+col.toString(16)+'">';
                str2 += str;
                str2 += '</font>';
                //str2 += strB;
                str = str2;
            }
            tf.htmlText = str;
            tf.width = tf.textWidth+20;
            tf.x = m.width+30;
            //addEventListener(Event.ENTER_FRAME,onEnter);
            
        }
        //今流れているテロップを終了し、次のテロップを表示
        public function skip():void{
            if(0 < list.length){ 
                list.shift();
                tf.htmlText = list[0];
                tf.width = tf.textWidth;
                tf.x = m.width;
            }
            else{ 
                dispatchEvent(new Event(Event.COMPLETE) );
                //removeEventListener(Event.ENTER_FRAME,onEnter);
            }
        }

        public function setTextField(w:Number=100,h:Number=20,t:TextField=null):void{
            
            if(t != null)tf=t;

            tf.mask = m;
            tf.text = " ";
            tf.selectable = false;
            tf.multiline = false;
            tf.wordWrap = false;
            tf.width=tf.textWidth;
            tf.height=h;
            addChild(tf); 
            
            m.graphics.clear();
            m.graphics.beginFill(0xff0000);
            m.graphics.drawRect(0,0,w,h);
            m.graphics.endFill();
 
        }
        
  



        private function onEnter(e:Event):void{
            tf.x -= speed;
            if(tf.x+tf.width < 0){ 
                if(0 < list.length){ 
                    list.shift();
                    tf.htmlText = list[0];
                    tf.width = tf.textWidth;
                    tf.x = m.width;
                }
                else{ 
                    dispatchEvent(new Event(Event.COMPLETE) );
                    //removeEventListener(Event.ENTER_FRAME,onEnter);
                }
                

            }
            

        }
    }