【问题标题】:How to target movie clip on the stage from doc class AS3?如何从 doc 类 AS3 定位舞台上的影片剪辑?
【发布时间】:2011-05-27 09:32:44
【问题描述】:

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

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

这是我的代码,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;
   }

  }
 }
}

【问题讨论】:

    标签: actionscript-3 class movieclip flash-ide


    【解决方案1】:

    你需要去publish settings->flash->settings(button)并选择“Automatically declare stage instances”。这应该可以解决问题。

    编辑

    在你的 recieptMovieClip 类中试试这个:

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

    因为这很烦人,所以您可以将 MovieClip(root) 存储在一个变量中。请务必注意,除非您的 movieClip 在显示列表中,否则 root 属性不存在。因此,在使用 addChild() 添加您的课程之前,这是行不通的。

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

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

    Main.display.target1_mc
    

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

    【讨论】:

    • 它被选中但我仍然有同样的问题
    • 出现错误说“访问未定义的属性target1_mc”
    • 您确定为movieClip 指定了实例名称target1_mc 吗?也许您应该将文件上传到某个地方,以便我们查看。
    • 是的,我确定他们有这些名字,但这不是 doc 类,对不起,我误解了,它是一个自定义类。这有什么不同吗???
    猜你喜欢
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多