{"id":94,"date":"2008-11-15T00:00:30","date_gmt":"2008-11-15T07:00:30","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=156"},"modified":"2008-11-15T00:00:30","modified_gmt":"2008-11-15T07:00:30","slug":"animate-along-a-path","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=94","title":{"rendered":"Animate Along a Path"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nstage.frameRate = 30;\r\nvar pencil:Shape = new Shape();\r\naddChild(pencil);\r\n\r\nvar index:int = 0;\r\nvar points:Array = new Array();\r\nvar circle:Shape = new Shape();\r\ncircle.graphics.beginFill(0x000000);\r\ncircle.graphics.drawCircle(0,0, 5);\r\naddChild(circle);\r\n\r\nstage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);\r\nstage.addEventListener(MouseEvent.MOUSE_UP, onUp);\r\n\r\ncircle.addEventListener(Event.ENTER_FRAME, onMoveCircle);\r\n\r\nfunction onDown(evt:MouseEvent):void {\r\n\tpencil.graphics.lineStyle(0,0x000000);\r\n\tpencil.graphics.moveTo(mouseX, mouseY);\r\n\tstage.addEventListener(MouseEvent.MOUSE_MOVE, onDraw);\r\n}\r\n\r\nfunction onUp(evt:MouseEvent):void {\r\n\tstage.removeEventListener(MouseEvent.MOUSE_MOVE, onDraw);\r\n}\r\n\r\nfunction onDraw(evt:Event):void {\r\n\tpencil.graphics.lineTo(mouseX, mouseY);\r\n\tpoints.push(new Point(mouseX, mouseY));\r\n}\r\n\r\nfunction onMoveCircle(evt:Event):void {\r\n\tif (points.length&gt;0) {\r\n\t\tcircle.x += (points&#x5B;index].x - circle.x) \/ 4;\r\n\t\tcircle.y += (points&#x5B;index].y - circle.y) \/ 4;\r\n\t\tindex++;\r\n\t\tif (index==points.length) {\r\n\t\t\tindex=0;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>Wrote this in response to a student question. It causes a circle shape to animate along lines drawn by the user.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>stage.frameRate = 30; var pencil:Shape = new Shape(); addChild(pencil); var index:int = 0; var points:Array = new Array(); var circle:Shape = new Shape(); circle.graphics.beginFill(0x000000); circle.graphics.drawCircle(0,0, 5); addChild(circle); stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown); stage.addEventListener(MouseEvent.MOUSE_UP, onUp); circle.addEventListener(Event.ENTER_FRAME, onMoveCircle); function onDown(evt:MouseEvent):void { pencil.graphics.lineStyle(0,0&#215;000000); pencil.graphics.moveTo(mouseX, mouseY); stage.addEventListener(MouseEvent.MOUSE_MOVE, onDraw); } function onUp(evt:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_MOVE, onDraw); } function onDraw(evt:Event):void { pencil.graphics.lineTo(mouseX, mouseY); points.push(new Point(mouseX, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,13],"tags":[3,4],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-arrays","category-motion","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=94"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}