简体   繁体   English

用Java3d编写游戏

[英]write game with java3d

I write this program but it has exception in line 我写了这个程序,但是它有例外

group.addChild(tg);

but when i add 但是当我添加

TransformGroup tg = new TransformGroup();

into the for loop block it runs with any problem please tell me it's reason. 进入for循环块,它会出现任何问题,请告诉我原因。

Thanks. 谢谢。

this is my code 这是我的代码

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;
}

this is it's exception 这是例外

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)

The same element can't exist more than once in the scene graph. 同一元素在场景图中不能重复存在。 When you create a new TransformGroup within the loop it doesn't break the rule, but if you don't create a new one for every addChild() you break this rule. 当您在循环中创建一个新的TransformGroup ,它不会违反规则,但是,如果您没有为每个addChild()创建一个新的TransformGroup ,则会违反此规则。

(There are exceptions to the "only once in the graph", via weaker references instead of parent/child, eg for attributes) (“图中仅一次”是例外,通过较弱的引用而不是父/子引用,例如属性)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM