在Flash中设置重置放置对象按钮(as3)

时间:2016-02-04 07:49:26

标签: actionscript-3 flash

我有一个拖放项目,每当我点击一个动画片段时,就可以制作一个可以拖动的克隆。我想知道当按下按钮时如何制作一个可以重置/删除克隆的按钮。

这是我到目前为止所得到的:

import flash.display.MovieClip;


var latestClone:MovieClip;

plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);

function onPlusPressed(event:MouseEvent):void
{

latestClone = new Plus();

latestClone.x = event.stageX;
latestClone.y = event.stageY;

addChild(latestClone);

latestClone.startDrag();

latestClone.addEventListener(MouseEvent.MOUSE_DOWN, latestClone.startDrag);

}

stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);

function onStageReleased(event:MouseEvent):void
{

if(latestClone != null){

    latestClone.stopDrag();

}
}

4 个答案:

答案 0 :(得分:0)

在数组中添加所有克隆,并遍历数组并对数组中的每个项目执行removeChild。所以:

var items:Array = new Array();
....
addChild(latestClone);
items.push(latestClone); 
....
var resetButton:SimpleButton = new SimpleButton();
//set your button properties here
resetButton.addEventListener(MouseEvent.CLICK, onResetClicked);
addChild(resetButton);

function onResetClicked(e:MouseEvent):void
{
    reset();
}
function reset():void
{
    for (var i:uint = 0; i < items.length; i ++)
    {     
         removeChild(items[i]);
         items[i] = null;
    }
    items = new Array();
}

希望这有帮助。

答案 1 :(得分:0)

我通过发布的代码和搜索其他问题测试了几种可能性,找到了我的问题的解决方案。

这是有效的代码:

import flash.display.MovieClip;
import flash.events.MouseEvent;

var latestClone:MovieClip;

plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);

function onPlusPressed(event:MouseEvent):void
{

latestClone = new Plus();

latestClone.x = event.stageX;
latestClone.y = event.stageY;

addChild(latestClone);

latestClone.startDrag();

latestClone.addEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed);

}

function onClonedPlusPressed(event:MouseEvent):void{


latestClone = MovieClip(event.currentTarget);


latestClone.startDrag();
}

stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);

function onStageReleased(event:MouseEvent):void
{

if(latestClone != null){

latestClone.stopDrag();
items.push(latestClone); 
}
}



resetButton.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);



function fl_MouseClickHandler(event:MouseEvent):void
{



}

var items:Array = new Array();






resetButton.addEventListener(MouseEvent.CLICK, onResetClicked);
addChild(resetButton);

function onResetClicked(e:MouseEvent):void
{
reset();
}
function reset():void
{
for (var i:uint = 0; i < items.length; i ++)
{        items[i].removeEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed);
     if(items[i].parent) items[i].parent.removeChild(items[i]);
      items[i] = null;
}
items = new Array()



}

答案 2 :(得分:0)

只需声明容器变量:

var clone_container:Sprite = new Sprite();

将所有克隆放入其中,比你可以非常轻松地清除它:

while(clone_container.numChildren > 0){
    clone_container.removeChildAt(0);
}

全部......

答案 3 :(得分:0)

你最常添加一个数组并将对象推入其中,

你的解决方案:

import flash.display.MovieClip;
import flash.events.Event;
var latestClone:MovieClip;
plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);
var plusArray:Array = new Array();

resetbtn.addEventListener(MouseEvent.CLICK,resetFunc);
function resetFunc(e:Event)
{
    for (var i=0; i<plusArray.length; i++)
    {
        removeChild(plusArray[i]);
    }
    plusArray = new Array()
}

function onPlusPressed(event:MouseEvent):void
{

    latestClone = new Plus();

    latestClone.x = event.stageX;
    latestClone.y = event.stageY;

    plusArray.push(latestClone);
    addChild(plusArray[plusArray.length-1]);
    plusArray[plusArray.length - 1].startDrag();
    plusArray[plusArray.length - 1].addEventListener(MouseEvent.MOUSE_DOWN, plusArray[plusArray.length-1].startDrag);

}

stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);

function onStageReleased(event:MouseEvent):void
{

    if (latestClone != null)
    {

        latestClone.stopDrag();

    }
}

祝你好运