First time here? First time here? Newsletter Signup Newsletter
651326 Members and growing! | Flash Jobs
Search tutorials
Author: Andross

The Code

Place this code in the first frame of the movie, preferably in a different layer.

	sprite.speed = 5;
	sprite.onEnterFrame = function() {
	if (Key.isDown(Key.RIGHT)) {
		this._x += this.speed;
	}
	if (Key.isDown(Key.LEFT)) {
		this._x -= this.speed;
	}
	if (Key.isDown(Key.DOWN)) {
		this._y += this.speed;
	}
	if (Key.isDown(Key.UP)) {
		this._y -= this.speed;
	}
};

I guess I have to do some explanation or this wouldn't be a tutorial, damn. Okay, well here it goes.
Lets look at the first line.

	sprite.speed = 5;
	

Here you define a variable inside the sprite movie clip called 'speed' and assign a value of 5 to it.

	sprite.onEnterFrame = function() {
	if (Key.isDown(Key.RIGHT)) {
		this._x += this.speed;
	}
	if (Key.isDown(Key.LEFT)) {
		this._x -= this.speed;
	}
	if (Key.isDown(Key.DOWN)) {
		this._y += this.speed;
	}  
	if (Key.isDown(Key.UP)) {
		this._y -= this.speed;
	}

Here you define a function that will execute whenever the sprite movie clip enters a frame. Depending on your movie's frame speed, a movie clip would go through a frame 30 times per second if your movie's frame speed were 30 frames/sec. Lets say your frame speed was 30, then this function would be executing 30 times per second, pretty fast huh?



	if (Key.isDown(Key.RIGHT)) {
		this._x += this.speed;
	}

The translation of this code to english would be something like this:
"If " the RIGHT ARROW key is "pressed down" than move "this" movie clip in the "positive x" direction by increments of 5 pixels.
The rest is basically the same thing except that the last two are in the y direction.

» Level Basic

Added: 2004-08-10
Rating: 6.93 Votes: 46
(10 being the highest)
» Author
16 years old. Male. Likes working with Flash.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.