Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the <code>filter()</code> method to check each item's name, like so:</p> <pre><code>var selectedPlatformArray:Array = platformArray.filter(checkName); </code></pre> <p>and somewhere in your code, define the <code>checkName</code> function</p> <pre><code>function checkName(item:MovieClip, index:int, array:Array):Boolean { return (item.name == platformClicked); } </code></pre> <p><code>selectedPlatformArray</code> will now contain all elements that return <code>true</code> for the <code>checkName</code> function, and as long as you don't have multiple <code>MovieClips</code> with the same name the array should only contain one element, which you can retrieve simply by accessing the first element of the array:</p> <pre><code>var selectedPlatform:MovieClip = selectedPlatformArray[0]; </code></pre> <p>Alternatively, you can also use the <code>getChildByName()</code> function, like so:</p> <pre><code>var selectedPlatform:MovieClip = stage.getChildByName(platformClicked); </code></pre> <p>However this depends on where the objects are added to, and if they're not all in the same container (or not added at all), then this isn't the best option. It's a quick and simple solution for small projects though.</p> <p>Anyway, whatever method you use you can then easily add the circle to it in your <code>equalHandler</code> function as usual:</p> <pre><code>selectedPlatform.addChild(newCircle); </code></pre> <p>I'd recommend checking out the documentation for both <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#filter%28%29" rel="nofollow"><code>filter()</code></a> and <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#getChildByName%28%29" rel="nofollow"><code>getChildByName()</code></a>, to get a better understanding of how they work, since my examples only showed how you'd use them in this specific situation.</p> <hr> <p>Complete code that you should have:</p> <pre><code>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 &lt; 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 = ""; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload