0

pong Gameを実装しようとしていますが、「PongGameView」と「GameState」の2つのクラスがあります。

「PongGameView」に実装されたマルチタッチイベントを実装し、パッドのxcooridantesを「GameState」に渡しました。

ユーザーが下部パッドに触れたときにパッドが独立して移動し、下部パッドのみが移動し、ユーザーが上部パッドに触れた場合は上部パッドのみが移動するようにしたかったのです。しかし、パッドの1つを動かそうとすると、両方のパッドが動いてしまうという問題があります。どうすればこれを達成できますか?

 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メソッドを持つ2番目のクラス

  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