使用数组中剪辑的名称将addCircle添加到现有的动画片段

时间:2013-01-19 22:37:11

标签: actionscript-3

我目前正在建设一个项目,我有一张地图上有许多船只和飞机。我想要实现的是检查它们之间的视线距离。 我已经设置了一个LOS计算器,它可以检查一个平台的高度和第二个平台的高度,然后给出响应。这很好。

然后我想基于此计算器的结果添加循环。因此,如果结果为10,则会绘制一个半径为10厘米的圆。如果结果为100则将其绘制为100,即可获得图片。这很有效。

我现在的问题是,我需要能够在进行计算之前或之后点击一个平台,并将.addCircle添加到该movieClip。我已经设置了一个数组来存储movieclips实例名称并跟踪它。我在舞台上添加了一个字段,以便您可以单击一个平台,它将识别单击的平台。我只是迷失了如何将圆圈放入已被点击的movieClip中。

我对AS3很新,所以这是我的开始。任何帮助将不胜感激。

我的代码附在下面。我希望我已正确插入。再次感谢

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

stage.focus=ht1;


// creation of array containing movieclips and code that adds the clicked movieclip to Array-platformClicked
var platformArray:Array = [arunta_mc, f15_mc];
var platformClicked = [];
var selectedPlatform:MovieClip = new MovieClip();

for(var i:int = 0; i < platformArray.length; i++) {
    platformArray[i].buttonMode = true;
    platformArray[i].addEventListener(MouseEvent.CLICK, item_onClick);
}

function item_onClick(event:MouseEvent):void {
    var selectedPlatformArray:Array = platformArray.filter(checkName);
    selectedPlatform = selectedPlatformArray[0];
    myText.text = event.currentTarget.name + " was clicked";
    var platformClicked = String(event.currentTarget.name);
trace(platformClicked);
}



function checkName(item:MovieClip, index:int, array:Array):Boolean
{
    return(item.name == platformClicked);
}



//setup of LOS Calculator code
var counter:Number=1;
operator_txt.text = "+";
ht1.restrict="-0123456789.";
ht2.restrict="-0123456789.";
var myresult:Number;
var test = [];

//start of code when equal button is pressed
equal_btn.addEventListener(MouseEvent.CLICK, equalhandler);
var newCircle:Shape = new Shape();//defines circle to be drawn

    function equalhandler(event:MouseEvent):void{
        newCircle.graphics.lineStyle(1, 0x000000);
        newCircle.graphics.beginFill(0x435632);
        newCircle.alpha = .1;
    //start of result code
    result_txt.text = String(int((1.23*(Math.sqrt(Number(parseFloat(ht1.text)+parseFloat(ht2.text)+""))))));
    var test = String(int((1.23*(Math.sqrt(Number(parseFloat(ht1.text)+parseFloat(ht2.text)+""))))));
    trace(test);
    //end of result code
    newCircle.graphics.drawCircle(0,0,test);//add circle based on LOS calculation
    newCircle.graphics.endFill();
    //var selectedPlatform:MovieClip = selectedPlatformArray[0];
    selectedPlatform.addChild(newCircle);//this is where I need to add newCircle to the movieClip that is clicked
    trace(selectedPlatform);
//trace(platformClicked);
}


//start of code for the clear button
clear_btn.addEventListener(MouseEvent.CLICK, clearhandler);
function clearhandler(event:MouseEvent):void{
ht1.text=ht2.text=result_txt.text="";
removeChild(newCircle);
var test = [];
}

1 个答案:

答案 0 :(得分:0)

您可以使用filter()方法检查每个项目的名称,如下所示:

var selectedPlatformArray:Array = platformArray.filter(checkName);

在代码中的某处,定义checkName函数

function checkName(item:MovieClip, index:int, array:Array):Boolean
{
    return (item.name == platformClicked);
}

selectedPlatformArray现在将包含为true函数返回checkName的所有元素,并且只要您没有多个MovieClips具有相同名称的数组应该只包含一个元素,只需访问数组的第一个元素就可以检索它:

var selectedPlatform:MovieClip = selectedPlatformArray[0];

或者,您也可以使用getChildByName()功能,如下所示:

var selectedPlatform:MovieClip = stage.getChildByName(platformClicked);

然而,这取决于添加对象的位置,如果它们不是全部在同一容器中(或根本没有添加),那么这不是最佳选择。对于小型项目来说,这是一个快速而简单的解决方案。

无论如何,无论您使用何种方法,都可以像往常一样轻松地在equalHandler函数中添加圆圈:

selectedPlatform.addChild(newCircle);

我建议您查看filter()getChildByName()的文档,以便更好地了解它们的工作原理,因为我的示例仅显示了您在这种特定情况下如何使用它们


您需要的完整代码:

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

stage.focus=ht1;


// creation of array containing movieclips and code that adds the clicked movieclip to Array-platformClicked
var platformArray:Array = [arunta_mc, f15_mc];
var platformClicked:String = "";
var selectedPlatform:MovieClip = new MovieClip();

for(var i:int = 0; i < platformArray.length; i++) {
    platformArray[i].buttonMode = true;
    platformArray[i].addEventListener(MouseEvent.CLICK, item_onClick);
}

function item_onClick(event:MouseEvent):void {
    var selectedPlatformArray:Array = platformArray.filter(checkName);
    selectedPlatform = selectedPlatformArray[0];
    myText.text = event.currentTarget.name + " was clicked";
    platformClicked = String(event.currentTarget.name);
    trace(platformClicked);
}



function checkName(item:MovieClip, index:int, array:Array):Boolean
{
    return(item.name == platformClicked);
}



//setup of LOS Calculator code
var counter:Number=1;
operator_txt.text = "+";
ht1.restrict="-0123456789.";
ht2.restrict="-0123456789.";
var myresult:Number;
var test:String = "";

//start of code when equal button is pressed
equal_btn.addEventListener(MouseEvent.CLICK, equalhandler);
var newCircle:Shape = new Shape();//defines circle to be drawn

function equalhandler(event:MouseEvent):void{
    newCircle.graphics.lineStyle(1, 0x000000);
    newCircle.graphics.beginFill(0x435632);
    newCircle.alpha = .1;
    //start of result code
    result_txt.text = String(int((1.23*(Math.sqrt(Number(parseFloat(ht1.text)+parseFloat(ht2.text)+""))))));
    test = String(int((1.23*(Math.sqrt(Number(parseFloat(ht1.text)+parseFloat(ht2.text)+""))))));
    trace(test);
    //end of result code
    newCircle.graphics.drawCircle(0,0,test);//add circle based on LOS calculation
    newCircle.graphics.endFill();
    //var selectedPlatform:MovieClip = selectedPlatformArray[0];
    selectedPlatform.addChild(newCircle);//this is where I need to add newCircle to the movieClip that is clicked
    trace(selectedPlatform);
    //trace(platformClicked);
}


//start of code for the clear button
clear_btn.addEventListener(MouseEvent.CLICK, clearhandler);
function clearhandler(event:MouseEvent):void{
    ht1.text=ht2.text=result_txt.text="";
    selectedPlatform.removeChild(newCircle);
    test = "";
}