English 中文(简体)
三、方案启动时的三角洲问题
原标题:DeltaTime issue at program start

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.

最佳回答

您需要在休息时间开始之前启动<代码>。 您没有说明它是如何启动的,但我的猜测是从0开始的,因此,关于第一个选择,deltatime, 正在被设定为目前时间的<代码>System.nanotime()。

P.S. 是否有理由在另一个地方使用System.nanotime()和System. CurrenttimeMillis(? 此外,还考虑在微秒(或纳米秒)中采用<条码>长期的数值,并取消浮动点计算。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...