Android触控移动球加速

时间:2012-02-07 06:17:55

标签: java android touch

我正在开发一款篮球比赛。我想用触摸事件来投球。我喜欢当玩家接触球时强大的屏幕会快速移动,而如果用户接触球会稍微慢一些。我该怎么办,期待每个人的帮助。这是我的来源。我遇到的问题是当球接触球时不会停止。

public class Graphic_Ball {
float radius = 40;
float x = 100;
float y = 100;
float speedX = 0;
float speedY = 0;
private Paint paint;
Bitmap bmap;
public Graphic_Ball(int color, Bitmap bm) {
    paint = new Paint();
    paint.setColor(color);
    this.bmap = bm;
}
public void moveWithCollisionDetection(Graphic_Boundary box) {
    x += speedX;
    y += speedY;
    if (x + radius > box.xMax) {
        speedX = -speedX;
        x = box.xMax - radius;
    } else if (x - radius < box.xMin) {
        speedX = -speedX;
        x = box.xMin + radius;
    }
    if (y + radius > box.yMax) {
        speedY = -speedY;
        y = box.yMax - radius;
    } else if (y - radius < box.yMin) {
        speedY = -speedY;
        y = box.yMin + radius;
    }
}
public float getX() {
    return x;
}
public void setX(float x) {
    this.x = x;
}
public float getY() {
    return y;
}
public void setY(float y) {
    this.y = y;
}
public void draw(Canvas canvas) {
    canvas.drawBitmap(bmap, x - radius, y - radius, null);
}
public class Graphic_Boundary {
int xMin, xMax, yMin, yMax;
private Paint paint;
private Rect bounds;
public Graphic_Boundary(int color) {
    paint = new Paint();
    paint.setColor(color);
    bounds = new Rect();
}
public void set(int x, int y, int width, int height) {
    xMin = x;
    xMax = x + width - 1;
    yMin = y;
    yMax = y + height - 1;
    bounds.set(xMin, yMin, xMax, yMax);
}

}

public class Graphic_SensorActivity extends Activity implements
    SensorEventListener {
private Graphic_Ball ball;
private Graphic_Boundary box;
Bitmap bmp;
private float previousX;
private float previousY;
private static final float NS2S = 1.0f / 1000000000.0f;
private float timestamp;
float a;
CustomDrawableView mCustomDrawableView = null;
private final Graphic_SensorBall model = new Graphic_SensorBall();
private Paint backgroundPaint;
private Paint ballPaint;
private SensorManager sensorMgr;
Bitmap bm;
int x, y;
float xx, yy;
int p3x = 93;
int p3y = 51;
int p6x = 150;
int p6y = 50;
int p7x = 94;
int p7y = 70;
int p8x = 67;
int p8y = 47;
int p9x = 96;
int p9y = 85;
int p10x = 150;
int p10y = 47;
int p11x = 98;
int p11y = 100;
Path p;
Path p2;
Path p3;
Path p4;
Path p5;
Path p6;
Path p7;
Path p8;
Path p9;
Path p10;
Path p11;
Paint paint;
int i = 5;
Paint paintcol;
float mx1, mx2;
float my1, my2;
public float xPosition, xAcceleration, xVelocity = 0.0f;
public float yPosition, yAcceleration, yVelocity = 0.0f;
public float xmax, ymax, xmin, ymin;
public float dx;
public float dy;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    mCustomDrawableView = new CustomDrawableView(this);
    setContentView(mCustomDrawableView);
    backgroundPaint = new Paint();
    backgroundPaint.setColor(Color.BLACK);
    ballPaint = new Paint();
    ballPaint.setColor(Color.RED);
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setStrokeWidth(3);
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.WHITE);
    paintcol = new Paint();
    paintcol.setColor(Color.BLACK);
    paintcol.setStrokeWidth(8);
    paintcol.setStyle(Paint.Style.STROKE);
    p3 = new Path();
    p4 = new Path();
    p5 = new Path();
    p6 = new Path();
    p7 = new Path();
    p8 = new Path();
    p9 = new Path();
    p10 = new Path();
    p11 = new Path();
    Display display = getWindowManager().getDefaultDisplay();
    xmax = (float) display.getWidth() - 100;
    ymax = (float) display.getHeight() - 60;
    xmin = (float) display.getWidth() - 190;
    ymin = (float) display.getHeight() - 60;
    model.setXmax(xmax);
    model.setXmin(xmin);
    model.setYmax(ymax);
    model.setYmin(ymin);
}
@Override
protected void onPause() {
    super.onPause();
    sensorMgr.unregisterListener(this);
    sensorMgr = null;
}
@Override
protected void onResume() {
    super.onResume();
    sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
    boolean accelSupported = sensorMgr.registerListener(this,
            sensorMgr.getDefaultSensor(SENSOR_ACCELEROMETER),
            SENSOR_DELAY_GAME);
    sensorMgr.registerListener(this,
            sensorMgr.getDefaultSensor(Sensor.TYPE_ORIENTATION),
            SENSOR_DELAY_GAME);
    if (!accelSupported) {
        sensorMgr.unregisterListener(this);
    }
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    if (event.sensor.getType() == SENSOR_ACCELEROMETER) {
        xAcceleration = event.values[0];
        yAcceleration = event.values[2];
        model.updateBall(xAcceleration, yAcceleration);
        final float dT = (event.timestamp - timestamp) * NS2S / 50000;
        model.setFrameTime(dT);
    }
    if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
    }
}
public class CustomDrawableView extends View {
    public CustomDrawableView(Context context) {
        super(context);
        bm = BitmapFactory.decodeResource(getResources(),
                R.drawable.wall_back);
        bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.basket_ball1);
        box = new Graphic_Boundary(0xff00003f); // ARGB
        ball = new Graphic_Ball(Color.GREEN, bmp);
        this.setFocusable(true);
        this.requestFocus();
        this.setFocusableInTouchMode(true);
    }
    @Override
    public void onSizeChanged(int w, int h, int oldW, int oldH) {
        box.set(0, 0, w, h);
    }
    protected void onDraw(Canvas c) {
        int width = c.getWidth();
        int height = c.getHeight();
        c.drawRect(0, 0, width, height, backgroundPaint);
        c.drawBitmap(bm, 0, 0, null);
        float rightx = 150;
        float topy = 37;
        float leftx = 93;
        float bottomy = 57;
        Paint paintt = new Paint();
        paintt.setColor(Color.RED);
        paintt.setStrokeWidth(5);
        paintt.setStyle(Paint.Style.STROKE);
        RectF ovalBounds = new RectF(leftx, topy, rightx, bottomy);
        c.drawOval(ovalBounds, paintt);
        p3.moveTo(p3x, p3y);
        while (p3x < 100 && p3y < 200) {
            p3.lineTo(p3x, p3y);
            p3x = p3x + 1;
            p3y = p3y + 10;
        }
        p6.moveTo(150, 50);
        while (p6x > 143 && p6y < 200) {
            p6.lineTo(p6x, p6y);
            p6x = p6x - 1;
            p6y = p6y + 10;
        }
        p7.moveTo(p7x, p7y);
        while (p7x <= 151 && p7y <= 80) {
            p7.lineTo(p7x, p7y);
            p7x = p7x + 11;
        }
        p8.moveTo(p8x, p8y);
        while (p8x <= 95 && p8y <= 110) {
            p8.lineTo(p8x, p8y);
            p8x = p8x + 28;
        }
        p9.moveTo(p9x, p9y);
        while (p9x <= 148 && p9y <= 140) {
            p9.lineTo(p9x, p9y);
            p9x = p9x + 17;
        }
        p10.moveTo(p10x, p10y);
        while (p10x < 180 && p10y <= 140) {
            p10.lineTo(p10x, p10y);
            p10x = p10x + 29;
        }
        p11.moveTo(p11x, p11y);
        while (p11x <= 148 && p11y <= 140) {
            p11.lineTo(p11x, p11y);
            p11x = p11x + 24;
        }
        c.drawPath(p3, paint);
        c.drawPath(p6, paint);
        c.drawPath(p7, paint);
        c.drawPath(p8, paintcol);
        c.drawPath(p10, paintcol);
        c.drawPath(p9, paint);
        c.drawPath(p11, paint);
        ball.draw(c);
        ball.moveWithCollisionDetection(box);
        invalidate();
    }
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    float currentX = event.getX();
    float currentY = event.getY();
    float deltaX, deltaY;
    float scalingFactor = event.getSize();
    switch (event.getAction()) {
    case MotionEvent.ACTION_MOVE:
        deltaX = currentX - previousX;
        deltaY = currentY - previousY;
        ball.speedX += deltaX * scalingFactor;
        ball.speedY += deltaY * scalingFactor;
    }
    previousX = currentX;
    previousY = currentY;
    return true;
}

}

0 个答案:

没有答案