Dear All,
Please help me with this, I am going to loose my mind.
This application is about displaying white bars on black background moving from left to right,
my aim is to let these bars cross the screen in a time margin at speeds of 5 seconds and 1 Second, keeping the distances equal between each bar and the other.
here is my final code
package swings;
import java.util.ArrayList;
import com.jme.app.SimpleGame;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial.CullHint;
import com.jme.scene.shape.Quad;
import com.jme.util.Timer;
import com.jmex.scene.TimedLifeController;
public class TestSlidingBars2DModified
extends SimpleGame {
// width in pixels
private int barWidth = 20;
// speed in pixel per second
private float barSpeed = 700;
// Array of Bars to move
private ArrayList<Quad> bars = new ArrayList<Quad> ();
// TimedLifeController TLC = new TimedLifeController(1);
private float tpf = 0;
private int Speed = 4;
@Override
protected void simpleInitGame() {
lightState.detachAll();
rootNode.setRenderQueueMode(Renderer.QUEUE_ORTHO);
com.jme.system.DisplaySystem.getDisplaySystem().getRenderer().clearBuffers();
com.jme.system.DisplaySystem.getDisplaySystem().getRenderer().draw(rootNode);
com.jme.system.DisplaySystem.getDisplaySystem().getRenderer().displayBackBuffer();
// create a few bars and attach it to the scene
for (int i = 1, xLoc = 0; xLoc < display.getWidth(); i++) {
xLoc = barWidth * 2 * i;
System.out.println(xLoc);
Quad bar = new Quad("q" + i, barWidth, display.getHeight());
// move the bar to the far left of the screen
bar.setLocalTranslation(barWidth / 2 + xLoc, display.getHeight() / 2, 0);
bar.setCullHint(CullHint.Never);
// add the bar to the array of bars, to move them later
bars.add(bar);
// attach the bar to the screen
rootNode.attachChild(bar);
}
}
/**
* Move the Bars to the right.
*/
@Override
protected void simpleUpdate() {
tpf = Timer.getTimer().getTimePerFrame();
// System.out.println("Timer per frame is " + tpf);
// if (tpf > 0.01f) {
// tpf = 0.01f;
// }
// move all bars
for (Quad bar : bars) {
System.out.println("it is now safely at " + bar.getLocalTranslation().x);
// if (bar.getLocalTranslation().x - barWidth >= display.getWidth() ) {
if (bar.getLocalTranslation().x >= display.getWidth())
{
// move the bar back to the left side of the screen
bar.getLocalTranslation().x = -barWidth/2;
// bar.getLocalTranslation().x = barWidth;
}
// move the bar to the right, independent of the current framerate
// bar.getLocalTranslation().x += barSpeed*tpf;
bar.getLocalTranslation().x += (display.getWidth() / Speed) * tpf;
}
}
public static void main(String[] args) {
TestSlidingBars2DModified game = new TestSlidingBars2DModified();
game.setConfigShowMode(ConfigShowMode.AlwaysShow);
com.jme.system.DisplaySystem.getDisplaySystem().setVSyncEnabled(true);
game.getNewSettings().setVerticalSync(true);
game.start();
}
}