Java Reference
In-Depth Information
4. Using the generated stub —as an instance of Node , the generated stub class
can be added into a scene graph. The following abbreviated snippet shows class
NbGeneratedSymbols used as the basis for a simple game. The full code for
this is located at ch08/source-code/src/fxzdemo/Targeting.fx .
...
def symbols = NbGeneratedSymbols {};
def triangle = symbols.triangle as ImageView;
def target = symbols.target as ImageView;
target.x = triangle.x;
symbols.requestFocus();
symbols. onKeyPressed = function (e:KeyEvent) {
if(e.code.equals(KeyCode.VK_RIGHT)){
target.translateX = target.translateX + 15;
triangle.translateX = triangle.translateX + 15;
}
if(e.code.equals(KeyCode.VK_LEFT)){
target.translateX = target.translateX - 15;
triangle.translateX = triangle.translateX - 15;
}
if(e.code.equals(KeyCode.VK_SPACE)){
for(i in [0..5]){
var icon = symbols.getObject("icon_{i}") as Node;
if(icon.intersects(target.boundsInParent)){
icon.visible = false;
break;
}
}
}
}
...
When the variable symbols is added to the scene, and the script is executed, it displays the
game shown in the next screenshot. As the red triangle moves left or right using the arrow
keys, it targets the icons lined up at the top portion of the window. When the Spacebar key is
pressed to fire, the target icon goes off screen:
 
Search WWH ::




Custom Search