通过 Flash 中的 Actionscript 3 访问符号实例

发布于 2024-12-14 01:41:40 字数 1199 浏览 1 评论 0原文

背景信息:

  • 我正在使用 Adob​​e Flash CS4
  • 我正在使用 Actionscript 3
  • 我正在将我的代码发布到 Adob​​e AIR 文档中,但是我不确定这是否与此特定问题相关。< /p>

  • 我在库中创建了两个符号,即大矩形,并将它们的实例放置在舞台上。

  • 我使用“getObjectsUnderPoint”来检测单击鼠标的位置下的对象。
  • 我已经验证 getObjectsUnderPoint 正在通过跟踪名称来检测符号实例。

问题

  • getObjectsUnderPoint 检测到的对象的跟踪名称都是instance1、instance2、instance3 等,即使我已经命名了这些实例。
  • 当试图确保鼠标单击由任一矩形包围的区域时,这会导致问题,因为我无法检查 object[i].name == "leftbox" 以及 instance1、instance2 等是否存在当我从舞台上添加/删除内容时,名称会发生​​变化,因此我不想检查实例2和实例3(它们是我现在创建的两个框,分别命名为“leftbox”和“rightbox”) 。

问题:

我在检测名称时做错了什么吗?我如何通过类中的 ActionScript 实际检测我在舞台上创建的实例名称

这是我的代码:

        var pt:Point = new Point(e.stageX, e.stageY);
        var objects:Array = getObjectsUnderPoint(pt);
        var action = 0;

        for(var i=0; i< objects.length; i++) {
            trace(objects[i].name);
        }

        if( objects.indexOf('left_box') >= 0 ){
            action = 1;

        }
        if(objects.indexOf('right_box') >= 0 ){
            action = 2;

        }

Background Information:

  • I am using Adobe Flash CS4
  • I am using Actionscript 3
  • I am publishing my code into an Adobe AIR document, however I am not sure that this is relevant to this particular problem.

  • I've created two symbols in the library, large rectangles, and placed instances of them on the stage.

  • I'm using "getObjectsUnderPoint" to detect the objects under where the mouse is clicked.
  • I have verified that getObjectsUnderPoint is detecting the symbol instances by tracing the names.

The Problem

  • The traced names of the objects detected by getObjectsUnderPoint are all instance1, instance2, instance3, etc. even though I have named the instances.
  • This causes an issue when trying to ensure that the mouse was clicked on a zone encompassed by either one of the rectangles, as I can't check if object[i].name == "leftbox" , and the instance1, instance2, etc. names change as I add/remove things from the stage, so I do not want to check for instance2 and instance3 (which are as of right now the 2 boxes I've created, which are named "leftbox" and "rightbox" respectively.

The Question:

Am I doing something wrong in detecting the names? How do I actually detect the instance names that I have created on the stage through the ActionScript in my class?

Here is my code:

        var pt:Point = new Point(e.stageX, e.stageY);
        var objects:Array = getObjectsUnderPoint(pt);
        var action = 0;

        for(var i=0; i< objects.length; i++) {
            trace(objects[i].name);
        }

        if( objects.indexOf('left_box') >= 0 ){
            action = 1;

        }
        if(objects.indexOf('right_box') >= 0 ){
            action = 2;

        }

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

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

发布评论

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

评论(3

流云如水 2024-12-21 01:41:40

继续 Fahim Akhter 的回答,

然后您可以循环获取父级,直到该对象是一个 movieclip

var o:DisplayObject=objects[i];
while(!(o.parent is MovieClip)) {
    o=o.parent;
}
var myMovieClip:MovieClip=o.parent;

这应该在 myMovieClip 中为您提供一个影片剪辑,当您跟踪 myMovieClip.name 时,您将得到您想要的正在寻找。

这也适用于多级符号(其中 1 个符号包含另一个符号)

Taking forward Fahim Akhter's answer,

You can then loop to get the parent until the object is a movieclip

var o:DisplayObject=objects[i];
while(!(o.parent is MovieClip)) {
    o=o.parent;
}
var myMovieClip:MovieClip=o.parent;

This should give you a movie clip in myMovieClip and when you trace myMovieClip.name, you'll get what you are looking for.

This will also work for multiple level symbols (where 1 symbol contains another)

流星番茄 2024-12-21 01:41:40

让我想起一句话“我没有解决你的问题,但我当然很欣赏这个问题”

如果你尝试跟踪(objects[i]),你会看到函数 getObjectsUnderPoint 返回一个形状而不是 MovieClip,所以你永远不会得到你正在寻找的名字。

Reminds me of a quotation "I do not have the solution to your problem, but I certainly admire the problem"

If you try to trace(objects[i]) you'll see the function getObjectsUnderPoint is returning a shape not the MovieClip, so you never get the name you are looking for.

故人爱我别走 2024-12-21 01:41:40

Fahim Akhter 在评论中指出,我正在描绘形状,而不是物体。这使我能够进行比迄今为止更准确的谷歌搜索。

搜索将我带到了这个论坛主题: http://www.actionscript.org/ forums/showthread.php3?t=231181

并在 这篇特定帖子(第6号),作者指出

“事实证明,getObjectsUnderPoint返回的是一个数组
最简单的对象 - 最子对象,如果这有意义的话。我能够
只需在我的末尾添加 .parent 即可解决部分问题
变量...”

我将 .parent 附加到该对象,并收到了适当的名称:

        var pt:Point = new Point(e.stageX, e.stageY);
        var objects:Array = stage.getObjectsUnderPoint(pt);
        var action = 0;

        for(var i=0; i< objects.length; i++) {
            trace(objects[i].parent.name);
        }

        if( objects.indexOf('left_box') >= 0 ){
            action = 1;

        }
        if(objects.indexOf('right_box') >= 0 ){
            action = 2;

        }

这解决了我的问题,并希望能让我们大家更好地理解 getObjectsUnderPoint。

Fahim Akhter pointed out in a comment that I was tracing shapes, not objects. This led me to a more accurate google search than I had been able to craft thus far.

The search led me to this forum thread: http://www.actionscript.org/forums/showthread.php3?t=231181

And in this particular post (number 6), the author pointed out that

"It turns out that getObjectsUnderPoint returns an array of the
simplest object- the child-est, if that makes any sense. I was able to
solve part of the problem by simply adding a .parent to the end of my
variable..."

I appended .parent to the object, and received the appropriate name:

        var pt:Point = new Point(e.stageX, e.stageY);
        var objects:Array = stage.getObjectsUnderPoint(pt);
        var action = 0;

        for(var i=0; i< objects.length; i++) {
            trace(objects[i].parent.name);
        }

        if( objects.indexOf('left_box') >= 0 ){
            action = 1;

        }
        if(objects.indexOf('right_box') >= 0 ){
            action = 2;

        }

This solves my problem, and hopefully leads us all to a better understanding of getObjectsUnderPoint.

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