I have a problem and i can not find any solution to this problem.
I am using an animationTimer to update everything and i try to fix it on 60fps as it should do it by itself. The Problem is that on my first screen it runs on 144 fps and on my second with 60fps. I have a Gsync monitor as my first screen and i think because i force Gsync the animationTimer is fixed to 144fps. Is there any method to prevent the animationTimer go further 60fps?
AnimationTimer timer = new AnimationTimer() {
#Override
public void handle(long now) {
long elapsedNanosPerFrame = 0;
long oldFrameTime = frameTimes[frameTimeIndex] ;
double frameRate = 0;
frameTimes[frameTimeIndex] = now ;
frameTimeIndex = (frameTimeIndex + 1) % frameTimes.length ;
if (frameTimeIndex == 0) {
arrayFilled = true ;
}
if (arrayFilled) {
long elapsedNanos = now - oldFrameTime ;
elapsedNanosPerFrame = elapsedNanos / frameTimes.length ;
while () {
}
frameRate = 1_000_000_000.0 / elapsedNanosPerFrame ;
label.setText(String.format("Current frame rate: %.3f", frameRate));
label.setTextFill(Color.GREEN);
}
update(elapsedNanosPerFrame);
}
};
And yes it is not a visible Label bug or something, the gameplay is faster.
Related
Im new to programming and I'm making a aim trainer game where you hit different circles and have to hit one before it changes to the next circle. I having some trouble though. I can't figure out how to make a hitbox for the circles that change for each circle and how to increase the score.
The code is in open processing in pjs.
class Timer {
int time;
int duration;
Timer() {
duration = 100;
reset();
}
void reset() {
time = millis() + duration;
}
boolean alarm() {
if ( millis() > time ) {
time = millis() + duration;
return true;
}
return false;
}
}
float x,y,s; //size and position of circles
color c; //colour of circles
int score = 0; //score
int miss = 0; //misses
Timer timer = new Timer();
void setup() {
size(700, 500);
timer.duration = 2000; //changes the circle every 2 seconds
missed =+ 1;
newEllipse(); // Make initial circle.
}
void draw() {
background(100);
fill(c);
text("SCORE: " + str(score),620,20);
text("MISSED: " + str(miss),20,20);
ellipse(x, y, s, s);
if( timer.alarm() ){
newEllipse();
}
}
void mousePressed(){
if( overEllipse() ){
score =+1;
newEllipse();
}
}
boolean overEllipse(){
if(mouseX > x && mouseX < x+s && mouseY > y && mouseY < y + s);
}
void newEllipse() {
s = random(5,min(50,50));
x = random(0, width - s);
y = random(0, height - s);
c = color( random(255), random(255), random(255) );
timer.reset();
}
Once I tried your code I realized that you were going for something very simple, so no need for OOP today. I fixed and commented your code so you can tweak it and test stuff:
float s; //size of circles
PVector targetPosition; // position of target
color c; //colour of circles
int score = 0; //score
int miss = 0; //misses
Timer timer = new Timer();
void setup() {
size(700, 500);
timer.duration = 2000; //changes the circle every 2 seconds
newEllipse(); // Make initial circle.
}
void draw() {
background(100);
fill(255); // easier to read if you don't change the color all the time
text("SCORE: " + str(score), 620, 20);
text("MISSED: " + str(miss), 20, 20);
fill(c);
ellipse(targetPosition.x, targetPosition.y, s, s);
if ( timer.alarm() ) {
miss += 1; // added a 'miss' when the user isn't able to click the target in due time
newEllipse();
}
}
// notice that I switched this method for 'mouseClicked' instead of 'mousePressed'
// the difference is that this works on a simple click, and run once per click instead of continuously
void mouseClicked() {
if (overEllipse()) {
score += 1;
newEllipse();
} else { // added a 'miss' when the user clicks at random
miss += 1;
}
}
boolean overEllipse() {
// these 3 lines are giving you feedback so you know how close to the target you were
PVector mousePosition = new PVector(mouseX, mouseY);
println(mousePosition.dist(targetPosition) + " / " + s/2);
// you only need the next line if you don't care about the feedback
return mousePosition.dist(targetPosition) < s/2;
}
void newEllipse() {
s = random(10, 50); // I removed the `min(50,50)` because the minimum number between 50 and 50 is... 50. Maybe you were going for something else, but like this it was not needed
targetPosition = new PVector(random(s/2, width - s/2), random(s/2, height - s/2));
c = color( random(255), random(255), random(255) );
timer.reset();
}
class Timer {
int time;
int duration;
Timer() {
duration = 100;
reset();
}
void reset() {
time = millis() + duration;
}
boolean alarm() {
if ( millis() > time ) {
time = millis() + duration;
return true;
}
return false;
}
}
The main problem here was that the method overEllipse made no sense, so I set it on fire and rewrote it using a PVector instead of straight coordinates. I did it mostly because it's easier than to use math to determine if we're clicking inside the target later (the PVector class will straight up do it for you, it's an awesome tool, take a look at how I tweaked the overEllipse method and how it's easy with this tool!). I also added some feedback as console println so you can know how close you clicked from the target.
I'll be around of there's something you'd like me to explain. Have fun!
Okay, so i've been trying to make smooth jumps in a 2D pure android with no game engine for a week now. I am using ScheduledThreadPoolExecutor right now.
So i have a variable called FPS. This is how many time the character has to go up before reaching 180 Y value;
Note: FPS is not really Frames per Second in the whole game.
When FPS is set to 45, the app never freezes (Unless I repeatly spam the jump button)
But on 90, AT RANDOM OCCASIONS WHEN I PLAY THE GAME the game FREEZES and logcat indicates:
E/WindowManager: Performed 6 layouts in a row. Skipping
It dosent matter what I use (EX: Handler Runnable and Thread), app
Here is the jump code:
public void jump() {
final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
if(!isRunning) {
executor.scheduleWithFixedDelay(new Runnable() {
boolean moveUpBln = true;
int timesLooped = 0;
float FPS = 90; //Never freezes with value of 45 (or under)
float charMovementFPS = 180 / FPS;
#Override
public void run() {
if(moveUpBln) {
isRunning = true;
characterModel.setY(characterModel.getY() - charMovementFPS);
if (timesLooped == FPS || timesLooped > FPS) {
moveUpBln = false;
timesLooped = -1;
}
timesLooped++;
}else{
isRunning = true;
characterModel.setY(characterModel.getY() + charMovementFPS);
if (timesLooped == FPS || timesLooped > FPS) {
executor.shutdownNow();
isRunning = false;
}
timesLooped++;
}
}
}, 0, 3, TimeUnit.MILLISECONDS);
}
}
NOTE: the -1 isn't the problem, this is intentional
What are these layouts the logcat is talking about? Am I putting too much pressure on my Non-Engine game?
I have been having this a problem for a while now and I just can't find a solution on how to fix it.
I have a character animation in my game, which works perfectly fine, except for the fact that, when I exit the app and enter it back without closing it, it simply disappears. By "disappearing", I don't mean it is just invisible, because if it was, the collision detection would still apply, but it doesn't.
I am using a custom animation class which looks like this:
public class Animation {
private Array<TextureRegion> frames;
private float maxFrameTime;
private float currentFrameTime;
private int frameCount;
private int frame;
public Animation(TextureRegion region, int frameCount, float cycleTime){
frames = new Array<TextureRegion>();
int frameWidth = region.getRegionWidth() / frameCount;
for(int i=0; i<=frameCount; i++)
frames.add(new TextureRegion(region, i * frameWidth, 0, frameWidth, region.getRegionHeight()));
this.frameCount = frameCount;
maxFrameTime = cycleTime / frameCount;
frame = 0;
}
public void update(float dt){
currentFrameTime +=dt;
if(currentFrameTime > maxFrameTime) {
frame++;
currentFrameTime = 0;
}
if(frame >= frameCount)
frame = 0;
}
public TextureRegion getFrame(){
return frames.get(frame);
}
}
I have heard about a function called "onPause", but I don't know how to access it. Even if I could, what would I write in there to fix my problem? I am not using GDX's "Screen", but a custom made "State" class.
EDIT: Here is my character movement code:
if(isAlive)
birdAnimation.update(dt);
if (position.y > 0 && isAlive) {
velocity.add(0, GRAVITY * dt);
}
velocity.scl(dt);
if(isAlive) {
position.add(MOVEMENT * dt, velocity.y);
velocity.scl(1 / dt);
}
if (position.y < 0)
position.y = 0;
if(position.y >= 1300 - sprite.getHeight())
position.y = 1300 - sprite.getHeight();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm fairly new to game programming in Java and I was wondering if someone could help me out with my game loop. I'm trying to avoid using premade engine libraries (I want to see if I can do it myself). Currently, my game loop is giving me a really low FPS, sometimes even in the single digits. Would this be a case where I have to use multithreading? Or is there a better fix to this problem? Any help is much appreciated!
Here is the code which runs the game loop. I don't include all of the code (because it would be a lot of files), but I think this might be enough, tell me if you need more. I also include a screenshot of the game that I'm trying to create. Thanks in advance!
package com.Farthorn.game;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferStrategy;
import javax.swing.ImageIcon;
public class Game extends Canvas implements Runnable
{
private static final long serialVersionUID = -4431078110090264106L;
public final static int WIDTH = 640;
public final static int HEIGHT = WIDTH / 12 * 9;
private Thread thread;
private boolean running = false;
private CharacterHandler cHandler;
private ObjectHandler oHandler;
public int change = 0;
private String mapBackgroundTop = "/Users/Ahhblala/Desktop/FarthornPics/background_top.png";
private String mapBackgroundBottom = "/Users/Ahhblala/Desktop/FarthornPics/background_bottom.png";
private Image backgroundTop;
private Image backgroundBottom;
public Game()
{
ImageIcon i = new ImageIcon(mapBackgroundTop);
backgroundTop = i.getImage();
i = new ImageIcon(mapBackgroundBottom);
backgroundBottom = i.getImage();
cHandler = new CharacterHandler();
cHandler.addCharacter(new Player(ID.Player, WIDTH/2-32, HEIGHT/2+64));
cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+180, HEIGHT/2+104, (Player) cHandler.characters.getLast()));
cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+195, HEIGHT/2+104, (Player) cHandler.characters.get(0)));
cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+300, HEIGHT/2+104, (Player) cHandler.characters.get(0)));
cHandler.addCharacter(new PlantEnemy(ID.PlantEnemy, WIDTH+315, HEIGHT/2+104, (Player) cHandler.characters.get(0)));
oHandler = new ObjectHandler();
oHandler.addObject(new HUD(10, 10, cHandler));
this.addKeyListener(new KeyInput(cHandler));
this.addMouseListener(new MouseInput(((Player) cHandler.characters.get(0))));
FarthornWindow window = new FarthornWindow(WIDTH, HEIGHT, this);
}
public synchronized void start()
{
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop()
{
try
{
thread.join();
running = false;
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run()
{
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while (running)
{
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1)
{
tick();
delta--;
}
if (running)
{
render();
}
frames++;
if(System.currentTimeMillis() - timer > 1000)
{
timer += 1000;
System.out.println("FPS " + frames);
frames = 0;
}
}
stop();
}
private void tick()
{
int deltaX = 0;
int afterX = 0;
deltaX = cHandler.characters.get(0).getX();
oHandler.tick();
cHandler.tick();
deltaX = cHandler.characters.get(0).getX();
if (deltaX != afterX)
change = (afterX-deltaX);
else
change = 0;
render();
}
private void render()
{
BufferStrategy bs = this.getBufferStrategy();
if (bs == null)
{
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(backgroundTop, 0+change, 0, null);
g.drawImage(backgroundBottom, 0+change, 364, null);
oHandler.render(g);
cHandler.render(g);
g.dispose();
bs.show();
}
public static int clamp(int var, int min, int max)
{
if (var >= max)
{
return var = max;
}
else if (var <= min){
return var = min;
}
else
return var;
}
}
As you might be able to tell, I am rendering a lot of outside images.
There's probably something inefficient in your rendering causing it to drop below your target of 60hz. That aside, there's definitely a problem in your "catch up" loop:
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1)
{
tick();
delta--;
}
Since your tick() routine also calls render(), tick() is effectively a frame in itself, but you are not incrementing your frame counter. Your tick() should be causing all the parts to update, but not render the image, render should only be called once per frame. You probably want to remove the render() line from your tick() routine.
This is just an exercise in mechanics. I am attempting to create three custom panels that control their own progress bar. It’s part of a time management program I am writing for myself to learn more about Java. The larger program uses dates input by the user to create the framework for the min/max of the progress bar. Both this program and the larger one exhibit the same behavior with multiple bars racing the clock.
The issue I am having is that if I have just one bar everything seems to work just fine, but when I have more than one everything seems to go bust. So I wrote this little program to test some things out. It’s very simple, takes three custom panels, gives them a label and uses a timer event to change the label and the position of the progress bar. My question is If the math lines up (System output shows the calculation) and I’m counting events every second (1000 milliseconds) why is everything beating the count down.
Please forgive my lack of form with my code. I’m more concerned with the logic than the form.
(Most of the below is cut from my larger program, so if you see extraneous bits they really do have a home)
Thank you in advance.
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
public class plaything extends JFrame implements ActionListener
{
myPanel[] mp = new myPanel[3];
JLabel[] jl = new JLabel[3];
short[] tim = new short[3];
short x = 0;
short t = 0; //used to stagger the clocks
short dateSaver; //holds temp dates
public plaything()
{
setSize(400, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 1) );
for(short x = 0; x < 3; x++)
{
mp[x] = new myPanel();
//sets all three bars to different 'times'
dateSaver = (short)(10 + t) ;
tim[x] = dateSaver;
mp[x].setMax( dateSaver );
jl[x] = new JLabel("Expires: " + dateSaver);
this.add(mp[x]);
mp[x].add( jl[x] );
t += 15; // 15 seconds
}
Timer time = new Timer(1000, this);
time.start();
}
public void actionPerformed(ActionEvent e)
{
if ( x < 60 )
{
x++;
}
else
{
x = 1;
}
for(myPanel m : mp)
{
m.tock();
}
for(short x = 0; x < 3; x++ )
{
mp[x].tock();
jl[x].setText( "" + --tim[x] );
}
}
private class myPanel extends JPanel
{
//Fields
private boolean finished = false;
//(x,y) Coords
private short x = 15;
private short y = 50;
//Size and shape
private short width = 200;
private short height = 10;
private short arcSize = 10;
//Bar essentials
private double max; //highest range of bar
private double fill = width; //sets filled in portion
private double tick; //calculates movement per event
private Color urgent = Color.BLUE; // Changes the color depending on the Urgency
//Constructors
public myPanel()
{
this.setBackground( Color.WHITE );
repaint();
}
//Mutators ( tick manipulation )
public void setMax( double maxIn )
{
this.max = maxIn;
System.out.println("Max: " + this.max );
this.tick = (double)width / this.max;
System.out.println("tick: " + this.tick );
}
//Methods
//Tick Manipulation
public void tock()
{
//Ends when zero
if( fill < 1 )
{
fill = width;
finished = true;
tick = 0;
urgent = Color.BLUE;
repaint();
}
else
{
fill -= tick ;
System.out.println("fill is " + fill );
repaint();
}
}
//Paint method
public void paint( Graphics g)
{
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor( urgent );
g2.draw(new RoundRectangle2D.Double(x,y + 40, width, height, arcSize, arcSize) );
g2.fill(new RoundRectangle2D.Double(x,y + 40, fill , height, arcSize, arcSize) );
}
}
public static void main(String[] args)
{
plaything pt = new plaything();
pt.setVisible(true);
}
}
My real only concern is where is my logic flawed concerning the progression of the bars and the labels. I hope to find how to make both reach zero together. (two days of research and work on just the bars alone)
Again thank you for your time.
You're calling tock() twice every iteration of your Timer:
for(myPanel m : mp)
{
m.tock(); // ONCE
}
for(short x = 0; x < 3; x++ )
{
mp[x].tock(); // TWICE
jl[x].setText( "" + --tim[x] );
}
You should remove one call, or the other.