• 新人补钙系列教程之:一天一招让你的代码越来越好


    不够优秀的代码:

    1. /**
    2. *
    3. * ActionScript 3.0 需要多个类同时工作来载入和回放Flash视频。你必须使用NetStream对象载入视
    4. * 频并控制回放,但是NetStream类只关心如何读取数据,至于这些数据是什么内容并不知道,因
    5. * 此就需要Video对象,Video对象得到NetStream的数据并显示到屏幕上。  
    6. * 
    7. * 
    8. * NetStream构造函数需要一个NetConnection对象作为参数,NetConnection对象关联将要播放的视频
    9. *
    10. *  
    11. */
    12. package tlg.tool 
    13.         
    14. {
    15.         
    16.         import flash.display.Sprite;
    17.         import flash.events.Event;
    18.         import flash.events.MouseEvent;
    19.         import flash.events.NetStatusEvent;
    20.         import flash.geom.Rectangle;
    21.         import flash.media.Video;
    22.         import flash.net.NetConnection;
    23.         import flash.net.NetStream;
    24.         import flash.text.TextField;
    25.         import flash.text.TextFieldAutoSize;
    26.         
    27.         import tlg.customEvent.SenceChooseInfoEvent;
    28.         
    29.         public class PlayVideo extends Sprite {
    30.                 
    31.                 // 
    32.                 public var _stream:NetStream;
    33.                 //
    34.                 private var _video:Video;
    35.                 //
    36.                 private var connection:NetConnection;
    37.                 
    38.                 public var _duration:uint;
    39.                 
    40.                 // 视频链接地址
    41.                 public var vidoUrl:String;
    42.                 
    43.                 private var _scrubbing:Boolean;
    44.                 
    45.                 private var _track:Sprite;
    46.                 
    47.                 private var _thumb:Sprite;
    48.                 
    49.                 public function PlayVideo( )                 
    50.                 {
    51.                         _video = new Video(160, 120);        
    52.                         addChild(_video);
    53.                         
    54.                         _duration = 0;
    55.                         
    56.                         connection= new NetConnection( );                        
    57.                         connection.connect(null);                        
    58.                         
    59.                         _stream = new NetStream( connection );        
    60.                         _stream.play( vidoUrl );
    61.                         
    62.                         var client:Object = new Object( );
    63.                         
    64.                         // 使用回调函数
    65.                         client.onMetaData = onMetaData;                        
    66.                         client.onCuePoint = onCuePoint;
    67.                         
    68.                         _stream.client = client;
    69.                         
    70.                         // ----------
    71.                         
    72.                         _video.attachNetStream( _stream );                                
    73.                         
    74.                         _stream.addEventListener(NetStatusEvent.NET_STATUS, statusHandler); 
    75.                         
    76.                         
    77.                         //------------------------------ 添加控制条 ---------------------
    78.                         if ( _track == null ){
    79.                         
    80.                                 videoScrubber ( );
    81.                         
    82.                         }
    83.                         
    84.                 }                
    85.                 
    86.                 private function onMetaData(data:Object):void {
    87.                         
    88.                         _duration = int ( data.duration );
    89.                         
    90.                         // 输出影片 原数据
    91.                         
    92.                         /*var key:String; 
    93.                         
    94.                         for (key in data) 
    95.                         { 
    96.                                 trace(key + ": " + data[key]); 
    97.                         } */
    98.                         
    99.                 }
    100.                 
    101.                 // -------------------------------- 提示点 -----------------------------------
    102.                 
    103.                 private function onCuePoint( cuePoint:Object ):void {
    104.                         
    105.                         trace(cuePoint.name + " " + cuePoint.time);
    106.                 }
    107.                 
    108.                 public function killVideo ( ):void {
    109.                         
    110.                         _video.clear( );
    111.                         
    112.                         //removeChild(_video);
    113.                         
    114.                 }
    115.                 
    116.                 private function statusHandler(event:NetStatusEvent):void 
    117.                         
    118.                 { 
    119.                         switch (event.info.code) 
    120.                         { 
    121.                                 case "NetStream.Play.Start": 
    122.                                         //dispatchEvent(new SenceChooseInfoEvent( "videoStart"));
    123.                                         break; 
    124.                                 
    125.                                 case "NetStream.Play.Stop": 
    126.                                         
    127.                                         killVideo ( );
    128.                                         //dispatchEvent(new SenceChooseInfoEvent( "videoOver"));
    129.                                         break; 
    130.                         } 
    131.                 }
    132.                 
    133.                 //--------------------------------- 添加控制滚动条 ----------------------------------
    134.                 
    135.                 
    136.                 private function videoScrubber ( ):void {
    137.                         
    138.                         // 滑块条
    139.                         _track= new Sprite( );
    140.                         _track.graphics.lineStyle(1,0xcccccc );
    141.                         _track.graphics.drawRect(0, -2.5,150, 5);
    142.                         _track.graphics.endFill();
    143.                         
    144.                         _track.y = _video.height + 20;
    145.                         
    146.                         this.addChild(_track);
    147.                         
    148.                         // 滑块
    149.                         _thumb = new Sprite( );
    150.                         
    151.                         _thumb.graphics.lineStyle( );
    152.                         _thumb.graphics.beginFill(0x000000);
    153.                         
    154.                         _thumb.graphics.drawRect(-5, -5, 10, 10);
    155.                         _thumb.graphics.endFill( );
    156.                         
    157.                         _thumb.y = _track.y ;
    158.                         
    159.                         this.addChild(_thumb);
    160.                         
    161.                         this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    162.                         
    163.                         _thumb.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    164.                         _thumb.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    165.                         
    166.                 }
    167.                 
    168.                 private function onMouseDown(event:MouseEvent):void {
    169.                         
    170.                         _scrubbing = true;
    171.                         
    172.                         var rectangle:Rectangle = new Rectangle(0, 0, _track.width, _thumb.y);
    173.                         
    174.                         _thumb.startDrag(false, rectangle);
    175.                 }
    176.                 private function onMouseUp(event:MouseEvent):void {
    177.                         
    178.                         _scrubbing = false;
    179.                         
    180.                         _thumb.stopDrag( );
    181.                         
    182.                 }
    183.                 
    184.                 private function onEnterFrame(event:Event):void {
    185.                         
    186.                         
    187.                         if(_duration > 0) {
    188.                                 
    189.                                 if(_scrubbing) {
    190.                                         
    191.                                         _stream.seek(_thumb.x *  Math.round(_duration) / 150);
    192.                                         
    193.                                 }
    194.                                 else {
    195.                                         
    196.                                         _thumb.x = _stream.time / Math.round(_duration) * _track.width;
    197.                                 }
    198.                         }
    199.                         
    200.                 }
    201.                 //--------------
    202.                 
    203.         }
    204. }
    复制代码

    需要一个引用这个PlayVideo 的类

    1. package tlg.sence
    2. {
    3.         import flash.display.DisplayObjectContainer;
    4.         import flash.display.Sprite;
    5.         
    6.         import tlg.tool.PlayVideo;
    7.         public class Sence extends Sprite
    8.         {
    9.                 public var playVideo:PlayVideo ;
    10.                 
    11.                 public function Sence( )
    12.                 {
    13.                         
    14.                 }
    15.                 
    16.                 public function addSenceContent ( str:String ):void {
    17.                         
    18.                         playVideo = new PlayVideo ( );                        
    19.                         
    20.                         playVideo._stream.play( str );                        
    21.                         
    22.                         this.addChild( playVideo );        
    23.                         
    24.                 }
    25.                 
    26.         }
    27. }
    复制代码

    那里不够好呢?首先我们看PlayVideo.as 

    public var _stream:NetStream; 方法是个公开方法,后果就是造成引用PlayVideo.as 时,需要额外调一下这个个属性:playVideo._stream.play( str );        

    public var vidoUrl:String; 方法是个公开方法;

    _video = new Video(160, 120); 参数已经写死;为以后其他类引用造成局限。同理layVideo 没有设有必要的位置信息属性,虽然我们可以,呵呵,你懂的

    同样的在以下代码中也将数据写死了,

    1. // 滑块条
    2.                         _track= new Sprite( );
    3.                         _track.graphics.lineStyle(1,0xcccccc );
    4.                         _track.graphics.drawRect(0, -2.5,150, 5);
    5.                         _track.graphics.endFill();
    6.                         
    7.                         _track.y = _video.height + 20;
    8.                         
    9.                         this.addChild(_track);
    10.                         
    11.                         // 滑块
    12.                         _thumb = new Sprite( );
    13.                         
    14.                         _thumb.graphics.lineStyle( );
    15.                         _thumb.graphics.beginFill(0x000000);
    16.                         
    17.                         _thumb.graphics.drawRect(-5, -5, 10, 10);
    18.                         _thumb.graphics.endFill( );
    复制代码

    好吧,给出解决以上不足的一个方法,就是抽象PlayVideo 类。怎么抽象?就是挑出所有引用某个值的变量。比如:

    1. // 
    2.                 public var _stream:NetStream;
    3.                 //
    4.                 private var _video:Video;
    5.                 //
    6.                 private var connection:NetConnection;
    7.                 
    8.                 public var _duration:uint;
    9.                 
    10.                 // 视频链接地址
    11.                 public var vidoUrl:String;
    12.                
    复制代码

    和写死的数据,如

    1. new Video(160, 120);
    复制代码

    下面要做的是就是将PlayVideo 抽象出来

    1. public function PlayVideo( nc:NetConnection,ns: NetStream,video:Video,flick:string,xpos:uint,ypos:uint,sizeW:uint,sizeY:uint )         {
    2. nc = new NetConnection ();
    3. nc.connect ( null );
    4. ns = new NetStream ( nc );
    5. vid = new video ( sizeW,sizeY );
    6. vid.attachNetStream ( ns );
    7. ns.play ( flick );
    8. vid .x = xPos;
    9. vid.y = yPos;
    10. addchild ( vid );
    11. }
    复制代码

    那究竟什么是抽象?为什么要抽象? 你可能通过其他途径获取了其要点。我举个榨汁机的例子:当你要榨汁时,只需把水果(参数)放进去,调整几个按钮(参数),并不关心它是怎么榨汁的。

    这也就是我们常说的黑盒子封装,封装很重要,因为在具体程序中强调,多依赖组合,而不是继承。

  • 相关阅读:
    zbb20180930 Postman 使用方法详解
    Cookie、Session、jsp、EL、JSTL
    Http协议、Tomcat、servlet
    xml、网络编程、 反射
    JDBC、DBUtils
    Java IO流对象、多线程
    mySql
    Java基础
    VueJs
    GIT
  • 原文地址:https://www.cnblogs.com/keng333/p/3170226.html
Copyright © 2020-2023  润新知