Monthly Archive for June, 2006

Page 2 of 3

Singleton pattern 에서 유의사항

Singleton pattern 으로 객체를 생성했을경우 플래시에서 뜻하지 않은 상황이 발생한다.

플래시에서 Export 한 후 Download simulate 를 하면 알수 없는 이유로 프로그램이 진행이 되질 않는다. 이것은 프로그램의 오류가 아니라 Singleton pattern 에서 인스턴스 참조값을 저장하기 위해 사용한 전역변수가 문제다.

다시말해, 플래시에서 simulate 를 하기 위해서는 Export 한 상태에서 한번 더 Ctrl+ Enter 을 눌러야된다. 이 상황에서 플래시는 메모리에서  전역변수를 소거하지 않고 그대로 남아있는 상태가 된다.

Singleton pattern 에서는 반드시 인스턴스는 단 한번 밖에 생성되지 않는다.

따라서 한번 Export 해서 생성된 인스턴스로 인해 simulate 할경우 원하지 않은 상황이 발생한다.

private static var sInstance:FramePulse = null;
public static function getInstance () : FramePulse {
    if (sInstance == null) {
      sInstance = new FramePulse();
    }
    return sInstance;
}

–> Download 테스트 시 메모리가 소거 되지 않아 전역속성인 sInstance 로 인해 인스턴스를 생성할 수 없다.

Event Programming

플래시 자체가 이벤트 기반의 프로그램이다 보니 발생하는 이벤트 중심으로 코딩하는 방법이 로직상이나 가독성 측면에서 좋은 코딩 방법이라는 생각이 든다.

하지만 이벤트 별로 패턴을 나누다 보니 코드길이가 자연스레 길어지고 이벤트 리스너(addEventListener) 를 연결하는 시간과 인스턴스를 초기화하는 시간과의 차이로 인해 적지않은 문제점이 발생한다.

var myLoader:XmlLoader=new XmlLoader(this,UrlSet.xmlUrl+"navigation.xml",100,100);
 
myLoader.setXmlAttribute(["label","src","loadingPos"]);
myLoader.addEventListener(XmlLoaderEvent.ON_START,this);
 
function onStart(evt:XmlLoaderEvent){
      myLoader.removeEventListener(XmlLoaderEvent.ON_START); 
   //파싱완료후 실행함수
 
}

간혹가다 인스턴스(myLoader ) 초기화보다 인스턴스 메소드가 먼저 실행되는 이상한 일이 발생할 경우가 있다.

Runtime Exceptions And Types

In ActionScript 2.0, type annotations were primarily a developer aid; at runtime, all values were dynamically typed. In ActionScript 3.0, type information is preserved at runtime, and utilized for a number of purposes. The Flash Player performs runtime type checking, improving the system’s type safety. Type information is also used to represent variables in native machine representations, improving performance and reducing memory usage.

단순히 개발자의 코드 가독성을 위한 도구였던 데이타 타입 설정이 As3.0 에서는 진정한 데이타 타입으로서 기능하게 되었다. 컴파일 타임에서의 데이타 타입의 체크에서 끝났던 것이 이제는 런타임에서도 타입체크가 가능하다.

이로써 실시간 예외처리도 가능해졌다. 플래시에서 가능 취약했던 부분인 런타임 디버깅이 비약적으로 발전했다. 이제보니 플래시가 스크립트 언어라고 하기엔 너무 커진것 같다는 생각이 든다..^^

Delegation in AS3.0

Event handling is simplified in ActionScript 3.0 thanks to its built-in delegation. In ActionScript 2.0, method closures would not remember what object instance they were extracted from, leading to unexpected behavior when the method closure was invoked. The mx.utils.Delegate class was a popular workaround; to use it, you would write code as follows:

myButton.addEventListener(“click”, Delegate.create(this, someMethod));

This class is no longer needed, since a method closure will now automatically remember its original object instance. Now, one can simply write:

myButton.addEventListener(“click”, someMethod);

꼭 바뀌었으면 하는 것이 AS3.0 에서 개선되었다. AS2.0 에서는 중첩된 메서드에서 부모 오브젝트에 접근하지 못했다. 그래서 지역변수를 이용하거나 mx.utils.Delegate 클래스를 사용해 오브젝트 스코프를 지정했는데 이젠…이런게 필요없다.

자동으로 오브젝트를 기억한다고 한다…앞으론 좀더 코드가 간결해질듯 싶다.^^

Bezier Curve Example

조절점이 3개일 경우 일반 베지에 곡선의 일반식

B(u) = P0 * ( 1 – u ) 2 + P1 * 2 * u ( 1 – u ) + P2 u2

을 이용하면 단순히 베지에 곡선을 그리는 것으로 끝나는 것이 아니라 각 포인트를 추적할 수 있다.

플래시에서 제공하는 curveTo 메서드도 물론 위 공식에 의한 계산으로 곡선을 그리는 것이지만 그리는것 이상의 것은 할 수 없었다.

function bezierPoint(mc:MovieClip,t:Number){
  var x = ( 1 - t ) * ( 1 - t ) * mc.p1._x + 2 * t * ( 1 - t ) * mc.p2._x + t * t * mc.p3._x;
  var y = ( 1 - t ) * ( 1 - t ) * mc.p1._y + 2 * t * ( 1 - t ) * mc.p2._y + t * t * mc.p3._y;
  mc.point_mc._x=x;
  mc.point_mc._y=y;
}

control point 와 anchor point  를 지정해 곡선을 그리고 더 나아가 그린 곡선위의 포인트를 계산에 의해 추적이 가능하다. 즉, 스크립트로 모션이 가능하다.