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 8717535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:19:37+00:00 2026-06-13T06:19:37+00:00

Sorry if the title didn’t make much sense. I’m making a Chip’s Challenge style

  • 0

Sorry if the title didn’t make much sense. I’m making a Chip’s Challenge style game in Java Eclipse where special things like the Player, coins, and pushable blocks are a class. Interactions between the pushable block and the player require a lot of collision detection logic, and the code is getting atrocious because I have to do the same logic checks for each instance of the class. For example:

public void pb1TouchingBaddy() {

    if (pb1.getTileX() == b1.getTileX() & pb1.getTileY() == b1.getTileY()) {
        if (b1.getbUp() == 1 & pb1.getTileY() - 1 != pb2.getTileY()) {
            pb1.move(0, -1);
        } else if (b1.getbDown() == 1
                & pb1.getTileY() + 1 != pb2.getTileY()) {
            pb1.move(0, 1);
        } else if (b1.getbRight() == 1
                & pb1.getTileX() + 1 != pb2.getTileX()) {
            pb1.move(1, 0);
        } else if (b1.getbLeft() == 1
                & pb1.getTileX() - 1 != pb2.getTileX()) {
            pb1.move(-1, 0);
        }
    }

    if (pb1.getTileX() == b2.getTileX() & pb1.getTileY() == b2.getTileY()) {
        if (b2.getbUp() == 1 & pb1.getTileY() - 1 != pb2.getTileY()) {
            pb1.move(0, -1);
        } else if (b2.getbDown() == 1
                & pb1.getTileY() + 1 != pb2.getTileY()) {
            pb1.move(0, 1);
        } else if (b2.getbRight() == 1
                & pb1.getTileX() + 1 != pb2.getTileX()) {
            pb1.move(1, 0);
        } else if (b2.getbLeft() == 1
                & pb1.getTileX() - 1 != pb2.getTileX()) {
            pb1.move(-1, 0);
        }
    }

    if (pb1.getTileX() == b3.getTileX() & pb1.getTileY() == b3.getTileY()) {
        if (b3.getbUp() == 1 & pb1.getTileY() - 1 != pb2.getTileY()) {
            pb1.move(0, -1);
        } else if (b3.getbDown() == 1
                & pb1.getTileY() + 1 != pb2.getTileY()) {
            pb1.move(0, 1);
        } else if (b3.getbRight() == 1
                & pb1.getTileX() + 1 != pb2.getTileX()) {
            pb1.move(1, 0);
        } else if (b3.getbLeft() == 1
                & pb1.getTileX() - 1 != pb2.getTileX()) {
            pb1.move(-1, 0);
        }
    }

    if (pb1.getTileX() == b4.getTileX() & pb1.getTileY() == b4.getTileY()) {
        if (b4.getbUp() == 1 & pb1.getTileY() - 1 != pb2.getTileY()) {
            pb1.move(0, -1);
        } else if (b4.getbDown() == 1
                & pb1.getTileY() + 1 != pb2.getTileY()) {
            pb1.move(0, 1);
        } else if (b4.getbRight() == 1
                & pb1.getTileX() + 1 != pb2.getTileX()) {
            pb1.move(1, 0);
        } else if (b4.getbLeft() == 1
                & pb1.getTileX() - 1 != pb2.getTileX()) {
            pb1.move(-1, 0);
        }
    }

So as you can see those three blocks of if statements are all the exact same logic, just applied to different instances of the same object. Is there any way to write this without having to repeat myself so much? I was able to turn the four directional movement logic into one method and thought I was a genius for a while, but I can’t figure out how to do a similar thing but with multiple instances of the same object. Right now I’m stuck with just two of the pushable blocks before I get too confused and can’t do the logic for another one. Would appreciate any advice, thanks!

  • 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-06-13T06:19:38+00:00Added an answer on June 13, 2026 at 6:19 am
    private function move(PBClass pb1, PBClass pb2, BClass b) {
        if (pb1.getTileX() == b.getTileX() & pb1.getTileY() == b.getTileY()) {
            if (b.getbUp() == 1 & pb1.getTileY() - 1 != pb2.getTileY()) {
                pb1.move(0, -1);
            } else if (b.getbDown() == 1
                    & pb1.getTileY() + 1 != pb2.getTileY()) {
                pb1.move(0, 1);
            } else if (b.getbRight() == 1
                    & pb1.getTileX() + 1 != pb2.getTileX()) {
                pb1.move(1, 0);
            } else if (b.getbLeft() == 1
                    & pb1.getTileX() - 1 != pb2.getTileX()) {
                pb1.move(-1, 0);
            }
        }
    }
    
    public void pb1TouchingBaddy() {
        move(pb1, pb2, b1);
        move(pb1, pb2, b2);
        move(pb1, pb2, b3);
        move(pb1, pb2, b4);
    }
    

    of course you’ll need to replace PBClass and BClass with your actual class names.

    If you have an array of objects:

    PBClass[] listPB = { pb1, pb2, pb3, pb4};
    BClass[] listB = { b1, b2, b3, b4};
    
    public void pb1TouchingBaddy() {
        for (int i=0; i < listPB.length - 1; i+=2) {
            for (int j=0; j < listB.length; j++) {
                move(pb[i], pb[i+1], b[j]);
            }
        }
    }
    

    Or you can use ArrayLists in the same way

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

Sidebar

Related Questions

I know that title didn't make sense, Im sorry! Its hard to word what
Sorry if the title didn't make any sense. Currently, the following parameters on the
Sorry if the title doesn't make sense. Basically I have a series of strings
[ Update: Changed question title to be more specific] Sorry if I didn't make
Let me first start off, sorry for the confusing title. I didn't know how
Sorry if that title didn't explain it well. Here is the table... (source: alexanderdickson.com
[Sorry for the title, I really didn't find a good one, if anyone has
Sorry for the vague title but I really didn't know what title to give
Sorry for the none-descriptive title, but I didn't really have a clue as to
I'm sorry if I didn't explain my problem well enough in the title. In

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.