/* function makePlayer, copyright 2004 Michael Kantor mkantor@mathlearning.net. You are hereby granted permission to use and/or modify this code for educational purposes. If you make any significant improvements, please share them with me. Contact the author for clearance for commercial use. This function creates a player to animate the drawing of a path stored in a text file. The text-file format is that produced by pathFinder: ________________________________ title=my drawing& &pathString= down 10 20 line 10 30 line 10 50 up 10 52 down 20 30 line 20 35 up 20 38& ________________________________ The function requires six parameters: makePlayer(clipName, sourceFile, penColor, showPen, speed, autoStart) clipName = "myDrawing" // name for movie clip to draw in sourceFile = "myFile.txt"// drawing source file penColor = 0xFF00DD // hex color of drawing showPen = true // show or hide pen speed = 30 // number of steps drawn per second, subject to machine speed autoStart = true // play drawing as soon as it's loaded Required resources: A movie clip in the library, exported for ActionScript with linkage "pen_mc." This movie clip should have its drawing end exactly at the registration point Optional resources (If the movie clip that calls the makePlayer function has these objects, they will be used): play_btn // play the drawing stop_btn // stop the drawing speed_ctrl // a slider to set the speed showPen_chk // a checkbox for showing or not showing the pen title_txt // a textfield to display the title of the current drawing */ function makePlayer(clipName, sourceFile, penColor, showPen, speed, autoStart) { relRoot = this; relRoot.play_btn.enabled = false; // don't allow playing until drawing is loaded p = relRoot.createEmptyMovieClip(clipName, 100); // create the drawing canvas p.speed = speed; // advance this many steps per second p.penColor = penColor; p.showPen = showPen; p.sourceFile = sourceFile; // file to load drawing from relRoot.speed_ctrl.setValue(p.speed); // update controls relRoot.showPen_chk.setSelected(p.showPen); // update controls p.path = new Array(); // store the drawing path LV = new LoadVars(); // object to read from course file LV._parent = p; LV.onLoad = function(success) { // after loading, this function is called if (!success) { // if not successful... p.showError(sourceFile); // display error message return; // ...and quit } p.errorPanel.removeMovieClip(); for(var i in this) { // properties from file now belong to LV this._parent[i] = this[i]; // copy properties to the canvas } relRoot.title_txt.text = p.title; // display title var array1 = p.pathString.split("\r"); // split string into lines var len = array1.length; var o; for (var i = 0; i < len; i++) { o = array1[i]; // one line of the file o = o.substr(o.indexOf("\r") + 1); // drop leading return o = array1[i].split(" "); // split line at spaces p.path.push(o[0]); // put movement type into p.path p.path.push(Number(o[1])); // put coordinates into p.path p.path.push(Number(o[2])); } relRoot.play_btn.enabled = true; // allow playing if (autoStart) p.playDrawing(); // auto start drawing } LV.load(p.sourceFile); // load a path from a text file p.createEmptyMovieClip("tracer", 10); // clip that draws p.attachMovie("pen_mc", "pen", 20); // tip is at reference point! p.pen.showPen = p.showPen; p.drawing = true; // function to respond to changes in "Show Pen" and "Speed" controls p.adjustDrawing = function() { clearInterval(p.int); // stop drawing process var tmp = relRoot.speed_ctrl.value; // update speed if (typeof tmp != "undefined") p.speed = tmp; tmp = relRoot.showPen_chk.selected; // update pen visibility if (typeof tmp != "undefined") p.pen.showPen = tmp; p.int = setInterval(p.drawNext, 1000 / p.speed); // start drawing process } relRoot.speed_ctrl.change = p.adjustDrawing; // watch for changes relRoot.showPen_chk.change = p.adjustDrawing; // Button actions p.stopDrawing = function() { clearInterval(p.int); }; relRoot.stop_btn.onRelease = p.stopDrawing; p.playDrawing = function() { clearInterval(p.int); // stop any existing drawing process p.tracer.clear(); // clear the screen p.tracer.lineStyle(3, p.penColor, 50); // set drawing style p.drawCounter = 0; // start at beginning p.int = setInterval(p.drawNext, 1000 / p.speed); // start a drawing process } relRoot.play_btn.onRelease = p.playDrawing; // function to draw one segment of the path // this function gets called p.speed times per second p.drawNext = function() { var n = p.path[p.drawCounter++]; // get next entry if (typeof n == "undefined") { // if end of path ... clearInterval(p.int); p.pen._visible = false; return; // ...then quit } var x = p.path[p.drawCounter++]; // get next two numbers var y = p.path[p.drawCounter++]; if (n.indexOf("down") != -1) { // on mouseDown, move to new spot p.tracer.moveTo(x, y); } p.pen._x = x; // bring pen along p.pen._y = y; p.pen._visible = p.pen.showPen; p.tracer.lineTo(x, y); // draw line segment updateAfterEvent(); // update screen display } p.showError = function(sourceFile) { var e = p.createEmptyMovieClip("errorPanel", 2032); e.lineStyle(3, 0, 100); e.beginFill(0x888888, 50); e.moveTo(-100, -100); e.lineTo(100, -100); e.lineTo(100, 100); e.lineTo(-100, 100); e.lineTo(-100, -100); e.endFill(); e._x = Stage.width / 2; e._y = Stage.height / 2; e.createTextField("error_txt", 100, -70, -70, 140, 140); var t = e.error_txt; t.multiline = t.wordWrap = t.border = true; t.text = "Cannot load drawing from file " + sourceFile; // indicate error e._visible = true; } } // end of function makePlayer