Advertisement

The oClock don't go in programming my game in java

Started by November 28, 2020 02:36 AM
1 comment, last by ddlox 4 years ago

I am making one game in Java with Eclipse like HaverstMoon

and the time running is soo important, to this i make the class Time:

package com.ecojogos.world;

import java.awt.BorderLayout;

import java.awt.Font;

import javax.swing.JLabel;

import com.ecojogos.main.Game;

public class Tempo extends Game{

public int frames;

public static int seconds;

public static int minutes;

public static int hours;

public static int days;

public static int estations;

public static JLabel contagemTempo;

public void tick(){

frames++;

if(frames == 60){

frames = 0;

seconds++;

}

seconds++;

if(seconds == 60){

seconds = 0;

minutos++;

}

minutes++;

if(minutes == 60) {

minutes = 0;

hours++;

}

hours++;

if (hours == 24) {

hours = 0;

days++;

}

days++;

if (days == 30) {

days = 0;

estations++;

}

estations ++;

if(estations == 4){

estations = 0;

}

contingTime = new JLabel ("00:00:00");

contingTime.setFont(new Font(contingTime.getName(), Font.PLAIN, 80));

frame.add(contingTime, BorderLayout.CENTER);

}

public void render() {

contingTime.setText(String.format("%02d:%02d:%02d",seconds, minutes, hours));

}

but when i render the CountingTime in the class Game to appear in the Screen of game, the counting don't count, stay in 00:00:00

class game:

in Game render () {

g.drawString(String.format("%02d:%02d:%02d",Tempo.segundos, Tempo.minutos, Tempo.horas), 180, 180);

}

move this out of the Tick( ) into your initialisation function somewhere:

//pseudo
public void init()
{
....
	contingTime = new JLabel ("00:00:00");
	contingTime.setFont(new Font(contingTime.getName(), Font.PLAIN, 80));
	frame.add(contingTime, BorderLayout.CENTER);
...
}
// also use the label in your render function
in Game render()
{
...drawString(contingTime.Format....
}

and make sure u call init() somewhere…

have fun ?

This topic is closed to new replies.

Advertisement