0

我正在尝试实现乒乓游戏,我有两个类作为“PongGameView”和“GameState”

我实现了在“PongGameView”中实现的多点触控事件,并将垫的 x 坐标传递给“GameState”。

我想实现当用户触摸底部垫时垫应该独立移动,只有底部垫应该移动,如果用户触摸,顶部垫,只有顶部垫应该移动。但是如果我尝试移动其中一个垫,我会遇到两个垫都在移动的问题。我将如何实现这一目标?

 public class PongGameView extends SurfaceView  implements SurfaceHolder.Callback{

private GameThread _thread;

public PongGameView(Context context, AttributeSet attrs) {
    super(context, attrs);

    //So we can listen for events...
    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    setFocusable(true); 

    //and instantiate the thread
    _thread = new GameThread(holder, context, new Handler());
}  

@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
    return _thread.getGameState().keyPressed(keyCode, msg);
}

//Implemented as part of the SurfaceHolder.Callback interface
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    //Mandatory, just swallowing it for this example

}

//Implemented as part of the SurfaceHolder.Callback interface
@Override
public void surfaceCreated(SurfaceHolder holder) {
    _thread.start();
}

//Implemented as part of the SurfaceHolder.Callback interface
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    _thread.stop();
}

private float _x = 0;
private float _y = 0;



private int mActivePointerId;

public boolean onTouchEvent(MotionEvent event) {



    int action = MotionEventCompat.getActionMasked(event);
    // Get the index of the pointer associated with the action.
    int index = MotionEventCompat.getActionIndex(event);
    int xPos = -1;
    int yPos = -1;
    boolean flag=false;


    if (event.getPointerCount() > 1) {

        // The coordinates of the current screen contact, relative to 
        // the responding View or Activity.  
        xPos = (int)MotionEventCompat.getX(event, index);
        yPos = (int)MotionEventCompat.getY(event, index);
        flag=true;

    } else {
        // Single touch event

        xPos = (int)MotionEventCompat.getX(event, index);
        yPos = (int)MotionEventCompat.getY(event, index);
    }



    _thread.getGameState().surfaceTouched(xPos, yPos, flag );
    return true;
}



 }

具有 sufaceThouched 方法的第二类

  public class GameState {

//screen width and height
final int _screenWidth = 300;
final int _screenHeight = 420;

//The ball
final int _ballSize = 10;
int _ballX = 100;   int _ballY = 100;
int _ballVelocityX = 3;     int _ballVelocityY = 3;

//The bats
final int _batLength = 75;  final int _batHeight = 10;
int _topBatX = (_screenWidth/2) - (_batLength / 2);
final int _topBatY = 20;
int _bottomBatX = (_screenWidth/2) - (_batLength / 2);  
final int _bottomBatY = 400;
final int _batSpeed = 3;
boolean flag =false;

public GameState()
{
}

//The update method
public void update() {

_ballX += _ballVelocityX;
_ballY += _ballVelocityY;

//DEATH!
if(_ballY > _screenHeight || _ballY < 0)        
{_ballX = 100;  _ballY = 100;}      //Collisions with the sides

if(_ballX > _screenWidth || _ballX < 0)
            _ballVelocityX *= -1;   //Collisions with the bats      

if(_ballX > _topBatX && _ballX < _topBatX+_batLength && _ballY < _topBatY)      
                 _ballVelocityY *= -1;  //Collisions with the bats      

if(_ballX > _bottomBatX && _ballX < _bottomBatX+_batLength 
                && _ballY > _bottomBatY)
                       _ballVelocityY *= -1;
}

public boolean keyPressed(int keyCode, KeyEvent msg)
{
if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT) //left
{
_topBatX += _batSpeed; _bottomBatX -= _batSpeed;
}

if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) //right
{
_topBatX -= _batSpeed; _bottomBatX += _batSpeed;
}

return true;
}

//the draw method
public void draw(Canvas canvas, Paint paint) {

//Clear the screen
canvas.drawRGB(20, 20, 20);

//set the colour
paint.setARGB(200, 0, 200, 0);



//draw the ball
canvas.drawRect(new Rect(_ballX,_ballY,_ballX + _ballSize,_ballY + _ballSize),
                             paint);

paint.setARGB(200, 200,0, 0);
//draw the bats
canvas.drawRect(new Rect(_topBatX, _topBatY, _topBatX + _batLength,
                                  _topBatY + _batHeight), paint); //top bat

flag = true ;
paint.setARGB(200,0,0,200);

canvas.drawRect(new Rect(_bottomBatX, _bottomBatY, _bottomBatX + _batLength, 
                                      _bottomBatY + _batHeight), paint); //bottom bat
flag = false ;

}

public boolean surfaceTouched(float posX, float posY, boolean flag) {

    if (flag= true)
    {
    _bottomBatX = (int) posX;
    }
    else
    {
        _topBatX = (int) posX;
    }


    return true;
    }

}

4

0 回答 0