1 个对象通过第三个对象链接到另一个对象,我如何在知道其他 2 个对象的基础上查找第三个对象?

发布于 2024-10-11 11:58:26 字数 2436 浏览 1 评论 0原文

我专门从事 AS3 工作,但我觉得这是一个非常不可知的问题。

具体来说,我有可以链接到其他球体对象的球体对象。它们通过第三个对象链接。我希望能够仅使用当前链接在一起的 2 个球体对象的知识来查找链接对象。你会如何处理这个问题?

我希望必须在每个球体对象中存储一个唯一的标识符字符串,只是为了避免全局检查以确保它们是唯一的......但如果归结为这一点,我并不反对这个想法。

----- 解决方案 ------

这是我最终得到的解决方案:

private function breakAtomicBonds( p_interactiveMatter:InteractiveMatter ):void
{
    var matchAmount:int;
    var key:Object;
    var atom:InteractiveMatter;
    var constraint:Constraint;
    var atoms:Vector.<InteractiveMatter>;
    var atomVOs:Vector.<InteractiveMatterVO>;
    var bond:InteractiveMatterVO;
    var bondIndex:int;
    var i:int;
    for( i = p_interactiveMatter.interactiveMatterVO.bonds.length - 1; i >= 0; i--) // each( bond in p_interactiveMatter.interactiveMatterVO.bonds )
    {
    bond = p_interactiveMatter.interactiveMatterVO.bonds[i];
    for( key in _constraints )
    {
        if( key is Constraint )
        {
            constraint = key as Constraint;
            atoms = _constraints[key] as Vector.<InteractiveMatter>;
            atomVOs = new Vector.<InteractiveMatterVO>;

            for each( atom in atoms )
            {
                atomVOs.push( atom.interactiveMatterVO );
            }

            matchAmount = 0;

            if( atomVOs.indexOf(p_interactiveMatter.interactiveMatterVO) != -1 )
            {
                matchAmount++;
            }

            if( atomVOs.indexOf( bond ) != -1 )
            {
                matchAmount++;
            }

            if( matchAmount == 2 )
            {
                trace( 'found constraint!' );
                _physicsWorld.removeConstraint( constraint );
                delete _constraints[constraint];

                for each(atom in atoms)
                {
                    bondIndex = atom.interactiveMatterVO.bonds.indexOf( bond )
                    if( bondIndex != -1 )
                    {
                        atom.interactiveMatterVO.bonds.splice( bondIndex, 1 ); 
                    }
                    bondIndex = atom.interactiveMatterVO.bonds.indexOf( p_interactiveMatter.interactiveMatterVO );
                    if( bondIndex != -1 )
                    {
                                    atom.interactiveMatterVO.bonds.splice( bondIndex, 1 );
                        }
                    }

                    break;
                }
            }
        }
    }
}

I'm working in AS3 specifically but I feel that this is a pretty agnostic question.

Specifically I have sphere objects that can be linked to other sphere objects. They are linked though a third object. I would like to be able to look up the the linking object with only the knowledge of 2 sphere objects that are currently linked together. How would you approach this?

I would like to having to store a unique identifier string in each sphere object, just to avoid a global check to ensure that they are unique... but not opposed to the idea if it comes down to it.

----- solution ------

here is the solution I ended up with:

private function breakAtomicBonds( p_interactiveMatter:InteractiveMatter ):void
{
    var matchAmount:int;
    var key:Object;
    var atom:InteractiveMatter;
    var constraint:Constraint;
    var atoms:Vector.<InteractiveMatter>;
    var atomVOs:Vector.<InteractiveMatterVO>;
    var bond:InteractiveMatterVO;
    var bondIndex:int;
    var i:int;
    for( i = p_interactiveMatter.interactiveMatterVO.bonds.length - 1; i >= 0; i--) // each( bond in p_interactiveMatter.interactiveMatterVO.bonds )
    {
    bond = p_interactiveMatter.interactiveMatterVO.bonds[i];
    for( key in _constraints )
    {
        if( key is Constraint )
        {
            constraint = key as Constraint;
            atoms = _constraints[key] as Vector.<InteractiveMatter>;
            atomVOs = new Vector.<InteractiveMatterVO>;

            for each( atom in atoms )
            {
                atomVOs.push( atom.interactiveMatterVO );
            }

            matchAmount = 0;

            if( atomVOs.indexOf(p_interactiveMatter.interactiveMatterVO) != -1 )
            {
                matchAmount++;
            }

            if( atomVOs.indexOf( bond ) != -1 )
            {
                matchAmount++;
            }

            if( matchAmount == 2 )
            {
                trace( 'found constraint!' );
                _physicsWorld.removeConstraint( constraint );
                delete _constraints[constraint];

                for each(atom in atoms)
                {
                    bondIndex = atom.interactiveMatterVO.bonds.indexOf( bond )
                    if( bondIndex != -1 )
                    {
                        atom.interactiveMatterVO.bonds.splice( bondIndex, 1 ); 
                    }
                    bondIndex = atom.interactiveMatterVO.bonds.indexOf( p_interactiveMatter.interactiveMatterVO );
                    if( bondIndex != -1 )
                    {
                                    atom.interactiveMatterVO.bonds.splice( bondIndex, 1 );
                        }
                    }

                    break;
                }
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与清晨阳光 2024-10-18 11:58:26

对于节点和小朋友们,可以参考复合设计模式

http://blog.createdbychristian.com/the-composite-design-pattern/

For nodes & children, you could refer to the Composite Design Pattern

http://blog.createdbychristian.com/the-composite-design-pattern/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文