用java3d写游戏

发布于 2024-10-14 22:57:14 字数 1173 浏览 4 评论 0原文

我编写了这个程序,但它有异常,

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)

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.

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)

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

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

发布评论

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

评论(1

假面具 2024-10-21 22:57:14

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

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

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.

(There are exceptions to the "only once in the graph", via weaker references instead of parent/child, e.g. for attributes)

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