java - 如何定期更改画布对象的颜色?
问题描述
我有一个名为 GameView 的类来制作射击游戏,但我希望在底部绘制大炮的射击类的大炮对象应该在 4 秒后改变它的颜色作为我的游戏项目的一部分,所以我使用 Timer 类来处理它但它不起作用它只会在我移动大炮并在屏幕上重绘大炮时改变颜色......
下面的一些有用的细节和代码
1.Gameview类-->画一个游戏板还包括射手类的大炮对象来画一个射手大炮
射手和大炮-->射手类在游戏中绘制射手,大炮是gameview类使用的对象
paint 是用于在射手类中绘制大炮的标识符的名称
游戏视图类
//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 的编程的新手
解决方案
你几乎明白了——试着更多地从“经过的时间”的角度来思考它。
在 GameView 构造函数中,将其更改为:
...
timer = new Timer();
startTime = (int)SystemClock.elapsedRealtime();
...
在onDraw()
中,将其更改为:
...
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
使用而不是进行时间数学计算也更好ints
,但您可以自己做。
那sleep()
inonDraw()
没有任何用处,所以我把它拿出来了。
推荐阅读
- drupal - Drupal 8 有没有办法在媒体库中显示图像的文件大小?
- php - 在 WordPress 中排队 CDN
- xpath - 如何使用带有重复段过滤器的 XPATH 选择字段?
- javascript - 为什么我不能在 Javascript 中使用 console.log 查看变量的更新计数?
- c - 防止 GCC 合并大括号组中的变量
- r - 如何使用整数列表作为一个整数函数的输入并获得一个列表作为输出?
- c++ - 无法使用 EGL/OpenGLES 上下文创建 OpenCL 上下文共享
- python - 如何增加turtle.tracer() 的延迟?[PYTHON]
- python - Django 我如何编辑评论
- r - 在 r 中编写 for 循环以组合具有匹配名称的列(几乎没有差异)