Make your own reusable classes using Flash and AS3
This article has been added to your 'Articles to Read' list.

KeyMover continued: Wrapping the screen
Jody Hall
My interest in Flash started mostly because of a Jib-Jab cartoon ("This Land") in 2004. I'm the author of a feature I call "Mazoons," which are a combination of mazes and cartoons. In 2002, I even had a book published, "Super Silly Mazes." I'm not a professional programmer, but making my mazes interactive by programming them with Flash became a hobby/obsession of mine, to the point where I have now learned more than I bargained for. Lately I'm working on a new website about Flash and Actionscript 3.0 called The Flash Connection.
View all articles by Jody HallFirst, however, go back to the fla file and make sure that the registration point of the ball MovieClip is in the upper left hand corner, as our programming statements in the class are going to assume that. If it's not, double-click the MovieClip in the library to edit it. All of the contained graphics should be selected automatically, but if not, make sure you select them all. Then, in the align panel (press CRTL-K for the panel if it's not already around), make sure the "to stage" button is selected. In the first row of buttons, click the align left and align top buttons. Done! Go back to scene 1, and we'll leave the fla file for now.
Now, back to the moveClip function in our KeyMover class. Let’s take on the right arrow key first. The logic goes like this: If the user is pressing the right arrow key, and if the _clip’s x property exceeds the width of the screen, change the _clip’s x property to the left side of the screen (which would be 0), minus the width of the _clip. The width of the screen is contained in stage.stageWidth. As before, we’ll use the _clip’s stage property to get a reference to the stage:
if (rightArrow) {
_clip.x += speed;
if (_clip.x > _clip.stage.stageWidth) {
_clip.x = -(_clip.width);
}
}
Save the class, go back to the fla and test the movie. You’ll get screen wrapping when you hold down the right arrow key. Pretty cool, eh? The coding for all the other arrow keys follow similar logic. Here's the entire KeyMover class, inlcuding the revised moveClip function, which now provides screen wrapping in all four directions:
package com.mysite.keyboard{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class KeyMover {
private var _clip:MovieClip;
private var rightArrow:Boolean;
private var leftArrow:Boolean;
private var upArrow:Boolean;
private var downArrow:Boolean;
private var speed:uint = 10;
public function KeyMover(clip:MovieClip) {
_clip = clip;
_clip.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
_clip.stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
_clip.stage.addEventListener(Event.ENTER_FRAME, moveClip);
}
private function keyPressed(event:KeyboardEvent) {
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = true;
}
if (event.keyCode == Keyboard.LEFT) {
leftArrow = true;
}
if (event.keyCode == Keyboard.UP) {
upArrow = true;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = true;
}
}
private function keyReleased(event:KeyboardEvent) {
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = false;
}
if (event.keyCode == Keyboard.LEFT) {
leftArrow = false;
}
if (event.keyCode == Keyboard.UP) {
upArrow = false;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = false;
}
}
private function moveClip(event:Event) {
if (rightArrow) {
_clip.x += speed;
if (_clip.x > _clip.stage.stageWidth) {
_clip.x = -(_clip.width);
}
}
if (leftArrow) {
_clip.x -= speed;
if (_clip.x < -(_clip.width)) {
_clip.x = _clip.stage.stageWidth;
}
}
if (upArrow) {
_clip.y -= speed;
if (_clip.y < -(_clip.height)) {
_clip.y = _clip.stage.stageHeight;
}
}
if (downArrow) {
_clip.y += speed;
if (_clip.y > _clip.stage.stageHeight) {
_clip.y = -(_clip.height);
}
}
}
}
}
That concludes the KeyMover class. There are, of course, other refinements that could be made. For example, I usually prefer to use the Timer class instead of the ENTER_FRAME event to provide continuous action, as you can create motion that’s smoother and way less dependent on the movie’s frame rate, especially if you also use the TimerEvent class's updateAfterEvent() method. But I wanted to keep this tutorial fairly simple and basic, and if you want to, you can go on to create that. Or, hit me up for the class file, and I’d be glad to share it with you.
If you’ve made it this far in the tutorial, and you’ve actually created the two useful classes outlined above (TestClass doesn't count, haha!), here’s a little side benefit for you:. In your fla file that you used for KeyMover, let’s make the ball a member of both of the classes we’ve made. Change the code in the first frame of the fla to this:
import com.mysite.keyboard.KeyMover;
import com.mysite.mouse.ClipDragger;
var keyMover:KeyMover = new KeyMover(ball);
var clipDragger:ClipDragger = new ClipDragger(ball);
Test the movie. You’ll find that now the ball movie clip is being controlled by both classes! You can click and drag the MovieClip, and you can also move it around with the arrow keys. How cool is that? The MovieClip is totally separate from the code that controls it, and it’s being controlled by two separate blocks of code simultaneously!
I hope you’ve enjoyed this tutorial, and I hope I’ve helped spark your imagination as to the potential of using classes in your programming. Because this is truly just the tip of the iceberg! Happy coding!
Spread The Word
Related Articles
25 Responses to "Make your own reusable classes using Flash and AS3" 
|
said this on 26 Nov 2007 10:52:43 AM CST
Seems like there is an er
1120: A |
|
said this on 26 Nov 2007 8:39:54 PM CST
I also wrote:
"In t C |
|
said this on 24 Jan 2008 6:46:37 PM CST
I've converted my sha
|
|
said this on 31 Jan 2008 1:11:41 PM CST
Drop a square convert to
in lower left make sure instanc |
|
said this on 12 Feb 2008 4:16:52 PM CST
The reason people are hav
var c The variable n To fix: |
|
said this on 17 May 2008 8:59:46 PM CST
Be sure to give your movi
|
|
said this on 16 Sep 2008 1:05:34 PM CST
Great tutorial.
Is it //something like this import com.mysite var // and dragIt(myClip); dragIt(myClip2); //etc. Or how could I do th Thanks |
|
said this on 13 Nov 2008 11:54:49 PM CST
Wow, I have been struggli
|
|
said this on 18 Nov 2008 8:23:00 PM CST
I feel like I should be p
I ha This ex Which els Not to mention you Very w |
|
said this on 24 Nov 2008 4:11:07 PM CST
I must say that this is t
Best regards / |
|
said this on 11 Dec 2008 9:28:28 PM CST
Thanks for this great tut
|
|
said this on 23 Jan 2009 4:21:08 AM CST
I agree, best as3 classes
|
|
said this on 31 Jan 2009 9:27:39 AM CST
Great stuu. thanks for th
|
|
said this on 19 Feb 2009 3:06:57 AM CST
Thanks for this great tut
|
|
said this on 18 Mar 2009 3:37:35 AM CST
Awesome, thank you for yo
Mo |
|
said this on 25 Apr 2009 1:22:40 PM CST
Great tutorial. As other
|
|
said this on 04 May 2009 12:15:40 PM CST
The best tutorial on how
|
|
said this on 06 May 2009 4:57:57 PM CST
Im somewhat puzzled by yo
|
|
said this on 29 Jun 2009 10:11:11 PM CST
Joeri,
Well, with this I made t I've also admitted I appreciated your |
|
said this on 12 May 2009 10:07:14 AM CST
"Besides, if you’ve never
Dear This could not Alix |
|
said this on 08 Jun 2009 9:15:36 PM CST
Thank you, THANK YOU,
|
|
said this on 29 Jun 2009 9:36:08 PM CST
Merci Jody,
I have been |
|
said this on 10 Jul 2009 9:32:15 AM CST
great tutorial.
really h thanks. |
|
said this on 05 Aug 2009 3:18:17 AM CST
Great introduction to cre
Righ |
|
said this on 25 Sep 2009 8:12:30 PM CST
Hey Jordy Thank You so mu
|


Author/Admin)