![]() ![]() | |
Flash Player 5。
this
标识符;引用对象或影片剪辑实例。在脚本执行时,this 引用包含该脚本的影片剪辑实例。在调用方法时,this 包含对包括所调用方法的对象的引用。
在附加到按钮的 on 事件处理函数动作中,this 引用包含该按钮的时间轴。在附加到影片剪辑的 onClipEvent() 事件处理函数动作中,this 引用该影片剪辑自身的时间轴。
因为 this 是在包含它的脚本的上下文中计算的,所以脚本中不能使用 this 引用类文件中定义的变量:
// 在文件 applyThis.as 中
class applyThis{
var str:String = "Defined in applyThis.as";
function conctStr(x:String):String{
return x+x;
}
function addStr():String{
return str;
}
}
// 在 FLA 中使用以下代码测试影片
import applyThis;
var obj:applyThis = new applyThis();
var abj:applyThis = new applyThis();
abj.str = "defined in FLA";
trace(obj.addStr.call(abj,null)); // 在 FLA 中定义
trace(obj.addStr.call(this,null)); // 未定义
trace(obj.addStr.call(obj,null)); // 在 applyThis.as 中定义
同样,若要调用动态类中定义的函数,您必须使用 this 设置该函数的范围:
// simple.as 不正确的版本
dynamic class simple{
function callfunc(){
trace(func());
}
}
// simple.as 正确的版本
dynamic class simple{
function callfunc(){
trace(this.func());
}
}
// FLA 文件中的语句
import simple;
var obj:simple = new simple();
obj.num = 0;
obj.func = function():Boolean{
return true;
}
obj.callfunc(); // simple.as 不正确版本中的语法错误
在下面的示例中,关键字 this 引用 Circle 对象。
function Circle(radius) {this.radius = radius;this.area = Math.PI * radius * radius;}
在下面分配给帧的语句中,关键字 this 引用当前的影片剪辑。
// 将当前影片剪辑的 alpha 属性设置为 20this._alpha = 20;
在下面的 onClipEvent() 处理函数内的语句中,关键字 this 引用当前的影片剪辑。
// 加载该影片剪辑时,对当前影片剪辑// 启动一个 startDrag() 操作。onClipEvent (load) {startDrag (this, true);}
![]() ![]() | |