Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
package { import flash.display.*; import flash.text.*; import flash.events.*; public class MyAnimation extends MovieClip { // Properties var myCircle:Sprite; var moveX:Number = 10; var moveY:Number = 10; var pausedMovie:Boolean = false; var oldX:Number; var oldY:Number; var friction:Number = .9; var gravity:Number = .5; // Constructor public function MyAnimation() { myCircle = new Sprite(); myCircle.graphics.lineStyle(5,0x000000); myCircle.graphics.beginFill(0xCCCCCC); myCircle.graphics.drawCircle(0,0,25); addChild(myCircle); myCircle.x = 100; myCircle.y = 150; addEventListener(Event.ENTER_FRAME, moveCircle); myButton.addEventListener(MouseEvent.CLICK, stopOnEveryFrame); myCircle.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); } public function startDragging(e:MouseEvent) { stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging); removeEventListener(Event.ENTER_FRAME, moveCircle); myCircle.startDrag(); addEventListener(Event.ENTER_FRAME, trackVelocity); } //startDragging public function stopDragging(e:MouseEvent) { stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging); myCircle.stopDrag(); addEventListener(Event.ENTER_FRAME, moveCircle); removeEventListener(Event.ENTER_FRAME, trackVelocity); } //stopDragging public function trackVelocity(e:Event){ moveX = myCircle.x - oldX; moveY = myCircle.y - oldY; oldX = myCircle.x; oldY = myCircle.y; } public function stopOnEveryFrame(e:MouseEvent) { if(pausedMovie){ addEventListener(Event.ENTER_FRAME, moveCircle); pausedMovie = false; } else { removeEventListener(Event.ENTER_FRAME, moveCircle); pausedMovie = true; } } // Methods public function moveCircle(e:Event):void{ moveY = moveY + gravity; if(myCircle.x > stage.stageWidth || myCircle.x < 0){ moveX = -moveX*friction; //change direction } if(myCircle.y > stage.stageHeight || myCircle.y < 0){ moveY = -moveY*friction; //change direction } myCircle.x = myCircle.x + moveX; myCircle.y = myCircle.y + moveY*gravity; } } }
This paste will be private.
From the Design Piracy series on my blog: