Return to Kroll Design home page

Flash Tutorials (with .FLA files included)

 
« Return

ActionScript 2

Detecting keyboard events

Download the FLA file

Here's the code for the 'Start' button, which when clicked, makes the circle visible by setting its alpha (opacity) to 100%, and moves it 100 pixels from the top and 100 pixels from the left. In ActionScript 2, you can place ActionScript for a given movie clip or button on the main timeline or attached to the movie clip itself. This example has the code attached to the button itself:

on(release)  {
	this.circle_mc._alpha = 100;
	this.circle_mc._x = 100;
	this.circle_mc._y = 100;
}

Here's the code for the circle. 'Key.RIGHT' is the right arrow key on your keyboard, so when you press the right arrow key the circle moves 6 pixels to the right. The other 3 arrow keys are handled in similar fashion:

onClipEvent(enterFrame) {
	
	if (Key.isDown(Key.RIGHT)) {
		this._x += 6;
	}

	if (Key.isDown(Key.LEFT)) {
		this._x -= 6;
	}
	
	if (Key.isDown(Key.UP)) {
		this._y -= 6;
	}
	
	if (Key.isDown(Key.DOWN)) {
		this._y += 6;
	}
	
}


 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM