A Simple Start: Interactivity with Flash

This tutorial is incredibly basic. If you don't know anything about ActionScript or programming in general, than this is probably the tutorial for you.

Alright, we're going to just make a simple circle that moves around when the user presses the arrow keys. Sound good?
Alright, first, draw the circle. (Not too difficult). Now, select it, right click, and convert it to symbol. Make sure that "Movie Clip" is selected for type. Name it "ball" and press okay.
Yay! You have a symbol! Now, click on the symbol and go to the properties tab on the bottom of the screen. Under "Instance name", put "ball". This is very important, or else actionscript won't be able to reference your symbol.

Now for the code. In the actions tab, put the following code.

Code:
  1. ball.onEnterFrame = function() {
  2. if (Key.isDown(Key.UP)) {
  3. this._y--;
  4. }
  5. if (Key.isDown(Key.DOWN)) {
  6. this._y++;
  7. }
  8. if (Key.isDown(Key.RIGHT)) {
  9. this._x++;
  10. }
  11. if (Key.isDown(Key.LEFT)) {
  12. this._x--;
  13. }
  14. }


See what we did? We started out by saying "ball.onEnterFrame = function()". What this does is creates a function that runs every frame. (The onEnterFrame function automatically executes every frame).
Next, for each key: up, down, left, or right, we run an if statement to check if that key is down. ("if (Key.isDown(Key.UP)), etc."). If it is, than we move the ball's x and y coordinates appropriately. (The term "this._x--" is subtracting one from the x value of the ball.

Press control+enter to see your movie. You should have this:


Stay tuned!

Search