canvas教程

《飞机大战》安卓游戏开发源码(二)(2)

字号+ 作者:H5之家 来源:H5之家 2018-02-09 14:05 我要评论( )

private ImageButton planeImg;// 飞机的图片 private int planeHalfWidth;//飞机宽度一半,美化射击效果 /** * 构造方法 * * @param context */ public PlaneRunnable(Context context, SurfaceHolder holder) { s

private ImageButton planeImg;// 飞机的图片

private int planeHalfWidth;//飞机宽度一半,美化射击效果
/**
* 构造方法
*
* @param context
*/
public PlaneRunnable(Context context, SurfaceHolder holder) {
super();
this.context = context;
this.holder = holder;
}

@Override
public void run() {


Canvas canvas = null;
while (plane.isShot()) {

canvas = holder.lockCanvas();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);

Paint paint = new Paint();
paint.setColor(Color.BLUE);
Log.i(CommonUtil.THREAD_TAG, plane.getX()+”:”+plane.getY());
canvas.drawText(CommonUtil.BULLET, plane.getX() + planeHalfWidth, plane.getY(), paint);

holder.unlockCanvasAndPost(canvas);
//发射一个子弹
shotOneBullet();
Log.i(CommonUtil.THREAD_TAG, “plane射击状态为true”);

try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.i(CommonUtil.THREAD_TAG, “plane停止射击”);
}

/**
* 发射一枚子弹
*/
private void shotOneBullet(){

Bullet bullet = new Bullet();
bullet.setX(plane.getX()+planeHalfWidth);
bullet.setY(plane.getY());
bullet.setFly(true);//飞行中

BulletRunnable bulletRunnable = new BulletRunnable(context,holder);
//子弹装进线程
bulletRunnable.setBullet(bullet);
//子弹开始飞行
new Thread(bulletRunnable).start();
}

public Plane getPlane() {
return plane;
}

public void setPlane(Plane plane) {
this.plane = plane;
}

public ImageButton getPlaneImg() {
return planeImg;
}

public void setPlaneImg(ImageButton planeImg) {
this.planeImg = planeImg;
planeHalfWidth = planeImg.getWidth() / 2;
}

}

下面是BulletRunnable 的代码:,为了美化一个效果,所以当子弹没有碰到任何东西,飞出屏幕边界的时候,在屏幕边缘要进行爆炸,爆炸线程:BoomRunnable

package com.pm_road.fireair.thread;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.util.Log;
import android.view.SurfaceHolder;

import com.pm_road.fireair.bean.Bullet;
import com.pm_road.fireair.common.CommonUtil;

/**
* 子弹线程
*
* @ClassName: BulletRunnable
* @Description: 子弹线程
* @author lipeng
* @date 2014-11-5 上午10:33:19
*
*/
public class BulletRunnable implements Runnable {

private Bullet bullet;//当前的子弹

private Context context;//上下文

private SurfaceHolder holder;

public BulletRunnable(Context context,SurfaceHolder holder) {
super();
this.context = context;
this.holder = holder;
}

@Override
public void run() {

Canvas canvas = null;
while(bullet.isFly()){

canvas = holder.lockCanvas();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);

Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawText(CommonUtil.BULLET, bullet.getX(), bullet.getY()-70, paint);

holder.unlockCanvasAndPost(canvas);

try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
bullet.setY(bullet.getY() -70);
if(bullet.getY() <= 0){
//如果当前线程的Y超过了屏幕,则将该线程回收
bullet.setFly(false);
//爆炸效果
boom();
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(CommonUtil.THREAD_TAG, “子弹飞行中”);
}
Log.i(CommonUtil.THREAD_TAG, “子弹线程结束”);
}

/**
* 爆炸效果
*/
private void boom(){

//声明爆炸效果
BoomRunnable boomRunnable = new BoomRunnable(context, holder);
boomRunnable.setBullet(bullet);
//爆炸
new Thread(boomRunnable).start();
}

public Bullet getBullet() {
return bullet;
}

public void setBullet(Bullet bullet) {
this.bullet = bullet;
}

}

以下是爆炸线程:BoomRunnable,该爆炸线程暂时没有调用图片,只是用几根线勾勒出一个简单效果 ,后期继续增加:

package com.pm_road.fireair.thread;

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评