如何从文档类 AS3 中定位舞台上的影片剪辑?

发布于 2024-10-08 16:04:09 字数 1254 浏览 3 评论 0原文

我正在制作动态拖放游戏。 我有一个包含拖放代码的拖动项目的类。

我的问题是我无法调用/访问我已经在命中测试语句中放在舞台上的影片剪辑。

这是我的代码和 target1_mc & target2_mc 是舞台上现有的影片剪辑:

package {
 import flash.display.MovieClip;
 import flash.events.MouseEvent;
 import flash.text.TextField;




 public class recipeMC extends MovieClip {



  private var startX:Number;
  private var startY:Number;
  private var counter:Number=0;




  public function recipeMC() {
   this.mouseChildren = false;
   this.buttonMode = true;
   this.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
   this.addEventListener(MouseEvent.MOUSE_UP, dropIt);
  }
  private function pickUp(event:MouseEvent):void {
   this.startDrag(true);
   this.parent.addChild(this);
   startX = this.x;
   startY = this.y;

  }
  private function dropIt(event:MouseEvent):void {
   this.stopDrag();



   *****if (this.hitTestObject(target1_mc)
   ||this.hitTestObject(target2_mc) )***** {
    this.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    this.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
    this.buttonMode = false;
    this.x = myTarget.x;
    this.y = myTarget.y;
    counter++;
   } else {
    //reply_txt.text = "Try Again!";
    this.x = startX;
    this.y = startY;
   }

  }
 }
}

I am making dynamic drag and drop game.
I have a class for the dragged items containing the drag drop code.

My problem is I can't call/access the movie clips I've already put on the stage in my hit test statement.

Here is my code and target1_mc & target2_mc are the existing movie clips on the stage:

package {
 import flash.display.MovieClip;
 import flash.events.MouseEvent;
 import flash.text.TextField;




 public class recipeMC extends MovieClip {



  private var startX:Number;
  private var startY:Number;
  private var counter:Number=0;




  public function recipeMC() {
   this.mouseChildren = false;
   this.buttonMode = true;
   this.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
   this.addEventListener(MouseEvent.MOUSE_UP, dropIt);
  }
  private function pickUp(event:MouseEvent):void {
   this.startDrag(true);
   this.parent.addChild(this);
   startX = this.x;
   startY = this.y;

  }
  private function dropIt(event:MouseEvent):void {
   this.stopDrag();



   *****if (this.hitTestObject(target1_mc)
   ||this.hitTestObject(target2_mc) )***** {
    this.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    this.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
    this.buttonMode = false;
    this.x = myTarget.x;
    this.y = myTarget.y;
    counter++;
   } else {
    //reply_txt.text = "Try Again!";
    this.x = startX;
    this.y = startY;
   }

  }
 }
}

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

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

发布评论

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

评论(1

£噩梦荏苒 2024-10-15 16:04:09

您需要进入发布设置->flash->设置(按钮)并选择“自动声明舞台实例”。那应该可以解决问题。

编辑

在你的recieptMovieClip类中尝试这个:

MovieClip(root).target1_mc
MovieClip(root).target2_mc

因为输入很烦人,所以你可以将MovieClip(root)存储在变量中。请务必注意,除非您的 movieClip 位于显示列表中,否则 root 属性不存在。因此,直到你的类被 addChild() 添加之前,这将不起作用。

另一种方法是使用文档类并将对主时间线的引用存储在静态变量中。我曾经写过一篇关于此事的博客文章。 看看

如果您使用该博文中的技术,您可以从任何地方访问主时间线,如下所示:

Main.display.target1_mc

希望有帮助。如果您需要,我可以上传一两个示例。

You need to go to publish settings->flash->settings(button) and select "Automatically declare stage instances." That should do the trick.

EDIT

Try this inside your recieptMovieClip class:

MovieClip(root).target1_mc
MovieClip(root).target2_mc

Because this is annoying to type you can store MovieClip(root) in a variable. It's important to note that the root property doesn't exist unless your movieClip is on the display list. So until your class has been added wth addChild() this will not work.

An alternative method is to use a document class and store a reference to the main timeline in a static variable. I wrote a blog post about that once. Have a look

If you use the technique in that blogpost you could access the main timeline from anywhere like this:

Main.display.target1_mc

Hope that helps. I can upload an example or two if you need them.

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