Extended: Drawing with actionscript
4,298 ViewsFlash Tutorials March 21st, 2007
Hi, this is an extension to drawing with actionscript. In this tutorial I will be teaching you how to add user-controlled movement to your as-drawn movieclip.
Ok, first thing to do is open up the actionsc panel (and do not close it). Now underneath the code that you wrote in the previous tutorial add this line of code:
-
onEnterFrame = function() {
All that does is defines a function, so when the movie enters this particular frame, it calls the function and runs the code in the brace ( { ).
Now we need to allow the movement, so add this code to the actions:
-
if(Key.isDown(Key.LEFT)){
-
square._x -= 5;
-
}
All this does is checks if the left arrow key is down, and if it is, make the object with the instance name of square (the object we drew in as) to move 5px to the left.
Now we repeat that code but changing Key.LEFT to Key.RIGHT, Key.DOWN and Key.Up and we change square._x -= 5 to: square._x += 5;, square._y -= 5; and square._y += 5; :
-
if(Key.isDown(Key.DOWN)){
-
square._y += 5;
-
}
-
if(Key.isDown(Key.UP)){
-
square._y -= 5;
-
}
-
if(Key.isDown(Key.RIGHT)){
-
square._x += 5;
-
}
and now we close the function defination:
-
};
That concludes the tutorial. Just to recap:
The full code from this tutorial:
-
onEnterFrame = function() {
-
if(Key.isDown(Key.LEFT)){
-
square._x -= 5;
-
}
-
if(Key.isDown(Key.DOWN)){
-
square._y += 5;
-
}
-
if(Key.isDown(Key.UP)){
-
square._y -= 5;
-
}
-
if(Key.isDown(Key.RIGHT)){
-
square._x += 5;
-
}
-
};
and the full code from this tutorial and the previous one is:
-
stop();
-
-
_root.createEmptyMovieClip("square", square.getNextHighestDepth());
-
square.beginFill(000000, 100);
-
square.lineStyle(4, 333333, 100);
-
square.moveTo(0, 265);
-
square.lineTo(0, 265);
-
square.lineTo(20, 265);
-
square.lineTo(20, 285);
-
square.lineTo(0, 285);
-
-
onEnterFrame = function() {
-
if(Key.isDown(Key.LEFT)){
-
square._x -= 5;
-
}
-
if(Key.isDown(Key.DOWN)){
-
square._y += 5;
-
}
-
if(Key.isDown(Key.UP)){
-
square._y -= 5;
-
}
-
if(Key.isDown(Key.RIGHT)){
-
square._x += 5;
-
}
-
};
Thanks for reading,
mortisimus
http://www.flashdoom.net












April 16th, 2007 at 3:17 am
Lovely guide, along with the first one. Thanks
April 16th, 2007 at 9:12 pm
Your very welcome, TutorialCode.com is just venturing out into the flash world and getting our feet wet.
Stick around and we will (hopefully) have more flash tutorials on the way!
April 24th, 2007 at 3:00 pm
this was very helpfull thanks for shareing
May 1st, 2007 at 2:20 am
no problem