Pointing with with InstanceName

Hi All
I want to know how can we assingn an instance name for a movie clip or sprite created dynamically

i have used .name and its working fine !!
but i am not able to call it from another function!!

//////////----------------------------------- here is the code
import flash.display.Sprite;
import flash.display.LineScaleMode;
import flash.display.CapsStyle;
import flash.display.JointStyle;
import flash.display.Shape;

function draw_trapezoid()
{

var trapezoid:Shape = new Shape();

trapezoid.graphics.lineStyle(10, 0xFFD700, 1, false, LineScaleMode.VERTICAL,
   CapsStyle.NONE, JointStyle.MITER, 10);

trapezoid.graphics.moveTo(100, 100);

trapezoid.graphics.lineTo(120, 50);
trapezoid.graphics.lineTo(200, 50);
trapezoid.graphics.lineTo(220, 100);
trapezoid.graphics.lineTo(100, 100);
trapezoid.name = 'trapezoid1';


this.addChild(trapezoid);

}

draw_trapezoid();

//--------------------- bug is here

trapezoid1.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown);

//------
function thumbDown(e:MouseEvent):void
{
trapezoid1.y+= trapezoid1.y*.02;
}
//------

//-----------------------------------

i dont want to write the code in the draw_trapezoid function
if i write there its working perfect
but i want to access and assign events from another function

//---- this is very simple and basic question but i was stuck up here and struggling hard to handle projects in as3

thanks in advance

I’m not sure what goal of this exercise actually is.
I couldn’t decide what “trapezoid.y+= trapezoid.y*.02;” is supposed to do.

Here’s my 2¢…

//I added Tweenmax to the script to see if it would function; it does.
//I also used Sprite instead of Shape. Shape would not work with TweenMax.
stop();
import gs.TweenMax;
import fl.motion.easing.*;

import flash.display.Sprite;
import flash.display.LineScaleMode;
import flash.display.CapsStyle;
import flash.display.JointStyle;
import flash.display.Shape;


var trapezoid:Sprite = new Sprite();
addChild(trapezoid);
trapezoid.name = 'trapezoid1';

function draw_trapezoid() {
	with (trapezoid.graphics) {
		lineStyle(10, 0xFFD700, 1, false, LineScaleMode.VERTICAL, CapsStyle.NONE, JointStyle.MITER, 10);
		moveTo(100, 100);
		lineTo(120, 50);
		lineTo(200, 50);
		lineTo(220, 100);
		lineTo(100, 100);
	}
}

draw_trapezoid();

trapezoid.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown, false, 0, true);
function thumbDown(e:MouseEvent):void {
	TweenMax.to(trapezoid, 1, {y:200});//used this value to get some feedback.
	trace(e.target.name);
//traces "trapezoid1" as expected; not sure about the relevance of the .name property here.
//it's certainly not required for tweening.
}

One function my friend: getChildByName().

Essentially the variable name by which you declare an object IS its instance name, and it cannot be dynamic. But you can have other vars reference it which means you can use array keys for dynamic naming.

With respect to your montage you have two options.

The more nub one:

this.addChild(trapezoid)
this.someArray[“trapezoid1”] = trapezoid;

Then refer to this.someArray[“trapezoid1”] when you want to refer to that trapezoid in other functions.

OR the better way:

trapezoid.name = “trapezoid1”;
this.addChild(trapezoid);

Then use this.getChildByName(“trapezoid1”) when you want to refer to it in a different function.