dynamic objects in if condition

how to make if condition which contains dynamic object? i tried this way, but error

function pass(xxx:String,yyy:String,zzz:String)
{
    //trace(xxx,yyy,zzz);
    if (this[xxx].hitTestObject(this[yyy])) //an original if (obj1.hitTestObject(obj2))
    {
       trace("right");
    }
    else
    {
      trace("fail");
    }
}

"this[]" is not work, TypeError: Error #1010: A term is undefined and has no properties. "this[]" can work if it is outside "if". Is there any other way for this problem? Thanks before

Answers


You should use getChildByName(), if you are transferring names of the MCs, but check if that name is a direct child of this.

function pass(xxx:String,yyy:String,zzz:String):void {
    var x=this.getChildByName(xxx);
    if (!x) return;
    var y=this.getChildByName(yyy);
    if (!y) return; // insert similar for zzz here
    if (x.hitTestObject(y))     {
        trace("right");
    }
    else
    {
        trace("fail");
    }
}

Otherwise specify what inputs does thje function have.


Unless you have a specific reason to be supplying the object names as Strings, I suggest changing the argument types to DisplayObject:

function pass(a:DisplayObject, b:DisplayObject):void
{
    if(a.hitTestObject(b))
    {
        trace("right");
    }
    else
    {
        trace("fail");
    }
}

If you need to use the Strings, just do this:

var obj1:DisplayObject = getChildByName("obj1");
var obj2:DisplayObject= getChildByName("obj2");

pass(obj1, obj2);

Need Your Help

Possible to eliminate “NOT IN” subquery from MySQL delete?

mysql optimization subquery mariadb

I've got two InnoDB tables, let's call them tableD and tableC. TableD has rows with a varchar primary key called id. TableC has rows with a BIGINT primary key called seq and another varchar column

Where to download pywin64?

python pywin

Now,I use windows7 professional,the version of python is 2.7.6,I need a editor to write code.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.