我在做一个杀鸟游戏的项目。每件事情都正常工作,但我希望我的小鸟在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;
}
}发布于 2016-06-17 16:55:37
速度是一段时间后的距离。如果我在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。您将需要鸟类开始位置的x和y位置,以及鸟类所在时间的x和y位置xEnd,yEnd,以及您希望它获取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的时候
如果时间是毫秒或更小,你将需要通过除法将它转换为秒。毫秒time = timeInMillisecond / 1000;
https://stackoverflow.com/questions/37878263
复制相似问题