Working with Movie Clips and Buttons > Using event methods in a Flash document |  |

Using event methods in a Flash document

Macromedia Flash MX ships with a sample movie called Scrolling_starfield.fla that can be found in the FlashMX/Samples folder on your hard drive (or by choosing Help > Samples in Macromedia Flash MX and then choosing Game element: Scrolling background). You can look at the ActionScript in Scrolling_starfield.fla to see how events are handled in a real Macromedia Flash document.

The author of Scrolling_starfield.fla included three event handlers in a script in Frame 1 of the main Timeline. The first is the `enterFrame` event for the main Timeline. Each movie clip Timeline has an `enterFrame` event that is invoked each time the playhead enters a frame. Even in a one-frame movie, the playhead repeatedly enters that frame and the event is invoked each time. By defining a function for `_root.enterFrame`, the author of Scrolling_starfield.fla created a function that executes over and over as the movie plays and scrolls the starfield. You don't need to understand the code in the body of the function to understand how the function is defined and assigned to the `onEnterFrame` event, as follows:

    _root.onEnterFrame = function() {
    	with (starfield) {
    		// move starfield based on the positions of the faders
    		_x += (horizfader._x-horizfader.initx)/15;
    		_y += (vertfader._y-vertfader.inity)/15;
    		//
    		// loop the starfield
    		if (_x>initx+220) {
    			_x = initx;
    		}
    		if (_x<initx) {
    			_x = initx+220;
    		}
    		if (_y>inity+215) {
    			_y = inity;
    		}
    		if (_y<inity) {
    			_y = inity+215;
    		}
    	}
    };

Scrolling_starfield contains two buttons, `generate` and `toggle`. The `generate` button creates a starfield when it is released, and the `toggle` button toggles the starfield's edge lines on and off when it is released. The author of Scrolling_starfield.fla defined a function for each button's `onRelease` event. Again, you don't need to understand the code in the body of the functions to appreciate how the functions are assigned to the `onRelease` events, as follows:

    // generate starfield when generate button is clicked
    generate.onRelease = function() {
    	for (i=1; i<=300; i += 4) {
    		for (a=0; a<=3; a++) {
    			duplicateMovieClip(starfield.star, "star"+(i+a), i+a);
    			if (i%7 == 0) {
    				starfield["star"+(i+a)]._xscale = 200;
    				starfield["star"+(i+a)]._yscale = 200;
    			}
    		}
    		starfield["star"+i]._x = Math.floor(Math.random()*220);
    		starfield["star"+i]._y = Math.floor(Math.random()*215);
    		starfield["star"+(i+1)]._x = starfield["star"+i]._x-220;
    		starfield["star"+(i+1)]._y = starfield["star"+i]._y;
    		starfield["star"+(i+2)]._x = starfield["star"+i]._x-220;
    		starfield["star"+(i+2)]._y = starfield["star"+i]._y-215;
    		starfield["star"+(i+3)]._x = starfield["star"+i]._x;
    		starfield["star"+(i+3)]._y = starfield["star"+i]._y-215;
    	}
    };
    //
    // toggle dotted cross-hair visibility
    toggle.onRelease = function() {
    	starfield.centerlines._visible = !starfield.centerlines._visible;
    };