class testing {
function testing() {
trace(this); //在这里的this是指testing本身
var myKey:Object = new Object();
myKey.onKeyDown = function() {
trace(this); //这里的this是指myKey
echo ("突破范围限制"); //当然你也可以使用_root.test.echo(), 但是这样的代码就无法灵活的应用在其他地方了
}
Key.addListener(myKey);
}
private function echo(str:String) {
trace(str);
}
}
---------------------- FLA ---------------------------
var test:testing = new testing();
解决方法很简单, 如下:
class testing {
function testing() {
var root:Object = this;
var myKey:Object = new Object();
myKey.onKeyDown = function() {
root.echo ("突破范围限制");
}
Key.addListener(myKey);
}
private function echo(str:String) {
trace(str);
}
}
测试之后是否出来了呢 ^_^
holybozo===========================================================================
茄茄是利用了flash的scope来解决的,蛮好的,偶一直这么用,或者给obj定义一个parent
var myKey:Object = {parent:this};
myKey.onKeyDown = function ()
{
this.parent.echo ("突破范围限制");
};
Key.addListener (myKey);
不过道理是一样,用法还是茄茄的好^^"
最简单的方法 var myKey:Object = new Object(this);
LeeFJ===================================================================
但是我这里有一个更好的解决方法,大家可以参考一下,关于Delegate类,大家可以看看我在CSDN的BLOG: http://blog.csdn.net/9116/ RSS :http://blog.csdn.net/9116/Rss.aspx ,非常支持番茄大王的写法,我在写FDO的时候就是用他那种方法的
import mx.utils.Delegate;
class testing
{
var NAME="番茄大王"
function testing ()
{
//var root:Object = this;
var myKey:Object = new Object ();
myKey.onKeyDown=Delegate.create (this, onKeyDown);
Key.addListener (myKey);
}
private function onKeyDown ()
{
trace (Key.getCode());
trace (NAME);
}
}
holybozo ============== ================================================
方法肯定还有很多
好的方法,我觉得差不多了??这里还有一个,是用监听的,不知道怎么样,呵呵
var myKey:Object = new Object ();
AsBroadcaster.initialize (myKey);
myKey.addListener (this);
myKey.onKeyDown = function ()
{
this.broadcastMessage ("echo", "突破范围限制");
};
本文地址:http://com.8s8s.com/it/it24007.htm