我有一个 AS3 类,它用动态图像扩展 movieClip,如何使该图像可拖动?

发布于 2024-09-13 21:28:33 字数 1069 浏览 7 评论 0原文

我需要使一个项目可拖动(可拖动?)抱歉,如果我的术语不正确!

我有一个类,我将在其中存储变量并进行计算:

package Classes
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;

    public class PrintItem extends MovieClip
    {     
        public var imageLoader:Loader;

        public function PrintItem()
        {

        }

        public function loadImage(url:String):void
        {
            imageLoader = new Loader();
            imageLoader.load(new URLRequest(url));
        }
    }
} 

我创建该类的一个新实例,在 loadImage 中向它传递一个 URL,然后将其添加到画布中,如下所示:

var myItem:PrintItem = new PrintItem();
myItem.loadImage("pic.jpg");
this.addChild(myItem.imageLoader);
myItem.imageLoader.x = 50;
myItem.imageLoader.y = 50;
myItem.imageLoader.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
myItem.imageLoader.addEventListener(MouseEvent.MOUSE_UP, dropIt);

但我被困住了。我需要使该项目可拖动并编写 pickUp 和 dropIt 函数。 event.target.startDrag(false);不起作用(Loader 不可拖动)和 event.target.parent.startDrag(false);不起作用,因为整个舞台变得可拖动!帮助!是因为舞台上只添加了Loader吗?

I need to make an item draggable (dragable?) Sorry if my terminology is not right!

I have a class where I will store variable and do calculations:

package Classes
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;

    public class PrintItem extends MovieClip
    {     
        public var imageLoader:Loader;

        public function PrintItem()
        {

        }

        public function loadImage(url:String):void
        {
            imageLoader = new Loader();
            imageLoader.load(new URLRequest(url));
        }
    }
} 

I create a new instance of the class, I pass it an URL in loadImage and I add it to the canvas like this:

var myItem:PrintItem = new PrintItem();
myItem.loadImage("pic.jpg");
this.addChild(myItem.imageLoader);
myItem.imageLoader.x = 50;
myItem.imageLoader.y = 50;
myItem.imageLoader.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
myItem.imageLoader.addEventListener(MouseEvent.MOUSE_UP, dropIt);

But there I am stuck. I need to make this item draggable and write the pickUp and dropIt functions. event.target.startDrag(false); doesn't work (Loader isn't draggable) and event.target.parent.startDrag(false); doesn't work as the whole stage becomes draggable! HELP! Is it because only the Loader is added to the stage?

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

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

发布评论

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

评论(1

只有一腔孤勇 2024-09-20 21:28:33

好的。我知道这只是几分钟,但我已经找到了答案。把它写在这里,我的脑子里就清楚了。这是对我有用的方法,以防对其他人有帮助:

首先,我将加载器图像作为子项添加到类中,如下所示:

public function loadImage(url:String):void
{
    imageLoader = new Loader();
    imageLoader.load(new URLRequest(url));
    this.addChild(imageLoader); // IMPORTANT!
    this.mouseChildren = false;
    this.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    this.addEventListener(MouseEvent.MOUSE_UP, dropIt);
}

然后我只需将类添加到舞台,而不是加载器:

this.addChild(myItem);

回到类,我添加了这个:

public function pickUp(event:MouseEvent):void
{
  trace("Pickup");
  this.startDrag(false);
}
public function dropIt(event:MouseEvent):void
{
  this.stopDrag();
  trace("X = " + event.target.x + " Y = " + event.target.y); // Just to help me
}

等等瞧!成功了!

Ok. I know it has only been a few minutes, but I have found the answer. Writing it down here cleared it in my head. Here's what worked for me in case it helps anybody else:

First, I added the Loader image as a child to the class like this:

public function loadImage(url:String):void
{
    imageLoader = new Loader();
    imageLoader.load(new URLRequest(url));
    this.addChild(imageLoader); // IMPORTANT!
    this.mouseChildren = false;
    this.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    this.addEventListener(MouseEvent.MOUSE_UP, dropIt);
}

And then I only had to add the class to the stage, not the loader:

this.addChild(myItem);

Back to the class, I added this:

public function pickUp(event:MouseEvent):void
{
  trace("Pickup");
  this.startDrag(false);
}
public function dropIt(event:MouseEvent):void
{
  this.stopDrag();
  trace("X = " + event.target.x + " Y = " + event.target.y); // Just to help me
}

Et voila! It worked!

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