<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 이 새롭게 지원하는 기능중에 가장 좋았던 것 중에 하나다.
더이상 중첩된 메서드 안에서 클래스 안에 선언된 메서드의 참조값을 알아내기 위해 지역변수로 참조를 하지 않아도 된다.
코드 가독성 측면에 있어서 좋지 않았었는데 이젠 끝까지 메서드 선언된 참조 위치를 가지고 있으니 파라미터나 리턴 값에 의해 참조값이 바뀔 염려는 없어진 셈이다.
ActionScript 3 introcuduces labels, new identifiers that can be associated with loop blocks. Why would you want to identify a loop block? Because you can use that identifier as a target for break and continue commands. Consider two loops where one is nested in the other. If at some point you want to exit both loops while in the nested loop, you can’t. The break command only exits the current block. A common workaround is to use a flag variable to be able to check that, when in the first loop, if that should be exited as well
중첩된 loop의 경우 기존에는 한번에 loop를 벗어나는것은 할수 없었다.
다만 flag를 이용해 벗어나는 경우가 유일한 방법이다. 이젠 그럴필요 없다.
As3.0 에서는 Label Statements 를 이용해 전체 루프를 빠져나올수가 있다.
As 2.0 ActionScript Code:
var i:Number;
var j:Number;
var exit:Boolean = false;
for (i=0; i<10; i++) {
for (j=0; j<10; j++) {
if (i >3 && j> 3) {
exit = true;
break;
}
}
if (exit) {
break;
}
}
—–> exit 라는 flag 변수를 이용해 체크한다.
As 3.0 ActionScript Code:
var i:Number;
var j:Number;
mainLoop: for (i=0; i<10; i++) {
for (j=0; j<;10; j++) {
if (i > 3 && j > 3) {
break mainLoop;
}
}
}
——> mainLoop 의 label 을 지정해 한번에 loop 탈출…..지저분하게 변수를 사용하지 않는 명확한 방법이다.
Using ActionScript 3, you can dynamically change the frame rate of your movie using the Stage class.
The Stage class (flash.display.Stage) is the class assigned to the stage object which is accessible from your main movie sprite/movie clip (or others within the same security sandbox) using the stage property. The stage object has a frameRate property which can contain any value between 0.01 and 1000 and determines the frame rate at which the Flash player plays back your movie. Changing this value lets you change the frame rate at runtime.
// change frame rate to 12 fps:
stage.frameRate = 12;
이전 버전에서는 한번 정한 framerate 는 바뀔수가 없었다. 즉 동적으로 frame 속도를 조절할수 없었음.
근데 과연 frameRate 가 1000까지 지원할까?…. 지원하더라도 눈으로 확인은 불가능 할듯.
다만 각각의 swf 파일의 속도는 조절안되고 전체 stage 의 속도만 조절되는것 같다.
One thing about previous versions of ActionScript was that you could never tell when the user no longer had his or her mouse over the Flash movie. This made it hard for people to know whether or not the user is still interacting with their movie or if they’ve given up and moved on to something more interesting. This was especially a problem for custom cursors where, if the user moved the cursor off the Flash movie, the custom cursor would still remain in the Flash movie not moving while the real cursor could be seen moving around every where else. ActionScript 3 now allows you to detect when the mouse has left the flash movie using the stage’s mouseLeave event. This event happens whenever the mouse exits the Flash movie. There is no mouseEnter event, but you can use mouseMove for that since mouseMove only occurs in Flash (for the stage, or really any, object) when the mouse is within the bounds of the movie.
html에서의 플래시 영역 밖으로 마우스 커서를 옮겼을경우 이전에는 정확히 그것을 알아낼 방법이 없었다. 마우스 커서가 안보인다 하더라도 엄연히 플래시 오브젝트 안에 남아서 실행되고 있었는데
이것을 as3.0 에서는 체크할수 있게 되었다.
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class Test extends Sprite {
private var cursor:Sprite = new Sprite();
public function Test() {
cursor.graphics.beginFill(0xFF);
cursor.graphics.drawRect(0, 0, 25, 25);
addChild(cursor);
stage.addEventListener(Event.MOUSE_LEAVE, cursorHide);
stage.addEventListener(MouseEvent.MOUSE_MOVE, cursorFollow);
Mouse.hide();
}
public function cursorHide(evt:Event):void {
cursor.visible = false;
}
public function cursorFollow(evt:MouseEvent):void {
if (!cursor.visible) cursor.visible = true;
cursor.x = stage.mouseX;
cursor.y = stage.mouseY;
evt.updateAfterEvent();
}
}
}
Recent Comments