Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Gdx.graphics.getDeltaTime(); Returns 0

$
0
0

I am coding a game in Java using Eclipse and Libgdx.

I am trying to make an object move until it reaches a certain random point. I am relying on delta time to make the objects speed the same on every device. But Gdx.graphics.getDeltaTime() is returning 0; so my game gets stuck in an infinite loop and the screen goes black. Gdx.graphics.getDeltaTime() returns a number greater than 0 in another one of my object classes.

package com.JrodManU.LaserJumper;

import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;

public class Laser {
    public Sprite image;
    public Rectangle bounds;
    public LaserBeam laserBeam;
    public float targetY;
    Random random;

    public Laser(int x) {
        image = Assets.laser;
        random = new Random();
        bounds = new Rectangle(x, random.nextFloat() * (892 - 32) + 32, 32, 16);
        laserBeam = new LaserBeam();
    }

    public void fire() {
        targetY = random.nextFloat() * (892 - 32) + 32;
        if(bounds.y >= targetY) {
            while(bounds.y >= targetY) {
                bounds.y -= 6f * Gdx.graphics.getDeltaTime();
                System.out.println(bounds.y);
                System.out.println(Gdx.graphics.getDeltaTime());
            }
            bounds.y = targetY;
        } else {
            while(bounds.y < targetY) {
                bounds.y += 6f * Gdx.graphics.getDeltaTime();
                System.out.println(bounds.y);
                System.out.println(Gdx.graphics.getDeltaTime());
            }
            bounds.y = targetY;
        }
    }
}

Viewing all articles
Browse latest Browse all 434

Trending Articles