I am making a very simple demo in AndEngine in which I have three rectangles: rect1, rect2 and rect3 in an ArrayList.
I register onAreaTouch as follows:
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX,
final float pTouchAreaLocalY) {
switch (pSceneTouchEvent.getAction()) {
case TouchEvent.ACTION_DOWN:
// this.setScale(1.0f);
this.mGrabbed = true;
break;
case TouchEvent.ACTION_MOVE:
if (this.mGrabbed) {
for (int i = 0; i < rectangles.size(); i++) {
int index = rectangles.indexOf(this);
if (i != index) {
if (this.collidesWith(rectangles.get(i))) {
// should not overlap
} else {
this.setPosition(pSceneTouchEvent.getX() - 90, pSceneTouchEvent.getY() - 90);
}
}
}
}
break;
case TouchEvent.ACTION_UP:
if (this.mGrabbed) {
this.mGrabbed = false;
}
break;
}
return true;
}
Rectangles are now overlapping with one another. I don't want this behavior, instead I'd like it if rect1 collides with rect2 then rect1 should not move further, but can move in any other direction.
[Cross-Posted here]