Archive for the 'Flash-AS3.0' Category

Page 2 of 4

Loading Zip files into Flash

As3.0 에서는 ByteArray Class 의 지원으로 bynary code 를 직접 플래시에서 읽고 쓸수가 있다.
플래시 외부로 data 를 export 할때나 불러들일때 압축된 bynary string 를 사용할 수 있어 기존에 할수 없었던 많은 작업들이 가능해졌다.

그중에 하나가 flash 안으로 zip 파일 형태를 로드할 수 있다는 것이다.
또한 그 스트리밍 데이타를 이용하여 zip 파일내에있는 이미지나 파일이 로드될 때 가져와서 쓸 수 있다.
단, 여기서 소개된 소스는 standard zip 파일 형식만 지원한다.

public function loadZip() {
   var request:URLRequest = new URLRequest("your.zip");
   var zip:FZip = new FZip();
   zip.addEventListener(FZipEvent.FILE_LOADED, fileCompleteHandler);
   zip.load(request);
}
private function fileCompleteHandler(evt:FZipEvent):void {
   var file:FZipFile = evt.file;
   trace("File loaded: " + file.filename)
   trace("  " + file.sizeCompressed);
   trace("  " + file.sizeUncompressed);
}

관련사이트
(http://codeazur.com.br/lab/fzip/)

Bound methods in AS3.0

<These quotes are from the Actionscript 3.0 language reference>

A bound method, sometimes called a method closure, is simply a method that is extracted from its instance. Examples of bound methods include methods that are passed as arguments to a function or returned as values from a function. New in ActionScript 3.0, a bound method is similar to a function closure in that it retains its lexical environment even when extracted from its instance. The key difference, however, between a bound method and a function closure is that the this reference for a bound method remains linked, or bound, to the instance that implements the method. In other words, the this reference in a bound method always points to the original object that implemented the method. For function closures, the this reference is generic, which means that it points to whatever object the function is associated with at the time it is invoked.
Understanding bound methods is important if you use the this keyword. Recall that the this keyword provides a reference to a method’s parent object. Most ActionScript programmers expect that the this keyword always refers to the object or class that contains the definition of a method. Without method binding, however, this would not always be true.
In previous versions of ActionScript, for example, the this reference did not always refer to the instance that implemented the method. When methods are extracted from an instance in ActionScript 2.0, not only is the this reference not bound to the original instance, but also the member variables and methods of the instance’s class are not available. This is not a problem in ActionScript 3.0 because bound methods are automatically created when you pass a method as a parameter. Bound methods ensure that the this keyword always references the object or class in which a method is defined.

The following code defines a class named ThisTest, which contains a method named foo() that defines the bound method, and a method named bar() that returns the bound method. Code external to the class creates an instance of the ThisTest class, calls the bar() method, and stores the return value in a variable named myFunc.

class ThisTest {
private var num:Number = 3;
function foo () { // bound method defined
trace ("foo's this: " + this);
trace ("num: " + num);
}
function bar () {
return foo; // bound method returned
}
}
var myTest:ThisTest = new ThisTest();
var myFunc:Function = myTest.bar();
trace(this); // output: [object global]
myFunc();
/* output:
foo's this: [object ThisTest]
output: num: 3 */

The last two lines of code show that the this reference in the bound method foo() still
points to an instance of ThisTest class, even though the this reference in the line just before
it points to the global object. Moreover, the bound method stored in the myFunc variable still
has access to the member variables of the ThisTest class. If this same code is run in
ActionScript 2.0, the this references would match and the num variable would be undefined.
<These quotes are from the Actionscript 3.0 language reference>

도큐먼트 문서에서 개인적으로 AS3.0 이 새롭게 지원하는 기능중에 가장 좋았던 것 중에 하나다.
더이상 중첩된 메서드 안에서 클래스 안에 선언된 메서드의 참조값을 알아내기 위해 지역변수로 참조를 하지 않아도 된다.
코드 가독성 측면에 있어서 좋지 않았었는데 이젠 끝까지 메서드 선언된 참조 위치를 가지고 있으니 파라미터나 리턴 값에 의해 참조값이 바뀔 염려는 없어진 셈이다.

Why must you use a function statement

Methods are defined using the function keyword.
You can use a function statement, such as the following:

public function sampleFunction() : String {}

Or you can use a variable to which you assign a function expression, as follows:

public var sampleFunction:Function = function () : String {}

In most cases you will want to use a function statement instead of a function expression for
the following reasons:

* Function statements are more concise and easier to read.

Function statements 는 좀더 간결하고 가독성이 뛰어나다.

* Function statements allow you to use the override and final keywords.

Function statements 는 override 와 final 키워드를 사용할 수 있게 한다.(override 방법은 expression 방법으로는 불가능합니다.)

* Function statements create a stronger bond between the identifier—that is, the name of the function—and the code within the method body. Because the value of a variable can be changed with an assignment statement, the connection between a variable and its function expression can be severed at any time. Although you can work around this issue by declaring the variable with const instead of var, such a technique is not considered a best practice because it makes the code hard to read and prevents the use of the override and final keywords.

Function statements 는 식별자(함수 이름과 메소드 몸체안에 있는 코드) 와의 결합을 더 강하게 한다.

변수값이 할당구문에 의해 변할 수 있기 때문에 function expression 과 변수와의 연결이 언제든지 위험하게 될수 있다.

비록 var 대신에 const 를 사용하여 변수선언을 하여 문제를 해결할 수 있겠지만, 그런 기술은 오버라이드(override)와 final 키워드의 사용을 막고, 코드를 읽기 어렵게 만들기 때문에 좋은 방법이라고 생각되어지지 않는다.

One case in which you must use a function expression is when you choose to attach a function to the prototype object.

전에는 function statement 방법과 function expression 과의 차이점에 큰 영향을 느끼지 못했다.

주로 가독성 측면에서 좋아서 전자의 방법으로 코딩을 해왔는데 이 방법이 추천하는 방식이였다니….

가독성 측면이나 메소드 오버라이드 같은 기능을 활용하려면  function statement  방법을 사용해야할 것이다.

Mouse Event in AS3.0

기존에 AS2.0 까지는 MovieClip 안에 버튼을 넣고 MovieClip 에 마우스 이벤트를 걸면 작동이 되질 않았다. 즉 버튼안에 버튼이 있는 상태는 마우스 이벤트가 발생하지 않아 네비게이션 같은 작업을 할때 정말 쉽지 않았다.

편법으로 hitTest 를 통해 이벤트를 체크했지만 리소스 측면에서 별로 바람직하지 못한 방법이다.

<AS2.0>

clip.onRollOver=function()

{

trace(“rollOver “+ this);

}

clip 이라는 무비클립안에 버튼이 들어가 있는 상태. 무비클립에 마우스 이벤트를 설정해도 작동하지 않는다.

<AS3.0>

function onRollOver(objEvent:Event)

{

trace(“RollOver “, objEvent.target, objEvent.target.name);

}

clip.addEventListener(MouseEvent.MOUSE_OVER, this.onRollOver);

버튼으로 작동하는 clip 무비클립 안에 버튼이 있어도 이벤트를 발생시킨다.

download sample

MD5 and SHA1 in AS3

javascript 버전으로 나온 MD5 와 SHA1 을 컨버팅…
점점더 flash가 application tool 로써 부각되는 만큼 보안에 신경써서 암호화시키는 방법을 좀더 고려해야겠다.

import com.dstrict.UB.util.system.security.MD5;
import com.dstrict.UB.util.system.security.SHA1;

trace(MD5.encrypt(“kimkijeung”)); //8447d508fb3dc5987853d6b2eb8a752f
trace(SHA1.encrypt(“kimkijeung”)); //c6fe97a875b287751d6d8c33256d4277fe1e2ee2

>Paj’s Home: Cryptography: JavaScript MD5