September 2018
Intermediate to advanced
328 pages
9h 10m
English
The code related to collision detection and the Rect movement is handled in the updateRectLocation() function, which is shown as follows:
/** * Updates the rectangle location by +/- 1px in the x or y based on * the current location. */void updateRectLocation() { // Determine if the bounding rectangle has "bumped" into either // the left/right side or top/bottom side. Depending on which side, // flip the direction: int xBouncePoint = bounds.width - rect.width; if (rect.x == xBouncePoint) rect.horizDir = 'L'; if (rect.x == 0) rect.horizDir = 'R'; int yBouncePoint = bounds.height - rect.height; if (rect.y == yBouncePoint) rect.vertDir = 'U'; if (rect.y == 0) rect.vertDir = 'D'; // If the direction has changed ...Read now
Unlock full access