以一个小球为示例,在鼠标点击的时修改它的加速度值(在短时间内移动的距离),加速度以一个值加速,在遇到边界时进行反向。
代码参考自:《ActionScript 3.0 动画教程》,添加了拖动时的范围限定<在整个文档内进行拖动>。
Ball.as
1: package 2: { 3: import flash.display.Sprite; 4: 5: /** 6: * ... 7: * @author ... 8: */ 9: public class Ball extends Sprite 10: { 11: public var radius:Number; 12: private var color:uint; 13: public var vx:Number; 14: public var vy:Number; 15: 16: public function Ball(radius:Number=40, color:uint=0xff0000) 17: { 18: this.radius = radius; 19: this.color = color; 20: 21: init(); 22: } 23: 24: private function init():void { 25: graphics.clear(); 26: graphics.beginFill(color); 27: graphics.drawCircle(0, 0, radius); 28: graphics.endFill(); 29: } 30: 31: } 32: 33: }
Throwing.as
1: package 2: { 3: import flash.accessibility.Accessibility; 4: import flash.display.Sprite; 5: import flash.display.Stage; 6: import flash.display.StageAlign; 7: import flash.display.StageScaleMode; 8: import flash.events.Event; 9: import flash.events.MouseEvent; 10: import flash.geom.Rectangle; 11: 12: /** 13: * ... 14: * @author ... 15: */ 16: public class Throwing extends Sprite 17: { 18: 19: private var ball:Ball; 20: private var vx:Number; 21: private var vy:Number; 22: private var bounce:Number = -0.7; 23: private var gravity:Number = .5; 24: private var oldX:Number; 25: private var oldY:Number; 26: 27: public function Throwing() 28: { 29: init(); 30: } 31: 32: private function init():void { 33: stage.scaleMode = StageScaleMode.NO_SCALE; 34: stage.align = StageAlign.TOP_LEFT; 35: 36: ball = new Ball(); 37: ball.x = stage.stageWidth / 2; 38: ball.y = stage.stageHeight / 2; 39: vx = Math.random() * 10 - 5; 40: vy = -10; 41: 42: addChild(ball); 43: ball.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); 44: addEventListener(Event.ENTER_FRAME, onEnterFrame); 45: } 46: 47: private function onMouseDown(evt:MouseEvent):void { 48: oldX = ball.x; 49: oldY = ball.y; 50: 51: stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); 52: 53: ball.startDrag(false, new Rectangle(ball.radius, ball.radius,stage.stageWidth - ball.radius*2, stage.stageHeight - ball.radius*2)); 54: 55: removeEventListener(Event.ENTER_FRAME, onEnterFrame); 56: addEventListener(Event.ENTER_FRAME, trackVelocity); 57: } 58: 59: private function onMouseUp(evt:MouseEvent):void { 60: stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp); 61: ball.stopDrag(); 62: removeEventListener(Event.ENTER_FRAME, trackVelocity); 63: addEventListener(Event.ENTER_FRAME, onEnterFrame); 64: } 65: 66: private function trackVelocity(evt:Event):void { 67: vx = ball.x - oldX; 68: vy = ball.y - oldY; 69: 70: oldX = ball.x; 71: oldY = ball.y; 72: } 73: 74: private function onEnterFrame(evt:Event):void { 75: vx += gravity; 76: ball.x += vx; 77: ball.y += vy; 78: var left:Number = 0; 79: var right:Number = stage.stageWidth; 80: var top:Number = 0; 81: var bottom:Number = stage.stageHeight; 82: 83: if (ball.x + ball.radius > right) { 84: ball.x = right - ball.radius; 85: vx *= bounce; 86: } else if (ball.x - ball.radius < left) { 87: ball.x = left + ball.radius; 88: vx *= bounce; 89: } 90: 91: if (ball.y + ball.radius > bottom) { 92: ball.y = bottom - ball.radius; 93: vy *= bounce; 94: } else if (ball.y - ball.radius < top) { 95: ball.y = top + ball.radius; 96: vy *= bounce; 97: } 98: 99: } 100: 101: } 102: 103: }