Actionscript 2.0 事件的范围

发布于 2024-12-29 22:14:41 字数 1424 浏览 3 评论 0原文

我在手机上使用 Actionscript 2.0,无法理解事件。

我正在使用我的所有代码创建一个类对象,并使用一组函数(全部作为该类的直接第一级子级)。有一个函数可以创建一个带有正方形的影片剪辑,并将 onPress 事件设置为另一个名为 hit 的函数:

public function draw1Sqr(sName:String,pTL:Object,sSide:Number,rgb:Number){
    // create a movie clip for the Sqr
        var Sqr:MovieClip=this.canvas_mc.createEmptyMovieClip(sName,this.canvas_mc.getNextHighestDepth());
    // draw square
        Sqr.beginFill(rgb); 
        //etc  ...more lines        

    //setup properties (these are accessible in the event)
        Sqr.sSide=sSide;
        Sqr.sName=sName; 

    //setup event
        Sqr.onPress = hit; // this syntax seems to lead to 'this' within
                            // the handler function to be Sqr (movieclip)

        //Sqr.onPress = Delegate.create(this, hit); 
        //I've read a lot about Delegate but it seems to make things harder for me.
    }



然后在我的事件处理程序中,我无法获得正确的范围...

public function hit(){
    for (var x in this){
        trace(x + " == " + this[x]);
    }
            //output results
                //onPress == [type Function]
                //sName == bSqr_7_4
                //sSide == 20

    trace(eval(this["._parent"])); //undefined
    trace(eval(this["._x"])); //undefined

}

出于某种原因,虽然范围设置为调用对象(Sqr,Movieclip)并且我可以访问我定义的属性,但我不能使用 Movieclip 对象的“本机”属性。

有关如何访问按下的 Movieclip 对象的 _x、_y 和其他属性的任何建议。

I'm using Actionscript 2.0 for a mobile phone and can't get my head around Events.

I'm creating a class object with all my code and using a group of functions (all as direct 1st level children of the class). There's one function that creates a Movieclip with a square on it and sets the onPress event to another function called hit:

public function draw1Sqr(sName:String,pTL:Object,sSide:Number,rgb:Number){
    // create a movie clip for the Sqr
        var Sqr:MovieClip=this.canvas_mc.createEmptyMovieClip(sName,this.canvas_mc.getNextHighestDepth());
    // draw square
        Sqr.beginFill(rgb); 
        //etc  ...more lines        

    //setup properties (these are accessible in the event)
        Sqr.sSide=sSide;
        Sqr.sName=sName; 

    //setup event
        Sqr.onPress = hit; // this syntax seems to lead to 'this' within
                            // the handler function to be Sqr (movieclip)

        //Sqr.onPress = Delegate.create(this, hit); 
        //I've read a lot about Delegate but it seems to make things harder for me.
    }

Then in my event handler, I just cannot get the scope right...

public function hit(){
    for (var x in this){
        trace(x + " == " + this[x]);
    }
            //output results
                //onPress == [type Function]
                //sName == bSqr_7_4
                //sSide == 20

    trace(eval(this["._parent"])); //undefined
    trace(eval(this["._x"])); //undefined

}

For some reason, although the scope is set to the calling object (Sqr, a Movieclip) and I can access properties I defined, I can't use the 'native' properties of a Movieclip object.

Any suggestions on how I can access the _x, _y and other properties of the Movieclip object that is pressed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2025-01-05 22:14:41

使用数组访问器或点访问器,但不能同时使用两者。例如:

trace(this._parent); // OR
trace(this["_parent"]);

至于你的迭代结果,我记得 AS2 在这方面很糟糕。当使用 for ... in 循环时,IIRC 仅返回动态属性。当您想要的只是您自己设置的键/值对时,这可以防止对象(通常用作哈希映射)包含其本机属性。

另外 - eval() 函数很容易被过度使用。除非你绝对必须执行编译时没有的 AS2 字符串,否则我建议避免它。快乐编码!

Use the array accessor or the dot accessor, but not both. For example:

trace(this._parent); // OR
trace(this["_parent"]);

As for the results of your iteration, I recall AS2 being screwy on this front. IIRC only dynamic properties are returned when looping with for ... in. This prevents Objects (which often serve as hash maps) from including their native properties when all you want are the key/value pairs you set yourself.

Also - the eval() function can be easily overused. Unless you absolutely must execute a String of AS2 that you don't have at compile-time I would recommend avoiding it. Happy coding!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文