Step 4: Wall Detection


First, we will check for a collision on the left side, by switching to the Left Detector animation and testing if the Enemy (which is now a blue line 16 pixels high and 1 pixel wide) is overlapping an obstacle. If a collision occurs, the Enemy will be sent the opposite direction.
Image( "Enemy" ) retrieves the current animation frame of the Enemy object. You can either type the expression directly or it can be generated for you by clicking Retrieve data from an object, then on the Enemy object and selecting Animation and then Current Frame.


Next, we will check for a collision on the right side of the Enemy, by switching to the Right Detector animation and testing for an overlap. Once again reversing the direction if a collision is detected.
Run the application and you will see the Enemy (still playing the Right Detector animation) moving back and forth across the screen. Before we switch back to the Walking animation, we must add gravity.
7