我试图每0.5秒使随机对象在屏幕上显示为障碍物。我该怎么做才能做到这一点?
我创建了一个“传入”类,以创建该类的不同对象,并每500毫秒将它们添加到“障碍”中。我向其中添加了draw方法,并尝试为每个对象调用它,以便它们向播放器移动。
进来的班级
package com.example.neonrace;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
class incoming {
int top;
int left;
int right;
int bttm;
Rect carr;
Bitmap incom;
Context context;
public incoming(int top, int left, int right, int bttm, Rect carr)
{
this.top = top;
this.left = left;
this.right = right;
this.bttm = bttm;
this.carr = carr;
this.incom = incom;
this.context = context;
}
public void draw(Canvas canvas){
incom = BitmapFactory.decodeResource(context.getResources(),
R.drawable.inc);
Rect carr = new Rect(this.left, this.top, this.right,
this.bttm);
canvas.drawBitmap(incom, null, carr, null);
if (GameView.r == 0) {
this.top = 0;
this.left = GameView.RoadLeft;
this.right = (GameView.DWidth * 4) / 10;
this.bttm = (int) ((GameView.DHeight * 1.5) / 10);
this.top = this.top + GameView.speed;
this.bttm = this.bttm + GameView.speed;
}
else if (GameView.r == 1) {
this.top = 0;
this.left = GameView.DWidth * 4 / 10;
this.right = GameView.DWidth * 5 / 10;
this.bttm = (int) ((GameView.DHeight * 1.5) / 10);
this.top = this.top + GameView.speed;
this.bttm = this.bttm + GameView.speed;
}
else if (GameView.r == 2){
this.top = 0;
this.left = GameView.DWidth * 5 / 10;
this.right = (GameView.DWidth * 6 / 10);
this.bttm = (int) ((GameView.DHeight * 1.5) / 10);
this.top = this.top + GameView.speed;
this.bttm = this.bttm + GameView.speed;
}
else {
this.top = 0;
this.left = GameView.DWidth * 6 / 10;
this.right = GameView.DWidth * 7 / 10;
this.bttm = (int) ((GameView.DHeight * 1.5) / 10);
this.top = this.top + GameView.speed;
this.bttm = this.bttm + GameView.speed;
}
}
}
主GameView
package com.example.neonrace;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Handler;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class GameView extends View {
public static int r;
public static int RoadLeft;
public static int DWidth;
public static int DHeight;
public static int speed;
Handler handler;
Runnable runnable;
final int UpdateMS = 30;
Bitmap background;
Bitmap car;
Display display;
Point point;
Rect rect;
Rect road;
Rect rcar;
int RoadRight;
int Carleft, Carright, Carup;
Paint paint;
int topRoad = -1000000;
int downRoad ;
int score ;
Random random;
List obstacles = new ArrayList<incoming>();
int NumOfObst;
Timer timer = new Timer();
int i;
Canvas canvas;
public GameView(Context context) {
super(context);
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
invalidate();
}
};
background =
BitmapFactory.decodeResource(getResources(),R.drawable.grass);
car = BitmapFactory.decodeResource(getResources(),
R.drawable.enzo);
display =
((Activity)getContext()).getWindowManager().getDefaultDisplay();
point = new Point();
display.getSize(point);
DWidth = point.x;
DHeight = point.y;
speed = 15;
RoadLeft = point.x * 3 / 10;
RoadRight = point.x * 7 / 10;
Carleft = point.x * 5 / 10;
Carright = point.x * 6 / 10;
Carup = point.y * 7 / 10;
rect = new Rect(0,0,DWidth,DHeight);
paint = new Paint();
paint.setColor(Color.BLACK);
Random random = new Random();
int r = random.nextInt(5);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(background, null, rect, null);
topRoad = topRoad + speed;
downRoad = downRoad + DHeight/2 + speed;
road = new Rect(RoadLeft, topRoad, RoadRight,downRoad);
canvas.drawRect(road, paint);
timer.schedule(new TimerTask() {
@Override
public void run() {
obstacles.add(incoming);
}
},500);
NumOfObst = obstacles.size();
for (i = 0; i <= NumOfObst; i++){
obstacles.get(i).draw(Canvas);
}
rcar = new Rect(Carleft, Carup, Carright, DHeight*9/10);
canvas.drawBitmap(car,null,rcar,null);
handler.postDelayed(runnable,UpdateMS);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int tchx = (int) event.getX();
if (action == MotionEvent.ACTION_DOWN){
if (tchx < DWidth/2){
Carleft = Carleft - (DWidth / 10);
Carright = Carright - (DWidth / 10);
}
else {
Carleft = Carleft + (DWidth / 10);
Carright = Carright + (DWidth / 10);
}
}
return true;
}
}
我希望随机物体会进入玩家的视野,但实际上我得到一个错误,我不知道如何解决。