初学Android之飞机大战(三)——myPlane

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;
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值