Java Reference
In-Depth Information
var isStatic = true;
startingX = control.translateX;
startingY = control.translateY;
startingRot = control.rotate;
var box = new net.phys2d.raw.shapes.Box(control.width, control.height);
body = new Body(box, 10);
staticBody = new StaticBody(box);
staticBody.setPosition(control.translateX + control.width/2.0, control.translateY +
body.setPosition(control.translateX + control.width/2.0, control.translateY +
function update(){
if (not isStatic){
control.translateX = body.getPosition().getX() - control.width/2.0;
control.translateY = body.getPosition().getY() - control.height/2.0;
control.rotate = Math.toDegrees(body.getRotation());
Listing 6-10 shows a function named doReplace and a helper class called ControlHandler . The
function doReplace follows the same pattern as the doReplace functions in the chapter on transitions. It
takes two Nodes , in this case Groups , and a function named doAfter that is called when the transition is
The function doReplace looks through the contents of the Group nodeToReplace and finds all children
that are of type Control . For each Control node found, a ControlHandler is created and added to the
Sequence handlers and to the Sequence inactives . The idea here is that handlers will keep a reference to
all Controls and Bodies in the animation. The Sequence inactives keeps track of which Controls have
not yet started to fall. The staticBody of each handler is also added to the world . If you recall a World
object is used to do the actual physics calculations, the staticBody is added at his point so that so that
the first few falling bodies will have something to bounce off of.
The Sequence inactives is then shuffled so that the Controls will start to fall in a random order. In
my testing I liked the randomness, but this line can easily be commented out to create a reproducible
The Timeline addBodies is created and started; this Timeline pops ControlHandlers from the
inactives and activates them. The process of activating a ControlHandler means that it should swap out
its StaticBody for a Body . This makes the Control start to fall. The Timeline addBodies activates a new
ControlHandler every .5 seconds, so that all of the Controls don't just start dropping all at once.
The Timeline worldUpdater is used to advance the location of each Body . This is basically identical to
the Timeline used in this chapter to explore physics. The world object is stepped and each handler is
updated. The update function takes the location and rotation of the body and applies it to the location
and rotation of the Control Node .
Two more Timelines are created to help manage when the transition is done. The Timeline
checkCleanup checks every second to see if all of the Controls have fallen below the original bottom
Search WWH ::

Custom Search