前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >as3 模拟“抛”的动作

as3 模拟“抛”的动作

作者头像
meteoric
发布2018-11-15 17:05:24
3700
发布2018-11-15 17:05:24
举报
文章被收录于专栏:游戏杂谈游戏杂谈

以一个小球为示例,在鼠标点击的时修改它的加速度值(在短时间内移动的距离),加速度以一个值加速,在遇到边界时进行反向。

代码参考自:《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: }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2011-03-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档