Interval 에 해당하는 글1 개
2007/01/28   User setInterval Class (1)

flash/As2.0 | 2007/01/28 16:30

<Interval Class>

import
com.dstrict.UB.events.Dispatcher;
import com.dstrict.UB.management.IntervalEvent;
/**--------------------------------------------------------------------------------
* @description 인터벌 생성및 실행 클래스
*    원하는 함수를 사용자가 원하는 횟수만큼 호출한후 onIntervalEnd 를 발행시킴...
*
* @implement
*   Interval#onIntervalEnd
*  
* @example
* <code>
*  _interval=new Interval();
 _interval.addEventListener(IntervalEvent.ON_INTERVAL_END,this);
 _interval.makeInterval(functionScope,functionReference,duration,loop,[parameter array]);
   
 public function onIntervalEnd(evt:IntervalEvent):Void{
    // 인터벌 이후에 이벤트를 사용하려면 구현...
     parameter array 인자가 그대로 넘어온다...
    
  }
* </code>
*  
*--------------------------------------------------------------------------------*/

class com.dstrict.UB.management.Interval extends Dispatcher{

 private var _IntervalID : Number;
 private var _count : Number;
 
  /**------------------------------------------------------------------------------
     * Constructor function
   *------------------------------------------------------------------------------*/

 public function Interval() {
  //생성자 함수 필요없음....
 }
 
 /**------------------------------------------------------------------------------
 * @description 전역속성인 setInterval 함수의 id 값 전부 제거
 *-------------------------------------------------------------------------------*/

 public static function clearAllInterval():Void{
  var topId=setInterval(function(){
  },100);
  for(var i=0 ; i<=topId; i++){
   clearInterval(i);
  }
 }
 
 
  /**------------------------------------------------------------------------------
    * @description interval 에의한 실행함수
    * @param loop : Number, 함수 실행할 횟수
   
*@param referObj : Object, 인터벌에 등록한 함수 오브젝트
    *-------------------------------------------------------------------------------*/
  public function execute(loop:Number,referObj:Object):Void{
     if(_count>loop){
        clearInterval(_IntervalID);
       _count=1;
     startEvent(new IntervalEvent(IntervalEvent.ON_INTERVAL_END,referObj.param)); //Dispatch  event
     }else{
        if(referObj!=undefined){
           referObj.func.apply(referObj.obj,referObj.param);
         }
         _count++;
      }
    }
 
 /**-----------------------------------------------------------------------------
 * @description  인터벌 생성
 * @param scope : Object, 실행할 함수의 스코프
 * @param func : Function, 실행할 함수
 * @param duration : Number, 인터벌 간격(밀리세컨드)
 * @param loop : Number, 함수 실행할 횟수
 *-------------------------------------------------------------------------------*/
 public function makeInterval(scope:Object,func:Function,duration:Number,
                                            loop:Number,parameter:Array):Void{
 
   clearInterval(_IntervalID);
   _count=1;
  _IntervalID=setInterval(this,"execute",duration,loop,{func:func,obj:scope,param:parameter});
 
 }
   
}



<DispatcherClass>

import mx.events.EventDispatcher;
/**
* @description     이벤트 디스패처 확장 클래스
*                        이벤트 발생할 클래스에 상속후 startEvent 함수 실행.....
*/
class com.dstrict.UB.events.Dispatcher {
   public var dispatchEvent:Function;
   public var addEventListener:Function;
   public var removeEventListener:Function;
 
   public function Dispatcher() {
      EventDispatcher.initialize(this);
   }
 
   public function startEvent(obj : Object) : Void {
     dispatchEvent(obj);
    }
 }

자주사용하는 인터벌을 쓰기 편하게 클래스로 만들었다.  원하는 함수를 원하는 횟수만큼 호출한후 자동으로 인터벌이 삭제된다. 함수 호출시 파라미터도 같이 호출할 수도 있고 인터벌이후에 발생하는 이벤트도 실행할 수도 있게 구성했다.

<example code>
  test 함수를 문자열 2개 인자와 함께 200 millisecond 간격으로 5번 호출한다.

 _interval=new Interval();
 _interval.addEventListener(IntervalEvent.ON_INTERVAL_END,this);
--> 인터벌이후에 발생하는이 벤트를 받기위해서 리스너를 붙인다. 받을 이벤트가 없다면 필요없다.
 _interval.makeInterval(this,test,200,5,["test string1......","test string2......"]);

function test(str:String,str2:String):Void{
  trace(str);
  trace(str2);
}

 function onIntervalEnd(evt:IntervalEvent):Void{
    // 인터벌 이후에 이벤트를 사용하려면 구현...
    // evt 값은 makeInterval 에서 test 함수를 호출할때 같이 쓰인 문자열 인자값이 배열로 그대로 들어온다. 
   // evt.target[0] -----> test string1...... evt.target[1] -----> test string2......
 
 }



 
 
태그 : , , ,
이 글의 관련글(트랙백) 주소 :: http://kimkijeung.com/trackback/82
김대우 2007/02/06 19:45 ReplyDelete
매우 알찬 내용이군요
많은 도움이 되었습니다

Name 
Password 
Homepage 
  secret
Comment 
  글쓰기


[PREV] [1] [NEXT]

 
전체 (105)
flash (74)
math&physics (4)
programming (11)
Flex2 (1)
Mac (2)
photo (0)
project (6)
주저리주저리 (3)
유용한 자료들 (1)
diary (0)
Book (1)
web (2)
«   2009/01   »
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31