|
|
| Demo 1 | Demo 2 |
Here are two demonstration projects showing how to script interactions between movie clips--and in the process, how to adapt existing code for new applications.
Both projects are based on the timelineVsScripting demo presented some weeks back. I've removed the background graphic, the keyframe animation, and one of the sounds.
In the first version (Demo 1, above left) the code that drives the blue bouncer is almost identical to the code from timelineVsScripting. I've added the bricks, each of which is a Movie Clip with a simple script attached:
onClipEvent(enterFrame){
if(this._visible == true && this.hitTest(_root.bouncer){
this._visible = false;
_root.bouncer.bounceOff();
}
}
On every repetition of the main movie's single frame, this script asks whether the Movie Clip called bouncer has collided with the current object (a brick), and whether the brick is visible. This script is installed on each copy of the brick.
If both conditions are met, two things happen. First, the brick that receives the collision becomes invisible--which means that future collisions will have no effect upon it--and the bouncer Movie Clip is told to perform the function called bounceOff(). You may recall that this function makes the animated object change direction (bounce back).
The second demonstration project (Demo 2, above right) adds a few more changes. Now the x-axis or horizontal position of the bouncing ball is set only when the ball returns to the bottom of the screen, and it matches the x-position of the mouse at that point. This allows the viewer to aim the ball. I've also added an indicator (the gray triangle) to show the current x-position.
Neither of these little sketches makes a very satisfactory computer game, of course; but they do show how one body of code can be rapidly modified for other purposes.
The Flash source files, breakoutDemo1.fla and breakoutDemo2.fla, are in MMShare/breakoutDemo on Crow.


