用java3d写游戏
我编写了这个程序,但它有异常,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
同一元素在场景图中不能多次存在。当您在循环中创建一个新的
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 everyaddChild()
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)