我正在做一个关于鸟类杀戮游戏的项目。每件事情都很好,但我希望我的小鸟在任何给定的手机屏幕上以 35秒的方式穿过屏幕。在每 20秒之后,它的时间减少到31.在35秒内穿过屏幕的数学公式(速度)是多少?目前我正在更新更新方法中的x轴值并为鸟精灵创建矩形。
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.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;
}
}
答案 0 :(得分:0)
速度是一段时间的距离。如果我在2秒钟内移动10米,那么我将以每秒2米的速度移动10米,即每秒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
。您将需要鸟类开始的x
和y
位置,以及鸟类在当时所在位置的x和y位置xEnd
,yEnd
以及你想要它的时间time
一开始你需要设置你需要的变量。我不做java(yuck)因此不记得该类获得系统时间。只需在参考文档中查找。
// 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
如果时间以毫秒或更短为单位,您需要通过除法将其转换为秒。即毫秒time = timeInMillisecond / 1000;