获取触地得分以移动Rect对象

时间:2016-02-22 20:36:58

标签: android android-studio touch pong

我试图让这个paddle_user在触摸屏幕时垂直移动。但是桨没有移动。我已经仔细检查了onTouch代码,但我仍然没有发现我做错了什么。

    package com.nblsoft.pong;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;



public class PongLogic extends View implements View.OnTouchListener  {

    //set screen constrains in dip
    Configuration configuration = this.getResources().getConfiguration();
    int dpHeight = configuration.screenHeightDp; //The current height of the available screen space, in dp units, corresponding to screen height resource qualifier.
    int dpWidth = configuration.screenWidthDp; //The current width of the available screen space, in dp units, corresponding to screen width resource qualifier.

        //int smallestScreenWidthDp = configuration.smallestScreenWidthDp; //The smallest screen size an application will see in normal operation, corresponding to smallest screen width resource qualifier.


        //DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
        //float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
        //float dpWidth = displayMetrics.widthPixels / displayMetrics.density;

    private int dptopixel(int DESIRED_DP_VALUE){

        final float scale = getResources().getDisplayMetrics().density;

        return (int)((DESIRED_DP_VALUE) * scale + 0.5f);
    }

    private int pixeltodp(int DESIRED_PIXEL_VALUE){

        final float scale = getResources().getDisplayMetrics().density;

        return (int) ((DESIRED_PIXEL_VALUE) - 0.5f / scale);
    }

    //set paddle size, speed, position vector

    int paddle_pos_x  = 4 * (dptopixel(dpWidth)/100);           //3 for 320x480, 10 for 1080x1920 etc.
    int paddle_width  =     (dptopixel(dpWidth)/10);            //
    int paddle_pos_y  =     (dptopixel(dpHeight)/10);           //48 for 320x480, 190 for 1080x1920 etc.
    int paddle_height =     (dptopixel(dpHeight)/100) + 3;      //the paddle is 100% of the total height of phone.

    int user_paddle_pos_x = 4 * (dptopixel(dpWidth)/100) ;
    int user_paddle_pos_y = dptopixel(dpHeight) - ((dptopixel(dpHeight)/10) + (dptopixel(dpHeight)/100) + 3)  ;

    //User Paddle
    public Rect paddle_user = new Rect(user_paddle_pos_x,
                                user_paddle_pos_y,
                                user_paddle_pos_x + paddle_width,
                                user_paddle_pos_y + paddle_height);

    //AI paddle
    Rect paddle_AI = new Rect(paddle_pos_x,
                              paddle_pos_y,
                              paddle_pos_x + paddle_width,
                              paddle_pos_y + paddle_height);


    //set ball position vector, Velocity vector, acceleration

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

        Paint mytext  = new Paint();
        mytext.setColor(Color.WHITE);

        // Draw Middle point
        canvas.drawRect(0, ((dptopixel(dpHeight)) / 2), (dptopixel(dpWidth)), (((dptopixel(dpHeight)) / 2) + 2), mytext);

        canvas.drawRect(paddle_user,mytext);
        canvas.drawRect(paddle_AI, mytext);


    //Practise Methods
        //canvas.drawText(Integer.toString(dptopixel(dpHeight)),300,300,mytext);
        //canvas.drawText(Integer.toString(dptopixel(dpWidth)), 400, 400, mytext);

        //canvas.drawText(Integer.toString(dpHeight),500,500,mytext);
        //canvas.drawText(Integer.toString(dpWidth),600,600,mytext);

        //canvas.drawText("Fuck", 700, 700, mytext);
        //canvas.drawRect(0,0,dptopixel(dpWidth),dptopixel(dpHeight),mytext);
    }

    //Override Touch method

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                paddle_user.offsetTo(10,0);
        }

        return true; //Event Handled
    }



    public PongLogic(Context context) {
        super(context);
        setBackgroundColor(Color.BLACK);            //to set background
        this.setFocusableInTouchMode(true);             //to enable touch mode
        this.setOnTouchListener(this);

    }



}

1 个答案:

答案 0 :(得分:1)

您必须实现OnTouchListener接口并执行此操作.setOnTouchListener(this)。

编辑:

public class CustomClass extends View implements View.OnTouchListener{}

然后在你的构造函数中添加this.setOnTouchListener(this);

EDIT2:

好的,所以我忘了告诉你但是当你做了一些修改例子时你必须调用draw方法并正确地做到这一点你调用invalidate。所以在你的ontouch方法中添加invalidate()。

这是我想要检查的代码(我只是剪切你的代码以便有一个更简单的例子):

public class PongLogic extends View implements View.OnTouchListener {



//set ball position vector, Velocity vector, acceleration
   Rect paddle_user = new Rect(0, 100, 100, 200);

public PongLogic(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setOnTouchListener(this);
    setBackgroundColor(Color.BLACK);            //to set background
    this.setFocusableInTouchMode(true);
}

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

    Paint mytext  = new Paint();
    mytext.setColor(Color.WHITE);
    mytext.setStyle(Paint.Style.STROKE);
    mytext.setStrokeWidth(2);

    canvas.drawRect(paddle_user, mytext);

}

@Override
public boolean onTouch(View v, MotionEvent event) {
    paddle_user.offsetTo(0,0);
    invalidate();
    return false;
}
}

所以你错过的基本上是invalidate()方法。所以你在乞讨中所做的并不是第一个OnTouch方法没有错,但如果你做一个自定义视图,那么最好用接口做。