如何从精灵as3中选择一个对象

时间:2015-03-20 07:38:51

标签: actionscript-3

当它是几个对象时,如何从精灵中选择一个对象(由文件加载的图片)? 我想拖动鼠标点击的一个。

private var spstage1:Sprite = new Sprite();
private var vecpic1:Vector.<String> = new Vector.<String>();
private var iconpic1:iconpic=new iconpic(); 
spstage1.graphics.beginFill(0xcccccc);
spstage1.graphics.drawRect(250,100,450,668);
stage.addChild(spstage1);
spstage1.addChild(iconpic1);
iconpic1.addEventListener(MouseEvent.CLICK, picclicked);

function picclicked(event:MouseEvent):void {
            var txtFilter:FileFilter = new FileFilter("picture", "*.jpg;*.png"); 
            //root.browseForOpen("Open", [txtFilter]); 
            file = new File(); 
            currentload="pic";
            file.addEventListener(Event.SELECT, dirSelected); 
            file.browseForOpen("Select a picture",[txtFilter]);
            //
            /*file.browse();
            file.addEventListener(Event.SELECT,onselect);*/
        }

protected function dirSelected(e:Event):void { 
                var re1:URLRequest=new URLRequest(file.nativePath);
                loader=new Loader();
                loader.load(re1);
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadCompletepic);
            }

function loadCompletepic(event:Event):void{
            var pic:BitmapData=new BitmapData(loader.width,loader.height,false);
            pic.draw(loader);
            bitmap=new Bitmap(pic);
            spstage1.addChild(bitmap);
            bitmap.x=ix;
            bitmap.y=iy;
            vecpic1.push(bitmap.name);

        }

1 个答案:

答案 0 :(得分:0)

MOUSE_DOWN事件监听器添加到spstage1 在侦听器处理程序中,检查:

function clickHandler(evt:MouseEvent):void {
    if (evt.target is Bitmap) {
        // here you decide what you have clicked on.
        (evt.target as DisplayObject).startDrag(); // do the stuff for dragging etc.
    }
}

这里evt.target将是被点击的对象。 您所要做的就是确定它是否是正确的对象。在此示例中,它只是检查为Bitmap类型。如果容器中有其他位图不需要单击,请使用其他方法,例如将加载的对象存储在某处,然后将目标与所有存储的对象进行比较。或者查看您似乎已经拥有的bitmap.name等等。