我有一个 AS3 类,它用动态图像扩展 movieClip,如何使该图像可拖动?
我需要使一个项目可拖动(可拖动?)抱歉,如果我的术语不正确!
我有一个类,我将在其中存储变量并进行计算:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的。我知道这只是几分钟,但我已经找到了答案。把它写在这里,我的脑子里就清楚了。这是对我有用的方法,以防对其他人有帮助:
首先,我将加载器图像作为子项添加到类中,如下所示:
然后我只需将类添加到舞台,而不是加载器:
回到类,我添加了这个:
等等瞧!成功了!
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:
And then I only had to add the class to the stage, not the loader:
Back to the class, I added this:
Et voila! It worked!