有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓在Java中计算仿真时间

我正在做一个关于捕鸟游戏的项目。一切正常,但我希望我的鸟在任何给定的移动屏幕上都能在35秒内穿过屏幕。每过20秒,它的时间就减少到31秒。35秒内通过屏幕的数学公式(速度)是多少?目前,我正在更新更新方法中的x轴值,并为鸟精灵创建矩形

import 安卓.graphics.Bitmap;
import 安卓.graphics.BitmapFactory;
import 安卓.graphics.Canvas;
import 安卓.graphics.Rect;
import 安卓.util.Log;
import 安卓.view.MotionEvent;
import 安卓.view.View;

import java.util.Random;
public class Birds extends GameObject implements View.OnTouchListener{
private Bitmap spritesheet;
private Rect rect;
public boolean firstTym = true;
private Animation animation = new Animation();
private String tag = "";
private int  y,touchX,touchY;
public int x=0;
private long startTime;

public Birds(String tag)
{
    this.tag = tag;
    spritesheet = BitmapFactory.decodeResource(Constants.res, R.drawable.bird_sprites);
    dy = 0;
    if(Constants.Width > 1300) {
        width = 120;
        height = 140;

    }
    else {

        width = 80;
        height = 72;
    }
    Bitmap[] image = new Bitmap[5];


    for (int i = 0; i < image.length; i++)
    {
        image[i] = Bitmap.createBitmap(spritesheet, i*width, 0, width, height);
    }

    animation.setFrames(image);
    animation.setDelay(10);
    startTime = System.nanoTime();
}
public void update()
{
    if(!firstTym) {
// here i am updating speed of bird in x-axis.
//i want bird to cross the screen in 35 seconds
        x += Constants.speed;
        Log.e("speed = ","" + Constants.speed);
    }
    else
    {
        Random r = new Random();
        r.nextInt(Constants.Width);
    }

    if(x > GamePanel.WIDTH){
        Constants.missed ++;
        x = 0;
    }
    if(y > GamePanel.HEIGHT)
    {
        x = 0;
    }
}
public void draw(Canvas canvas) {
    Random r = new Random();
    if (x == 0 || firstTym) {
        y = r.nextInt(GamePanel.HEIGHT - 150);

        Constants.RAND = r.nextInt(1);
        firstTym = false;
    }
    animation.update();

    y += Constants.RAND;
    rect = new Rect(x, y, x + 80, 72 + y);
    setRect(rect);
    setTag(tag);
    canvas.drawBitmap(animation.getImage(), null, rect, null);
    if (x < 0) {
        canvas.drawBitmap(animation.getImage(), null, rect, null);
    }
}
public int getX()
{
    return x;
}
public int getY()
{
    return y;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    touchX = (int)event.getX();
    touchY = (int)event.getY();
    return true;
}
}

共 (1) 个答案

  1. # 1 楼答案

    速度是距离随时间的变化。如果我在2秒内移动10米,那么我以每秒10除以2米的速度移动,也就是每秒5米。我们可以把它写成s = D / t,其中s是速度,D是距离,t是时间。有了它,我们可以安排它,这样我们就可以根据其他两个组件来定义任何一个组件

    因此我们得到

    s = D / t; // get the speed in term of distance and time
    t = D / s; // get the time in term of distance and speed
    D = t * s; // get the distance in term of time and speed
    

    距离D以像素(即宽度)为单位,时间以秒为单位。当你开始鸟的飞行时,你需要获得系统时钟时间startTime,即当前时间timeNow。您将需要鸟开始的xy位置,以及鸟在时间中的位置的x和y位置xEndyEnd,以及您希望它花费的时间time

    一开始你需要设置你需要的变量。我不做java(恶心),所以记不起类来获取系统时间。只需在参考文档中查找即可

    // set up
    time = 35; // how long to cross the screen
    x = 0; // start pos
    y = 100; // 
    xEnd = screenWidth;  // end pos
    yEnd = 100;
    startTime = ?.now() // ? is whatever class you get the system time from 
    

    用这些来确定鸟的位置

    // each frame get the time in seconds
    timeNow = ?;
    if( timeNow - startTime < time) { // make sure time is not over
        currentX = x + ((xEnd - x) / time) * (timeNow - startTime))
        currentY = y + ((yEnd - y) / time) * (timeNow - startTime))
    }else{
       // All done
    }
    

    你知道当timeNow - startTime > time

    如果时间单位为毫秒或更小,则需要通过除以将其转换为秒。Ie的毫秒数time = timeInMillisecond / 1000;