В конце концов, зависит от того, что вы хотите сделать в hitDetection
Если вы просто хотите проверить, произошло ли попадание
В этом случаевы можете просто получить список Rect
и проверить, произошел ли какой-либо удар
GameplayScene.java
public void hitDetection() {
ArrayList<Rect> listOfBounds = obstacleManager.getObstacleBounds();
for(Rect bound : listOfBounds) {
// Do you stuff
// Note the here, you are receiving a list of Rects only.
// So, you can only check if a hit happened.. but you can't update the Obstacles because here, you don't have access to them.
// Nothing stops you of receiving the whole list of items if you want to(like the reference of ObstacleManager.obstacles).
}
}
ObstacleManager.java
public ArrayList<Rect> getObjectBounds() {
// You can also return just an array.. like Rect[] listToReturn etc
ArrayList<Rect> listToReturn = new ArrayList(obstacles.size());
for (Obstacle item : obstacles) {
listToReturn.add(item.getObjectBounds);
}
return listToReturn;
}
Есливам нужно обновить некоторую информацию о препятствии, которое было достигнуто
В этом случае вы можете передать логику hitDetection вам ObstacleManager (я предполагаю, что вы проверяете координаты X и Y, чтобы проверить, было ли препятствиехит):
GameplayScene.java
public void hitDetection(int touchX, int touchY) {
Obstacle objectHit = obstacleManager.getObstacleTouched(int touchX, int touchY);
if (objectHit != null) {
objectHit.doStuffAlpha();
objectHit.doStuffBeta();
} else {
// No obstacle was hit.. Nothing to do
}
}
ObstacleManager.java
public Obstacle getObstacleTouched(int touchX, int touchY) {
Obstacle obstacleToReturn = null;
for (Obstacle item : obstacles) {
if(item.wasHit(touchX, touchY) {
obstacleToReturn = item;
break;
}
}
return listToReturn;
}
Есть несколько способов достичь того, что вы хотите.Одни лучше других и т. Д. В конце концов, зависит от того, что именно вы хотите сделать.