package com.example.a11918.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
/**
* Created by 11918 on 2017/5/25.
*/
/**
* 我的飞机类
*/
public class MyPlane {
private Bitmap bmpPlane;//我的飞机图片
private Bitmap bmpHP;//我的HP图片
private int flow = 3;//我的HP数量
private int planeX;
private int planeY;
private boolean isOnPlane;
public MyPlane(Bitmap bmpPlane,Bitmap bmpHP){
this.bmpPlane = bmpPlane;
this.bmpHP = bmpHP;
/**
* 获取我的飞机的初始位置
*/
planeX = GameSurfaceView.screenWidth / 2-bmpPlane.getWidth()/2;
planeY = GameSurfaceView.screenHight - bmpPlane.getHeight();
}
/**
* 将我的飞机画出来
* @param canvas
* @param paint
*/
public void drawPlane(Canvas canvas, Paint paint){
//裁剪boss
canvas.drawBitmap(bmpPlane,planeX,planeY,paint);
for (int i=0;i<flow;i++){
canvas.drawBitmap(bmpHP, (bmpHP.getWidth() + 10) * i+10, GameSurfaceView.screenHight - bmpHP.getHeight() - 20, paint);
}
}
/**
* 飞机移动的方法
* @param event
*/
public void touch(MotionEvent event){
/**
* 获取鼠标X,Y轴坐标
*/
float touchX = event.getX();
float touchY = event.getY();
/**
* 判断鼠标是否点击在飞机上
*/
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(touchX>planeX&&touchX<planeX+bmpPlane.getWidth()){
if(touchY>planeY&&touchY<planeY+bmpPlane.getHeight()){
isOnPlane = true;
}
}
}
/**
* 限制飞机移动范围
*/
if(event.getAction()==MotionEvent.ACTION_MOVE&&isOnPlane){
if (touchX>bmpPlane.getWidth()/2&&touchX<GameSurfaceView.screenWidth-bmpPlane.getWidth()/2){
if (touchY>bmpPlane.getHeight()/2&&touchY<GameSurfaceView.screenHight-bmpPlane.getHeight()/2) {
planeX = (int) touchX - bmpPlane.getWidth() / 2;
planeY = (int) touchY - bmpPlane.getHeight() / 2;
}
}
}
if (event.getAction()==MotionEvent.ACTION_UP){
isOnPlane = false;
}
}
}
初学Android之飞机大战(三)——myPlane
最新推荐文章于 2024-05-03 13:32:04 发布