用java3d写游戏

时间:2011-01-28 17:24:52

标签: java 3d

我编写了这个程序,但它在行中有例外

group.addChild(tg);

但是当我添加

TransformGroup tg = new TransformGroup();

进入for循环块,运行时遇到任何问题请告诉我原因。

感谢。

这是我的代码

public BranchGroup Creat()
{
    BranchGroup group = new BranchGroup();
    TransformGroup tg = new TransformGroup();
    for(float x = 0.0f; x < 1.0f; x += 0.1f)
    {
         Transform3D td = new Transform3D();
         Vector3f vector3f = new Vector3f(x, x, x);             
         td.setTranslation(vector3f);
         tg.setTransform(td);
         tg.addChild(new Cone(0.05f, 0.1f));
         group.addChild(tg);             
   }



    return group;
}

这是例外

Exception in thread "main" javax.media.j3d.MultipleParentException: Group.addChild: child already has a parent
    at javax.media.j3d.GroupRetained.checkValidChild(GroupRetained.java:478)
    at javax.media.j3d.GroupRetained.addChild(GroupRetained.java:487)
    at javax.media.j3d.Group.addChild(Group.java:290)
    at t39.Draw.Creat(Draw.java:68)
    at t39.Draw.<init>(Draw.java:50)
    at t39.Main.main(Main.java:22)

1 个答案:

答案 0 :(得分:1)

相同的元素在场景图中不能存在多次。当您在循环中创建新的TransformGroup时,它不会破坏规则,但如果您没有为每个addChild()创建一个新规则,则会破坏此规则。

(“图中只有一次”有例外,通过较弱的引用而不是父/子,例如属性)