【发布时间】:2020-01-21 20:32:36
【问题描述】:
我有一个名为 GameView 的类来制作射击游戏,但我希望射击类的大炮对象在底部画一门大炮,作为我的游戏项目的一部分,应该在 4 秒后改变它的颜色,所以我使用 Timer 类处理它,但它不起作用它只会在我移动大炮并在屏幕上重绘大炮时改变颜色......
一些有用的细节和代码如下
1.Gameview 类--> 绘制游戏板还包括射手类的大炮对象来绘制射手大炮
射手和大炮-->射手类在游戏中绘制射手,大炮是gameview类使用的对象
paint 是射击类中用于绘制大炮的标识符的名称
GAMEVIEW 课程
//Package and Import
public class GameView extends FrameLayout implements View.OnTouchListener{
//some code to declare variables used
shooter cannon;
Timer timer;
int time ,startTime=6000;//6000 milisec
@SuppressLint("ClickableViewAccessibility")
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
timer = new Timer();
time = (int)(SystemClock.elapsedRealtime()*startTime);
cannon = new shooter(Color.BLUE, mContext);
addView(cannon);
cannon.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
//Some code to call cannon.move(); function onTouch and some other code
return true;
}
@Override
protected void onDraw (final Canvas canvas){
super.onDraw(canvas);
time = (int)(SystemClock.elapsedRealtime()*startTime);
drawGameBoard(canvas);
try {
Thread.sleep(1);
} catch (InterruptedException ignored) {
}
// what sort algorithm I have to use to change its color after every 6000 milisec (please be little bit detailed)
if(time%6000==0) {
if(cannon.paint.getColor()==Color.RED) {
cannon.paint.setColor(Color.BLUE);
}
else { cannon.paint.setColor(Color.RED);
}
}
invalidate();
}
@Override
protected void onSizeChanged ( int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
width = w;height = h;
aliens.setBounds(0, 0, width, height);
for (int i = 0; i < bullets.size(); i++) {
bullets.get(i).setBounds(0, 0, width, height);
}
}
public void drawGameBoard (Canvas canvas){
cannon.draw(canvas);
for (int i = bullets.size() - 1; i >= 0; i--) {
if (bullets.get(i) != null) {
bullets.get(i).draw(canvas);
if (bullets.get(i).move()) {
continue;
}
bullets.remove(i);
}
}
for (int i = explosions.size() - 1; i >= 0; i--) {
if (explosions.get(i) != null) {
if (!explosions.get(i).draw(canvas)) {
explosions.remove(i);
}
}
}
if (aliens != null) {
aliens.draw(canvas);
RectF guyRect = aliens.getRect();
for (int i = bullets.size() - 1; i >= 0; i--) {
if (RectF.intersects(guyRect, bullets.get(i).getRect())) {
explosions.add(new explosion(Color.RED, mContext, aliens.getX() , aliens.getY()));
score+=10;
aliens.reset();
bullets.remove(i);
break;
}
}
if (aliens.move()) {
return;
}
aliens = null;
}
}
// Whenever the user shoots a bullet, create a new bullet moving upwards
public void shootCannon () {
//some code to shootCannon (StackOverflow)
}
}
这里的射手类(大炮是这个类的对象)可能在任何方面都有用
//package and Imports
public class shooter extends View {
public Paint paint;
Point center;
int left,right,cW,cH,i=0,shootPoint,top;
public shooter(int color, Context c) {
super(c);
paint = new Paint();
paint.setColor(color);
}
public void move() {
if(left==0)
{
left=center.x;
right=cW;
shootPoint=left+(left/2);
}
else
{
left=0;
right=center.x;
shootPoint=right/2;
}
invalidate();
}
public float getPosition()
{
return shootPoint;
}
public int shooterY(){ return (int)top;}
public void draw(Canvas canvas)
{
super.draw(canvas);
//some code here to initiate left,top,right
canvas.drawCircle(shootPoint,top+5,cW/4,paint);
canvas.drawRect(left,top,right,bottom,paint);
}
}
如果还有不明白的地方请告诉我
尽管说不,但请尽量详细回答,因为我是基于 GUI 的编程的新手
【问题讨论】:
-
只允许在 UI 线程上调用
invalidate()(在大多数情况下,调用setColor())。当您使用Timer时,您没有使用 UI 线程;您正在使用后台线程。此外,您正在创建大量TimerTasks(每次调用onDraw()一个);那只会弄得一团糟。我建议您在游戏开始时花点时间破解 (startTime = SystemClock.uptimeMillis()),然后定期在onDraw()内调用它,以确定 4 秒何时过去。 -
打算尝试 SystemClock.elapsedRealtime() 但它实际上只有在我移动大炮并在不同坐标处重新绘制时才有效。请让它更详细一点......我没有完全理解你......
-
其余的都做对了;你打电话给
setColor(),然后打电话给invalidate(),然后砰——你的大炮就是新颜色(从下一次计划的屏幕刷新开始)。这里的所有都是它的。不要使用Timer,并注意在 UI 线程上执行你的逻辑。使用Log.d()和 logcat 确保您的逻辑每 4 秒只执行一次。 -
请不要介意,但我仍然很困惑,因为它无论如何都不会定期工作,这意味着每 4 秒一次。虽然如果这是你的真名,谢谢greeble..
-
OK 编辑您的问题以包含您的新代码并告诉我,我会看看它。
标签: java android algorithm colors android-canvas