First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing!
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: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
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: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.