如何从文档类 AS3 中定位舞台上的影片剪辑?
我正在制作动态拖放游戏。 我有一个包含拖放代码的拖动项目的类。
我的问题是我无法调用/访问我已经在命中测试语句中放在舞台上的影片剪辑。
这是我的代码和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要进入发布设置->flash->设置(按钮)并选择“自动声明舞台实例”。那应该可以解决问题。
编辑
在你的recieptMovieClip类中尝试这个:
因为输入很烦人,所以你可以将MovieClip(root)存储在变量中。请务必注意,除非您的 movieClip 位于显示列表中,否则 root 属性不存在。因此,直到你的类被 addChild() 添加之前,这将不起作用。
另一种方法是使用文档类并将对主时间线的引用存储在静态变量中。我曾经写过一篇关于此事的博客文章。 看看
如果您使用该博文中的技术,您可以从任何地方访问主时间线,如下所示:
希望有帮助。如果您需要,我可以上传一两个示例。
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:
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:
Hope that helps. I can upload an example or two if you need them.