【发布时间】:2013-07-11 19:18:29
【问题描述】:
我在拖放功能和对象选择方面遇到问题。
我创建了简单的 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;
}
}
}
我该怎么做才能选择实际上在鼠标光标上的每个图标(实例)?
【问题讨论】:
-
请注意,虽然我的回答完全删除了
f_move,但在这种方法中,不需要else if(active==false)- 如果active不是真的,那就是假的,所以else就足够了。您还可以将if(active==true)缩短为if (active)。 -
最后一件事:除了相当高级的情况,使用
stage.addChild()将实例添加到主时间线不是一个好主意 - 因为您实际上并没有将它们添加到时间线,而是“在显示层次结构中”(您在 Flash 中看到的主时间线本身就是stage的子级)。它在这里可以工作,但在其他情况下可能会导致很难找到错误,而您真正想要的是this.addChild()。参见例如stackoverflow.com/questions/13096993/correct-use-of-addchild
标签: actionscript-3 flash