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;