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.
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();
开发者_Python百科 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.
(There are exceptions to the "only once in the graph", via weaker references instead of parent/child, e.g. for attributes)
精彩评论