如何定期更改 Canvas 对象的颜色?
How to change the color of a Canvas Object Periodically?
我有一个名为 GameView 的 class 用于制作射击游戏,但我希望射击游戏 class 的大炮对象在底部绘制一门大炮,应该在 4 秒后改变其颜色作为一部分我的游戏项目,所以我使用 Timer class 来处理它,但它不起作用它只在我移动大炮并在屏幕上重绘大炮时改变颜色...
一些有用的细节和下面的代码
1.Gameview Class--> 绘制游戏板还包括射手的大炮对象 class 绘制射手大炮
shooter and cannon--> shooter class在游戏中绘制一个shooter,cannon是gameview使用的对象class
paint 是射击游戏中用来给大炮上色的标识符的名称class
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 (Whosebug)
}
}
这是射手Class(大炮是这个class的对象)可能在任何方面都有用
//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 的编程的新手
您几乎已经掌握了 - 尝试更多地从 "elapsed time" 的角度来考虑它。
在 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
来计算时间,但您可以自己计算。
还有onDraw()
里面的那个sleep()
没有任何用处,所以我把它拿出来了。
我有一个名为 GameView 的 class 用于制作射击游戏,但我希望射击游戏 class 的大炮对象在底部绘制一门大炮,应该在 4 秒后改变其颜色作为一部分我的游戏项目,所以我使用 Timer class 来处理它,但它不起作用它只在我移动大炮并在屏幕上重绘大炮时改变颜色...
一些有用的细节和下面的代码
1.Gameview Class--> 绘制游戏板还包括射手的大炮对象 class 绘制射手大炮
shooter and cannon--> shooter class在游戏中绘制一个shooter,cannon是gameview使用的对象class
paint 是射击游戏中用来给大炮上色的标识符的名称class
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 (Whosebug)
}
}
这是射手Class(大炮是这个class的对象)可能在任何方面都有用
//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 的编程的新手
您几乎已经掌握了 - 尝试更多地从 "elapsed time" 的角度来考虑它。
在 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
来计算时间,但您可以自己计算。
还有onDraw()
里面的那个sleep()
没有任何用处,所以我把它拿出来了。