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: 7 Votes: 44 |
| » Author |
| 16 years old. Male. Likes working with Flash. |
| » Download |
| Download the files used in this tutorial. |
| Download (4 kb) |
| » Forums |
| More help? Search our boards for quick answers! |
-
You must have javascript enabled in order to post comments.


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