How to break through the scope limit in Class [Flash8 Guys]

xiaoxiao2021-03-06  41

Poster:

Super-Tomato

Friends I want to learn Class will encounter some scope restrictions, such as using ONKEYDOWNs in Class, which cannot call other functions, almost many web pages are just a simple explanatory method without mentioning this. I will teach you a small skill to break through this range.

Class testing {

Function Testing () {

TRACE (this); // This this is the testing itself

Var mykey: Object = new object ();

mykey.onkeydown = function () {

TRACE (this); // This is this refers to myKey

Echo ("Breakth Range Limit"); // Of course you can also use _root.test.echo (), but this code is unable to apply in other places.

}

Key.Addlistener (mykey);

}

Private function echo (str: string) {

Trace (STR);

}

}

-------------------------------------------------

VAR test: Testing = new testing ();

The solution is simple, as follows:

Class testing {

Function Testing () {

Var root: Object = THIS;

Var mykey: Object = new object ();

mykey.onkeydown = function () {

Root.echo ("Breakth Range Limit");

}

Key.Addlistener (mykey);

}

Private function echo (str: string) {

Trace (STR);

}

}

Will it come out after the test? ^ _ ^ Holybozo ========================================= ==================================== 茄 is solved by using Flash scope to solve, pretty good I have always used it, or define a Parent to OBJ.

Var mykey: object = {parent: this}; mykey.onkeydown = function () {this.parent.echo ("breakthrough range limit");}; key.addlistener (mykey);

But the reason is the same, the usage is also a good ^^ "

The simplest method var mykey: Object = new object (this); leefj =================================== ================================== But I have a better solution here, everyone can refer to it, about DELEGATE class, you can look at me in the 9CBS blog: http://blog.9cbs.net/9116/ rss: http://blog.9cbs.net/9116/rss.aspx, very supported the Word of Tomato King, When I wrote FDO, it was import mx.utils.delegate of the way; class testing {var name = "Tomato king" function testing () {// var root: Object = this; var mykey: object = new Object (); mykey.onkeydown = delegate.create (this, onkeydown); key.addlistener (mykey);} private function onkeydown ()} (key.getcode (); trace (name);}} Holybozo == ============ ======================================================================================================================================================= ========== Method is definitely a lot

Good way, I think it is almost?? There is another one, use listening, don't know how, huh, huh

Var mykey: Object = new object (); asbroadcaster.initialize (mykey); mykey.addlistener (this); mykey.onkeydown = function () {this.broadcastmessage ("echo", "breakthrough range limit");};

转载请注明原文地址:https://www.9cbs.com/read-75989.html

New Post(0)