This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JavaFX and the fly likes Delay program until end TranslateTransition JavaFX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JavaFX
Bookmark "Delay program until end TranslateTransition JavaFX" Watch "Delay program until end TranslateTransition JavaFX" New topic
Author

Delay program until end TranslateTransition JavaFX

Thomas Brouwer
Greenhorn

Joined: Nov 18, 2010
Posts: 1
Hey everyone,

My name is Thomas and I am currently working on a final High School programming project. For this project we are programming a game with JavaFX, in which we feature several mini-games to improve children's mathematical skills. One of about 10 mini-games is a puzzle where you have to reach the exit of the level by sliding over ice.

Let me explain the mechanics of this mini-game. When you press an arrow the character has to keep sliding in that direction until it bumps into an object (I have four rocks & borders of the map). What I decided to do is make a function for each direction in which I define a translation of a certain distance, and then keep repeating this translation until there is an object right next to the character, at which time it will stop moving. In many programming languages this would be an easy task, but there is a problem in JavaFX: when I execute the translation the code will continue executing, allowing the user to press a button while still moving which should not be allowed: the code has to wait until the movement is complete. Most languages have a delay(x ms) function for this, but JavaFX does not have one.

I was wondering whether anyone knows a solution for this problem. I need to make a delay either in the TranslateTransition, or in a function since I made a while loop that keeps executing the translation but without a delay it executes the transition a million times.

Any help will be greatly appreciated!

Thomas Brouwer


The code (with the loop I talked about as comments since it freezes the program):


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javafx.toneel.pac;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.animation.transition.TranslateTransition;
import javafx.animation.*;

var character: Rectangle;
def scenewidth = 600;
def sceneheight = 600;
var topborder: Rectangle;
var leftborder: Rectangle;
var rightborder: Rectangle;
var bottomborder: Rectangle;
var Moving = false;
var object1: Rectangle;
var object2: Rectangle;
var object3: Rectangle;
var object4: Rectangle;
var minX = scenewidth/12;
var minY = sceneheight/12;
var maxX = 11*scenewidth/12;
var maxY = 11*sceneheight/12;

/**
* @author Thomas
*/

Stage {
title: "Application title"
scene: Scene {
width: scenewidth
height: sceneheight
content: [
//code to make the borders of the map. The map consists of blocks 10x10 and
//another block as borders on each side, so 12 horizontally and 12 vertically
topborder = Rectangle {
width: scenewidth
height: sceneheight/12
x: 0
y: 0
fill: Color.BLUE
}
leftborder = Rectangle {
width: scenewidth/12
height: sceneheight
x: 0
y: 0
fill: Color.BLUE
}
rightborder = Rectangle {
width: scenewidth/12
height: sceneheight
x: 11*scenewidth/12
y: 0
fill: Color.BLUE
}
bottomborder = Rectangle {
width: scenewidth
height: sceneheight/12
x: 0
y: 11*sceneheight/12
fill: Color.BLUE
}
//code to define the character
character = Rectangle {
width: scenewidth/12
height: scenewidth/12
x: 6*scenewidth/12
y: 6*sceneheight/12
fill: Color.BLUE
//make the keyevent input accessible
focusTraversable:true
onKeyPressed: function( e: KeyEvent ) {
//prevent key input while the object is already moving, but this
//does not work either.
if (Moving == false){
if (e.code == KeyCode.VK_LEFT) {
Moving = true;
Movingcheckleft();
}
if (e.code == KeyCode.VK_RIGHT) {
Moving = true;
Movingcheckright();
}
if (e.code == KeyCode.VK_UP) {
Moving = true;
Movingcheckup();
}
if (e.code == KeyCode.VK_DOWN) {
Moving = true;
Movingcheckdown();
}
}
}
}
//code to define the objects
object1 = Rectangle {
width: scenewidth/12
height: sceneheight/12
x: 10*scenewidth/12 //one square from the rightborder
y: sceneheight/12
fill: Color.YELLOW
}
object2 = Rectangle {
width: scenewidth/12
height: sceneheight/12
x: 9*scenewidth/12 //two squares from the rightborder
y: 8*sceneheight/12 //three squares from the bottomborder
fill: Color.YELLOW
}
object3 = Rectangle {
width: scenewidth/12
height: sceneheight/12
x: 6*scenewidth/12 //five squares from the leftborder
y: 7*sceneheight/12 //four squares from the bottomborder
fill: Color.YELLOW
}
object4 = Rectangle {
width: scenewidth/12
height: sceneheight/12
x: 7*scenewidth/12 //four squares from the rightborder
y: 2*sceneheight/12 //two squares from the topborder
fill: Color.YELLOW
}
]
}
}

function Movingcheckleft() {
//move one square to the left
var Transitionleft = TranslateTransition {
node: character
duration: 200ms
byX: (-scenewidth/12)
interpolator: Interpolator.LINEAR
}
//check whether there is an object right of the character. As long as there isn't, the
//character will move 1 square to the right. However, while expressions do not seem
//to work in combination with Transition.play.
//while (((character.x - scenewidth/12) != (object1.x)) and ((character.x - scenewidth/12) != (object2.x)) and ((character.x - scenewidth/12) != (object3.x)) and ((character.x - scenewidth/12) != (object4.x)) and (character.x != minX)){
Transitionleft.play();
//}
Moving = false;
x = 0;
}

function Movingcheckright() {
var Transitionright = TranslateTransition {
node: character
duration: 200ms
byX: (scenewidth/12)
interpolator: Interpolator.LINEAR
}
//while (((character.x + scenewidth/12) != (object1.x)) and ((character.x + scenewidth/12) != (object2.x)) and ((character.x + scenewidth/12) != (object3.x)) and ((character.x + scenewidth/12) != (object4.x)) and (character.x != maxX)){
Transitionright.play();
//}
Moving = false;
}
function Movingcheckup() {
var Transitionup = TranslateTransition {
node: character
duration: 200ms
byY: (-sceneheight/12)
interpolator: Interpolator.LINEAR
}
//while (((character.y - sceneheight/12) != (object1.y)) and ((character.y - sceneheight/12) != (object2.y)) and ((character.y - sceneheight/12) != (object3.y)) and ((character.y - sceneheight/12) != (object4.y)) and (character.y != minY)){
Transitionup.play();
//}
Moving = false;
}
function Movingcheckdown() {
var Transitiondown = TranslateTransition {
node: character
duration: 200ms
byY: (sceneheight/12)
interpolator: Interpolator.LINEAR
}
//while (((character.y + sceneheight/12) != (object1.y)) and ((character.y + sceneheight/12) != (object2.y)) and ((character.y + sceneheight/12) != (object3.y)) and ((character.y + sceneheight/12) != (object4.y)) and (character.y != maxY)){{
Transitiondown.play();
//}
Moving = false;
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Delay program until end TranslateTransition JavaFX
 
Similar Threads
JavaFX "wonders"
JasperReports - Is connection always required?
Jasper Report. Problem in html print preview of graphical element
Creating a custom datasource in Jasper Reports and iReport
Jasper Report