如何使物体在触摸时漂浮?

问题描述

我正在尝试在 android 中制作这个游戏,其中警察应该没有重力并因此坠落,直到用户每次点击屏幕时它都会向上移动,类似于飞扬的小鸟。

这是警察如何静止在底部并且触摸屏幕没有任何反应的屏幕截图。

click on me to find the screenshot of the gamescreen

这是相同的代码: 这是我的看法

public class PoliceView extends View {

private Bitmap police[] = new Bitmap[2];
private int policeX = 10;
private int policeY;
private int policeSpeed;

private int canvasWidth,canvasHeight;

private boolean touch = false;


private Bitmap background;
private Paint scorePaint = new Paint();
private Bitmap life[] = new Bitmap[2];

public PoliceView(Context context) {
    super(context);

    police[0] = BitmapFactory.decodeResource(getResources(),R.drawable.police);
    police[1] = BitmapFactory.decodeResource(getResources(),R.drawable.police);
    background = BitmapFactory.decodeResource(getResources(),R.drawable.gamebackground);


    scorePaint.setColor(Color.BLACK);
    scorePaint.setTextSize(70);
    scorePaint.setTypeface(Typeface.DEFAULT_BOLD);
    scorePaint.setAntiAlias(true);

    life[0] = BitmapFactory.decodeResource(getResources(),R.drawable.heart);
    life[1] = BitmapFactory.decodeResource(getResources(),R.drawable.brokenheart);

    policeY = 550;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvasWidth = canvas.getWidth();
    canvasHeight = canvas.getHeight();

    //The order of draw matters as objects are drawn in this same order
    canvas.drawBitmap(background,-90,null);

    int minPoliceY = police[0].getHeight();
    int maxPoliceY = canvasHeight - police[0].getHeight() * 3;
    policeY = policeY + policeSpeed;

    if(policeY < minPoliceY){
        policeY = minPoliceY;
    }

    if(policeY > minPoliceY){
        policeY = maxPoliceY;
    }

    policeSpeed = policeSpeed + 2;

    if(touch){

        canvas.drawBitmap(police[1],policeX,policeY,null);
        touch = false;
    }
    else{
        canvas.drawBitmap(police[0],null);

    }

    canvas.drawText("score:",40,80,scorePaint);
    //Three lives for the police
    canvas.drawBitmap(life[0],100,null);
    canvas.drawBitmap(life[0],160,280,null);



@Override
public boolean onTouchEvent(MotionEvent event)
{
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        touch = true;

        policeSpeed = -22;
    }
    return true;
}}

这是我主要活动的代码

public class GameScreen extends AppCompatActivity {

private PoliceView gameView;
private Handler handler = new Handler();
private final static long interval = 30;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    gameView = new PoliceView(this);
    setContentView(gameView);

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    gameView.invalidate();
                }
            });
        }
    },interval);
}}

我基本上需要帮助,让警察在每次用户点击屏幕时向上移动,如果他不点击类似于飞扬的小鸟,就会向下坠落。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)