0

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

然后我想根据这个计算器的结果添加Circle。因此,如果结果是 10,它将绘制一个半径为 10 厘米的圆。如果结果是 100,那么它会在 100 处绘制它,你会得到图片。这行得通。

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

我对 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 = [];
}
4

1 回答 1

0

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

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

在你的代码的某个地方,定义checkName函数

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

selectedPlatformArraytrue现在将包含为该函数返回的所有元素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 = "";
}
于 2013-01-20T14:04:20.780 回答