我在拖放功能和对象选择方面有问题。
我创建了一个简单的flash编曲器(添加桌子图标到舞台室)。我有一个按钮,可以创建表图标的新实例,我可以在舞台上拖放它。
问题是,我只能拖放最后添加的图标。如果我添加新的实例od图标,我不能接受(拖放)之前创建的任何图标:/
我的代码在这里:主要类
import flash.events.MouseEvent;
import flash.events.Event;
import com.adobe.images.JPGEncoder;
import flash.geom.Point;
btn_middleTable.addEventListener(MouseEvent.CLICK, f_middleIco);
btn_bigTable.addEventListener(MouseEvent.CLICK, f_bigIco);
btnSave.addEventListener(MouseEvent.CLICK, f_save);
function f_middleIco(event:MouseEvent):void
{
var middle:MiddleIco = new MiddleIco();
middle.x = 20;
middle.y = 20;
stage.addChild(middle);
trace("created");
}
function f_bigIco(event:MouseEvent):void
{
var big:BigIco = new BigIco();
big.x = 20;
big.y = 20;
stage.addChild(big);
trace("created");
}
function f_save(event:MouseEvent)
{
var jpgEncoder:JPGEncoder;
jpgEncoder = new JPGEncoder(90);
var bitmapData:BitmapData = new BitmapData(stage.width, stage.height);
bitmapData.draw(stage, new Matrix());
var img = jpgEncoder.encode(bitmapData);
var file:FileReference = new FileReference();
file.save(img, "filename.png");
}
图标实例包:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.Point;
public class BigIco extends MovieClip {
public var active:Boolean;
public function BigIco() {
// constructor code
this.addEventListener(Event.ENTER_FRAME, f_move);
this.addEventListener(MouseEvent.MOUSE_DOWN,downf);
this.addEventListener(MouseEvent.MOUSE_UP,upf);
}
public function f_move(e:Event)
{
if(active==true)
{
startDrag();
}
else if(active==false)
{
stopDrag();
}
}
public function downf(e:MouseEvent)
{
active = true;
}
public function upf(e:MouseEvent)
{
active = false;
}
}
}
我能做些什么来选择鼠标光标上的每个图标(实例)?
理论:startDrag
和stopDrag
不应该被重复调用,但它们将在每帧、每添加一个剪辑中被调用一次,因为您使用的是ENTER_FRAME
侦听器。
我还没有测试过这一点,但调用stopDrag
实际上可能会停止任何拖动,包括在其他剪辑上,因为首先只允许拖动一个Sprite
(因此MovieClip
)。
因此,如果该理论成立,则第一个图标上的startDrag()
将立即被调用stopDrag()
的新图标上的f_move()
取消。
但在任何情况下,您都不需要ENTER_FRAME
侦听器。这应该会得到相同的结果(除了它实际工作之外)——只需在鼠标侦听器中立即调用拖动方法:
public class BigIco extends MovieClip {
public var active:Boolean;
public function BigIco() {
// constructor code
this.addEventListener(MouseEvent.MOUSE_DOWN, downf);
this.addEventListener(MouseEvent.MOUSE_UP, upf);
}
public function downf(e:MouseEvent)
{
// Unless you're going to use 'active' for other stuff,
// you can remove this line:
active = true;
startDrag();
}
public function upf(e:MouseEvent)
{
// Same here:
active = false;
stopDrag();
}
}
除此之外,我在您的代码中找不到任何真正的问题。
编辑:"完美"版本,如果指针没有超过图标,也会检测鼠标向上:
public class BigIco extends MovieClip {
public function BigIco() {
// Only add mouse down listener here. We'll add up when needed below:
this.addEventListener(MouseEvent.MOUSE_DOWN,downf);
}
public function downf(e:MouseEvent)
{
// Add event listener to stage, so as to be triggered even if
// the pointer isn't over the icon when releasing the button.
// Note that this will also work even with the mouse *outside*
// the stage/swf area:
stage.addEventListener(MouseEvent.MOUSE_UP,upf);
startDrag();
}
public function upf(e:MouseEvent)
{
// Remember to remove the event listener after use:
stage.removeEventListener(MouseEvent.MOUSE_UP, upf);
stopDrag();
}
}