i) 三角洲 时间使用。 我的法典如下:
public class Time {
public static float deltaTime = 0;
public static long frameCount = 0;
public static float fixedTime = 0;
public static float fixedDeltaTime = 0.1f;
public static float maxDeltaTime = 0.25f;
iii
而现在,我主修(主修)的功用是:
while (running) {
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
float newTime = System.nanoTime() / 1000000000.0f;
Time.deltaTime = frameTime = newTime - currentTime;
if(frameTime > Time.maxDeltaTime)
frameTime = Time.maxDeltaTime;
currentTime = newTime;
accumulator += frameTime;
while(accumulator > Time.fixedDeltaTime)
{
this.gamePanel.update(); // where the player and my enemy gets updated
accumulator -= Time.fixedDeltaTime;
iii
this.gamePanel.render(canvas);
//Perform all non-physics-related updates here
++Time.frameCount;
framesSkipped = 0; // resetting the frames skipped
timeDiff = System.currentTimeMillis() - beginTime;
// calculate sleep time
sleepTime = (int)(FRAME_PERIOD - timeDiff);
if (sleepTime > 0) {
// if sleepTime > 0 we re OK
try {
Thread.sleep(sleepTime);
iii catch (InterruptedException e) {iii
iii
while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
// we need to catch up
this.gamePanel.update(); // update without rendering
sleepTime += FRAME_PERIOD; // add frame period to check if in next frame
framesSkipped++;
iii
if (framesSkipped > 0) {
Log.d(TAG, "Skipped:" + framesSkipped);
iii
// for statistics
framesSkippedPerStatCycle += framesSkipped;
// calling the routine to store the gathered statistics
storeStats();
iii
iii finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
iii
iii //end finally
iii
iii
in the gamePanel.update() ive got the update calls for the player and my enemy. Now my Problem is, that at the start of my game the deltaTime is extremly high und thus my movement is very fast, because i have the following in my enemy class in the update method:
x += vX * Time.deltaTime; // velocity * deltaTime
y -= vY * Time.deltaTime;
//Log.d(TAG, "Time:"+Time.deltaTime+" x: "+x+" y: "+y);
vY -= gravity;
Am i doing it right, or is something wrong with my structure? Thanks for any help.