Java 如何定期更改画布对象的颜色?
我有一个名为GameView的类,用于制作射击游戏,但我希望射击类的cannon对象在底部绘制大炮,在4秒后作为我游戏项目的一部分更改其颜色,所以我使用Timer类来处理它,但它不起作用。它只在我移动大炮并在屏幕上重新绘制大炮时更改颜色 下面是一些有用的细节和代码 1.Gameview类-->绘制游戏板还包括shooter类的cannon对象以绘制shooter cannonJava 如何定期更改画布对象的颜色?,java,android,algorithm,colors,android-canvas,Java,Android,Algorithm,Colors,Android Canvas,我有一个名为GameView的类,用于制作射击游戏,但我希望射击类的cannon对象在底部绘制大炮,在4秒后作为我游戏项目的一部分更改其颜色,所以我使用Timer类来处理它,但它不起作用。它只在我移动大炮并在屏幕上重新绘制大炮时更改颜色 下面是一些有用的细节和代码 1.Gameview类-->绘制游戏板还包括shooter类的cannon对象以绘制shooter cannon 射手和大炮-->射手类在游戏中绘制射手,大炮是gameview类使用的射击对象 paint是用于在shooter类中绘制
//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)
}
}
如果还有什么不明白的,请告诉我
尽管可以说不,但请尝试详细回答,因为我对基于GUI的编程还不熟悉您几乎已经了解了这一点——试着用“经过的时间”来更多地思考它
在GameView构造函数中,将其更改为:
...
timer = new Timer();
startTime = (int)SystemClock.elapsedRealtime();
...
...
super.onDraw(canvas);
int elapsedTime = (int)(SystemClock.elapsedRealtime() - startTime);
//elapsedTime is now the number of milliseconds that have elapsed since startTime.
drawGameBoard(canvas);
if((elapsedTime/6000)%2==0) { //True for 0-5999, false for 6000-11999, true for 12000-17999, etc.
if(cannon.paint.getColor()!=Color.BLUE) {
cannon.paint.setColor(Color.BLUE);
cannon.invalidate();
}
}
else {
if(cannon.paint.getColor()!=Color.RED) {
cannon.paint.setColor(Color.RED);
cannon.invalidate();
}
}
invalidate(); //prompt a re-draw of this GameView (gameboard)
...
在onDraw()
中,将其更改为:
...
timer = new Timer();
startTime = (int)SystemClock.elapsedRealtime();
...
...
super.onDraw(canvas);
int elapsedTime = (int)(SystemClock.elapsedRealtime() - startTime);
//elapsedTime is now the number of milliseconds that have elapsed since startTime.
drawGameBoard(canvas);
if((elapsedTime/6000)%2==0) { //True for 0-5999, false for 6000-11999, true for 12000-17999, etc.
if(cannon.paint.getColor()!=Color.BLUE) {
cannon.paint.setColor(Color.BLUE);
cannon.invalidate();
}
}
else {
if(cannon.paint.getColor()!=Color.RED) {
cannon.paint.setColor(Color.RED);
cannon.invalidate();
}
}
invalidate(); //prompt a re-draw of this GameView (gameboard)
...
使用longs
而不是int
来计算时间也更好,但你可以自己做
而
onDraw()
中的sleep()
没有任何用处,所以我把它取了出来。调用invalidate()
(在大多数情况下,调用setColor()
)只允许在UI线程上进行。当您使用计时器时,您没有使用UI线程;您正在使用背景线程。此外,您正在创建大量的timertask
(每次调用onDraw()
)一个;那只会弄得一团糟。我建议您在游戏开始时进行一次时间破解(startTime=SystemClock.uptimeMillis()
),然后定期在onDraw()
中调用它,以确定4秒的时间。要尝试SystemClock.elapsedRealtime()但它实际上只有在我移动大炮并在不同的坐标下重新绘制时才起作用。请让它更详细一点。。。我没能完全理解你..你把剩下的都做好了;你调用setColor()
,然后调用invalidate()
,然后砰的一声-你的大炮就是新的颜色(从下一次预定屏幕刷新开始)。就这些。不要使用计时器
,注意在UI线程上执行逻辑。使用Log.d()
和logcat确保您的逻辑每4秒只执行一次。请不要介意,但我仍然感到困惑,因为它没有周期性地工作,也就是每4秒执行一次。虽然如果这是你的真名,谢谢greeble。好的,编辑你的问题,包括你的新代码,让我知道,我会看一看。Hii@greeble31我再次陷入了一个问题,因为我对Android的经验不足。如果可以的话,这里是所有的细节,因为仍然没有答案。。