Java game programming: multiple instances of an object, treat same way?

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!

以上就是Java game programming: multiple instances of an object, treat same way?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏