如何定期更改Canvas对象的颜色?

问题描述 投票:0回答:1

我有一个名为GameView的类,用于制作射击游戏,但我希望射击类的炮对象在底部绘制一个大炮,作为游戏项目的一部分,它应在4秒后更改其颜色,因此我使用Timer类来工作在它上,但是它不起作用,只有当我移动大炮并在屏幕上重画大炮时,它才会改变颜色...

下面一些有用的详细信息和代码

1.Gameview类->绘制游戏板还包括射击类的大炮对象以绘制射击大炮

  1. 射击者和大炮->射击类在游戏中绘制射击者,而大炮是gameview类使用的射击对象

  2. paint是用于在射击类中绘制加农炮的标识符的名称

GAMEVIEW CLASS

//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的编程新手

java android algorithm colors android-canvas
1个回答
0
投票

您几乎已经掌握了它–尝试从“经过时间”的角度考虑它。

© www.soinside.com 2019 - 2024. All rights reserved.