2011-02-27 6 views
0

초보자 Android 개발자이며 화면 아래쪽에있는 박쥐 (포수)를 움직이는 게임을 만들려고합니다. 캐치 떨어지는 블록.충돌없이 화면에 그려진 블록의 ArrayList에서 블록을 제거하십시오.

현재 충돌 감지에 어려움을 겪고 있습니다. 충돌 감지 자체는 올바르게 작동하지만 게임 충돌로 인해 떨어지는 블록을 제거하려고 할 때

문제 코드는 checkCollisions() 메소드의 "블록 제거"주석 아래에있는 행이지만 명확성을 위해 아래 전체 GameView를 게시했습니다.

package com.mattdrewery.supercatch; 

import android.view.View; 
import android.view.MotionEvent; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Rect; 
import java.util.Random; 
import java.util.ArrayList; 

public class GameView extends View 
{ 
private Catcher catcher; 
private ArrayList<NormalBlock> blocks; 
private Random rand; 
private int screenWidth; 
private int screenHeight; 
private boolean gameOver; 
private int updateCount; 

public GameView(Context context) 
{ 
    super(context); 
    setFocusable(true); 

    updateCount = 1; 

    // Set gameOver to false 
    gameOver = false; 

    // Create the ArrayList of normal blocks 
    blocks = new ArrayList<NormalBlock>(); 

    // Create a random number generator 
    rand = new Random(); 

    // Create the catcher 
    catcher = new Catcher(context, R.drawable.catcher, 230, 250); 
} 

@Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) 
{ 
    screenWidth = this.getWidth(); 
    screenHeight = this.getHeight(); 
} 

@Override 
protected void onDraw(Canvas canvas) 
{ 
    updateCatcher(); 
    checkCollisions(); 
    updateBlocks(); 

    // Draw the catcher to the canvas 
    canvas.drawBitmap(catcher.getImage(), catcher.getPosX(), catcher.getPosY(), null); 

    // For each normal block in the block array 
    for (NormalBlock block : blocks) 
    { 
     // Draw block to the canvas 
     canvas.drawBitmap(block.getImage(), block.getPosX(), block.getPosY(), null); 
    } 

    // Redraw the screen 
    invalidate(); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) 
{ 
    // Get the action from the touch screen 
    int eventAction = event.getAction(); 

    int X = (int) event.getX(); 

    // If the user presses on the screen.... 
    if (eventAction == MotionEvent.ACTION_DOWN) 
    { 
     if (X < screenWidth/2) 
     { 
      catcher.setMovingLeft(true); 
      catcher.setMovingRight(false); 
     } 
     else 
     { 
      catcher.setMovingLeft(false); 
      catcher.setMovingRight(true); 
     } 
    } 

    if (eventAction == MotionEvent.ACTION_MOVE) 
    { 
     if (X < screenWidth/2) 
     { 
      catcher.setMovingLeft(true); 
      catcher.setMovingRight(false); 
     } 
     else 
     { 
      catcher.setMovingLeft(false); 
      catcher.setMovingRight(true); 
     } 
    } 

    if (eventAction == MotionEvent.ACTION_UP) 
    { 
     catcher.setMovingLeft(false); 
     catcher.setMovingRight(false); 
    } 

    return true; 
} 

private void updateCatcher() 
{ 
    // Check whether the catcher is moving and update position accordingly 
    if (catcher.isMovingLeft()) 
    { 
     catcher.moveLeft(); 
     if (catcher.getPosX() < 0) 
     { 
      catcher.setPosX(0); 
     } 
    } 
    else if (catcher.isMovingRight()) 
    { 
     catcher.moveRight(); 
     if (catcher.getPosX() > (screenWidth - catcher.getImage().getWidth())) 
     { 
      catcher.setPosX(screenWidth - catcher.getImage().getWidth()); 
     } 
    } 
} 

private void updateBlocks() 
{ 
    updateCount++; 

    if (updateCount >= 100) 
    { 
     updateCount = 1; 
     blocks.add(new NormalBlock(getContext(), R.drawable.nblock, 
       rand.nextInt(screenWidth - 20), 0)); 
    } 

    // For each normal block in the block array 
    for (NormalBlock block : blocks) 
    { 
     // Check if block has hit the bottom of the screen 
     if (block.getPosY() + block.getImage().getHeight() > screenHeight) 
     { 
      gameOver = true; 
     } 
     else 
     { 
      // Drop the block down 
      block.dropDown(); 
     } 
    } 
} 

private void checkCollisions() 
{ 
    // Get Rectangle for the catcher 
    Rect catcherRect = new Rect(catcher.posX, catcher.posY, 
      catcher.posX + catcher.image.getWidth(), 
      catcher.posY + catcher.image.getHeight()); 

    // For each block in the array 
    for (NormalBlock block : blocks) 
    { 
     // Get Rectangle for the current block 
     Rect blockRect = new Rect(block.posX, block.posY, 
       block.posX + block.image.getWidth(), 
       block.posY + block.image.getHeight()); 

     if (Rect.intersects(catcherRect, blockRect)) 
     { 
      // Remove block 
      blocks.remove(block); 
     } 
    } 

} 

public int getScreenHeight() 
{ 
    return screenHeight; 
} 

public int getScreenWidth() 
{ 
    return screenWidth; 
} 

} 사전에

감사합니다,

답변

0

이 함께 for (NormalBlock block : blocks)를 교체하십시오 :

int count = blocks.size(); for (int i=0;i<count;i++){ Block block = blocks.get(i);

을 당신이 있었다 추측하고있어 여기

코드입니다 점점 ConcurrentModificationException 거기.

+0

도움을 주셔서 감사합니다.하지만 불행히도 저는 여전히 같은 문제를 겪고 있습니다. : – MattDrewery

+0

실제로, 목록에서 요소를 제거 할 때마다 카운트 ('count -;')를 줄여야합니다 .logcat은 무엇을 표시합니까? – zrgiu

+0

고마워요 :) 그게 수정되었습니다. – MattDrewery

관련 문제