Can anyone assist me on a countdown timer for fengGUI i've tried calling thread.sleep(); and attempted to do the millisecond thing but neither worked out properly. I can get it easly to display in the console but if i try and change the integer to string to be displayed in the label in a container it waits until the loop is broken and then shows the final integer number in the label only.
Can you provide a snippet of code which shows your counter logic?
Do the counting down in a separate Thread, or check how much time has passed every update and increment a variable to determine the time passed.
skyuzo said:
Do the counting down in a separate Thread, or check how much time has passed every update and increment a variable to determine the time passed.
Yes, if you tie up the game thread in a loop, then it won't update and render until that loop is completed. So do intensive work in another thread, or have it based on the update time as skyuzo said.
thx for the quick replies
I had tried using the Timer class in FengGUI but I was never able to implement it properly i then tried using this but i wasn’t able to attach it to the label. I then tried a more basic one but I’ve still been unable to get it working properly i know I’m still a newb but does anyone have any suggestions
Your links are broken.
Your last code segment, the loop, where do you call this?
their just different methods i tried to implement in a mock version of a GUI
......
private MenuItem endProgram(final Display d, final String s){
MenuItem endProgram = new MenuItem(s);
//initializes End Program Window;
endProgram.addMenuItemPressedListener(new IMenuItemPressedListener(){
public void menuItemPressed(MenuItemPressedEvent menuItemPressedEvent)
{
//remember to change how this exits when implimented
try {
gameWindow(d, s);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
return endProgram;
}
private Window gameWindow(Display d, String s)throws java.lang.InterruptedException {
//Sets up the Window
Window mainMenu = FengGUI.createWindow(disp, false, false, false, true);
mainMenu.removeAllWidgets();
mainMenu.addWidget(mainMenu.getTitleBar());
mainMenu.setTitle(s);
mainMenu.setPosition(new Point(disp.getWidth()/2,disp.getHeight()/2));
mainMenu.setMovable(false);
mainMenu.setResizable(false);
mainMenu.setLayoutManager(new RowLayout(false));
//Sets up the exit Label
Label endProgramLabel = FengGUI.createLabel("Program will end in ");
//initialize count down
//TODO
Trying to initialize the count down timer
//Attach the Label to the window
mainMenu.addWidget(endProgramLabel);
//mainMenu.addWidget(second);
mainMenu.pack();
// Update the display with the newly added components
disp.pack();
return mainMenu;
}
Let me rough something out for you. Your problem is that you are preventing the game thread from continuing until after your loop. So when you call thread.sleep you are sleeping the whole game.
Lets see if I can help you with some pseudo code:
class CountDown extends Controller {
private float elapsedTime = 0.0f;
private float totalTime;
private Container fengContainer;
private Widget lastLabel = null;
public CountDown(float totalTime, Container fengContainer) {
this.totalTime = totalTime;
this.fengContainer = fengContainer;
}
public void update(float tpf) {
elapsedTime += tpf;
float countDownValue = totalTime - elapsedTime;
if(countDownValue < 0.0f)
countDownValue = 0.0f;
// Ideally you would update update the text of the label instead of creating a new one as done below.
// Without code in front of me I couldn't do it for you, but I know you can.
if(lastLabel != null)
fengContainer.removeWidget(lastLabel);
lastLabel = FengGUI.createLabel(Integer.toString((int)countDownValue);
fengContainer.addWidget(lastLabel);
if(countDownValue == 0.0f)
setActive(false);
}
}
Attach this to some node or your root node. Every update cycle this will get called and it will change the label. This should be enough to get you started, if you get stuck hollar.
I'm not entirely sure how you can integrate this with your current setup, but maybe it will help you understand what is happening.
thx I was able to get a proper idea from your code only thing is thier are some layout problems that doesn't seem to be solved no matter where i put layout but the FPS label has the same problem so i'm not completly sure how to solve it heres the code i used regardless
///////////////counter//////////////////
/**
* CountDownLabel used to count down from a certain numer to 0 at a specified dely rate
* CountDownLabel(IContainer parent, String wru,int count, int delay)
*/
public class CountDownLabel extends Label implements ILabel
{
private int ccount = 0;// number recieved from user
private int cdown = 0; //number shown in label
private int cdelay = 0;// countdown delay best between 1-5
private String ig=""; //What its counting down for
public CountDownLabel(IContainer parent, String wru,int count, int delay) {
parent.addWidget(this);
parent.updateMinSize();
parent.layout();
ig=wru;
ccount= count+1;
cdelay=delay*100;
cdown = count*cdelay;
}
@Override
public void paint(Graphics g)
{
if (cdown >= 1) {
setText(ig+cdown/cdelay+" seconds Left");setSizeToMinSize();
layout();
if (cdown == 1){setText("Terminating..."); }
cdown--;
setSizeToMinSize();
layout();
}
super.paint(g);
}
}