First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing!
Search tutorials
Author: Jesse

Step 1: Letting the user draw

First thing's first. Make a new Flash document and call it drawing AI. Then, type the following code in the actions panel.

lines = {}
_root.createEmptyMovieClip("pen", 10)
pen.lineStyle(1, 000000, 100)

This code simply stops the move and creates an object called lines. Then, it creates a movie clip called pen and sets it up so it can draw with the lineStyle method. Now, let's look at the code that lets you draw.

_root.onMouseDown = function(){
	drawing = true
	pen.moveTo(_xmouse, _ymouse)
	segments += 1
	name = "line"+segments
	lines[name] = ["break", _xmouse, _ymouse]
_root.onMouseMove = function(){
		pen.lineTo(_xmouse, _ymouse)
		segments += 1
		name = "line"+segments
		lines[name] = [_xmouse, _ymouse]
_root.onMouseUp = function(){
	drawing = false

This is alot of code, but it's pretty simple. It makes it so that when you click the movie, drawing is set to true ans then pen moves to the mouse's x and y positions. Then it adds one to the segments variable, and creates an array in the lines object, containing the string "break", the x position of the mouse, and the y position of the mouse. This code makes sure that when the drawing is being re-drawn by the computer, it knows to take a break so it can move to the new position, instead of constantly drawing lines. Then, if the mouse is moving, the game checks to see if drawing is true. If it is, the pen movie clip draws a line to the mouse, and adds one to the segments variable. We then create an array in lines containing the mouse's position, but we do not include "break" so the computer knows we are only drawing a line. Finally, when the mouse is released, drawing is set to false. Now, create a movie clip with a red circle and a black text field saying "ok". Put this in the first frame and type the following code into the actions panel for the ok movie clip.


What this code does is it simply sends the root timeline to the sixth frame when the movie clip is pressed.

» Level Intermediate

Added: 2004-12-24
Rating: 8 Votes: 7
(10 being the highest)
» Author
Just started working with Flash.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!


  • 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.