碰撞检测不起作用

时间:2016-09-17 11:16:05

标签: java android eclipse collision detection

基本上我一直在做一个简单的游戏,我现在想在玩家和敌人之间创建碰撞检测。我已经制作了一个玩家和敌人的类,然后还有一个单独的GameView类。我的问题是没有碰撞发生,我只是不明白为什么,我已经改变了代码的次数,但我似乎仍然无法破解它。我将把我的代码留在下面,如果有人看到我出错的地方,那将是非常有帮助的。谢谢。

GameView类:

// Select all dates in the past
if (e.Day.Date < System.DateTime.Today)
{   

  // Disable date
  e.Day.IsSelectable = false;
  // Change color of disabled date
  e.Cell.ForeColor = Color.Gray;
} 

玩家等级:

public class GameView extends SurfaceView implements Runnable {

    Canvas canvas;
    SurfaceHolder surfaceHolder;
    Thread thread = null;
    volatile boolean playing;
    Paint paint;
    Context context;
    Player player;
    int screenX, screenY, numberOfEnemies = 4, distanceBetweenEnemies;
    int enemyX [] = new int[numberOfEnemies];
    int enemyY [] = new int[numberOfEnemies];
    private boolean paused = true;
    Enemy enemy;
    Enemy [] enemies;
    Random random;
    Rect [] enemyRectangle;

    public GameView (Context context, int x, int y) {
        super(context);
        this.context = context;

        surfaceHolder = getHolder();
        paint = new Paint();
        thread = new Thread();
        screenX = x;
        screenY = y;
        player = new Player (context, screenX, screenY);

        enemies = new Enemy[numberOfEnemies];
        enemyRectangle = new Rect[numberOfEnemies];
        enemy = new Enemy (context, screenX);
        distanceBetweenEnemies = screenX * 3 / 4;

        for (int i = 0; i < numberOfEnemies; i ++) {

            enemies[i] = new Enemy(context, screenX);
            enemyX[i] = screenX / 2 - enemy.getEnemyBitmap().getWidth() / 2 + i * distanceBetweenEnemies;
            random = new Random();
            enemyY[i] = random.nextInt(screenY - enemy.getEnemyBitmap().getHeight() / 2);
        }
    }

    @Override
    public void run() {
        while (playing) {
            draw();

            if(!paused){
                update();
            }
        }
    }

    private void draw () {

        if (surfaceHolder.getSurface().isValid()) {

            canvas = surfaceHolder.lockCanvas();
            canvas.drawColor(Color.argb(255,  26, 128, 182));
            canvas.drawBitmap(player.getPlayerBitmap(), player.getX(), player.getY(), null);

            for (int i = 0; i < numberOfEnemies; i ++) {

                canvas.drawBitmap(enemies[i].getEnemyBitmap(), enemyX[i], enemyY[i], null);
                enemyRectangle [i] = new Rect(enemyX[i], enemyY[i], enemy.getEnemyBitmap().getWidth(),
                    enemy.getEnemyBitmap().getHeight());
                enemyX[i] += enemy.getEnemySpeed();

            }

            update ();
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }

    private void update () {

        player.updatePlayerPosition();
        player.noLeaveScreen();

        for (int i = 0; i < numberOfEnemies; i ++) {

            if (enemyX[i] < 0 - enemy.getEnemyBitmap().getWidth()) {
                enemyY[i] = random.nextInt(screenY);
                enemyRectangle [i] = new Rect(enemyX[i], enemyY[i], enemy.getEnemyBitmap().getWidth(),
                    enemy.getEnemyBitmap().getHeight());
                enemyX[i] += numberOfEnemies * distanceBetweenEnemies;
            } else {
                enemyX[i] += enemy.getEnemySpeed();
            }

            if (Rect.intersects(player.getPlayerRectangle(), enemyRectangle[i])) {
                Log.e("COLLISION:", "Detected");
                enemyX[i] = - 200;
            }
        }
    }

    public void pause () {
        playing = false;

        try {
            thread.join();
        } catch (InterruptedException e) {
            Log.e("Error:", "joining thread");
        }
    }

    public void resume () {
        playing = true;
        thread = new Thread(this);
        thread.start();
    }

    @Override
    public boolean onTouchEvent (MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:

                player.playerJump();

                break;
        }
        return true;
    }
}

敌人类:

public class Player {

    Bitmap playerBitmap;
    Rect playerRectangle;
    int x, y, playerJumpSpeed, gravity, screenY;
    boolean playerIsMoving;

    public Player (Context context, int screenX, int screenY) {
        this.screenY = screenY;
        gravity = 2;
        playerBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
        x = screenX/2 - playerBitmap.getWidth()/2;
        y = screenY/2 - playerBitmap.getHeight()/2;
        playerRectangle = new Rect(x, y, playerBitmap.getWidth(), playerBitmap.getHeight());
        playerJumpSpeed =  - 1000;
        playerIsMoving = true;
    }

    public void updatePlayerPosition () {

        while (playerIsMoving) {
            y += gravity;

            break;
        }
    }

    public void playerJump () {

        while (playerIsMoving) {
            y += playerJumpSpeed;

            break;
        }

    }

    public void noLeaveScreen () {

        if (y < 0) {
            playerJumpSpeed = 0;
        } else {
            playerJumpSpeed = - 40;
        }

        if (getY() > (screenY - playerBitmap.getHeight())) {
            gravity = 0;
        } else {
            gravity = 2;
        }

    }

    public int getX () {
        return x;
    }
    public int getY () {
        return y;
    }
    public Bitmap getPlayerBitmap () {
        return playerBitmap;
    }

    public Rect getPlayerRectangle () {
        return  playerRectangle;
    }

}

有课程,有任何帮助表示赞赏!

0 个答案:

没有答案