Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4000138
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:44:40+00:00 2026-05-20T07:44:40+00:00

I am a beginner Android developer and I am trying to make a game

  • 0

I am a beginner Android developer and I am trying to make a game where you move a bat (catcher) along the bottom of the screen and you have to catch falling blocks.

I am struggling currently with the collision detection. The collision detection itself is working correctly but when I then try to remove the block that is falling the game crashes.

The problem code is the line under the “remove block” comment in the checkCollisions() method but I have posted the whole GameView below for clarity.

Here is the code:

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;
}

}

Thanks in advance,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:44:40+00:00Added an answer on May 20, 2026 at 7:44 am

    Try replacing for (NormalBlock block : blocks) with this:

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

    I’m guessing you were getting a ConcurrentModificationException there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a beginner android developer and I am trying to have a button
I am a beginner in Android game development, while searching i found Rokon, a
I am only a beginner to Android, but I have noticed a number of
I am a beginner to android. I am trying to figure out the errors
I am beginner to android... Am trying following: In list of spinner items if
Hi everyone I am a beginner in android, please help. I have DBAdapter class
I am a complete beginner trying to develop for FCKeditor so please bear with
As a complete beginner with no programming experience, I am trying to find beautiful
I'm a Lisp beginner. I'm trying to memoize a recursive function for calculating the
I am a Java beginner , I want to develop Android app , what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.