[Actionscript]互动小游戏Demo

写了一个Demo,可能最近的一个项目需要用到,要实现的功能是用鼠标拨开上面的小球来让下面的物品呈现出来。

Demo演示如下:
http://www.kxbd.com/article.asp?id=245

文档类:
ContractedBlock.gif ExpandedBlockStart.gif Code
 1 package  {
 2     
 3     import flash.events.Event;
 4     import flash.events.MouseEvent;
 5     import flash.display.*;
 6     import gs.TweenLite;
 7     
 8     public class test002 extends MovieClip {
 9         private static const numCol:uint = 23;
10         private static const numRow:uint = 17;
11         private static const ballAll:uint= numCol * numRow;
12         private static const ballWidth:uint = 26;
13         private static const ballHeight:uint = 26;
14         private static const ballSetTop:int = 0;
15         private static const ballSetLeft:int = 0;
16         private static const isMoveDis:uint = 50;
17         private static const moveDis:uint = 50;
18         private static const numLeft:uint = 10;
19         private var isDown:Boolean = false;
20         private var ballArr:Array = [];
21         private var ballHitsArr:Array = [];
22         private var box:Box;
23         private static const boxAlpha = 0.2;
24         
25         public function test002() {
26             box = new Box();
27             setBox();
28             addChild(box);
29             for (var x:uint = 0; x < numCol; x++) {
30                 for (var y:uint = 0; y < numRow; y++) {
31                     var thisBall:Ball = new Ball(x * ballWidth + ballSetLeft,y * ballHeight + ballSetTop);
32                     ballArr.push(thisBall);
33                     addChild(thisBall);
34                 }
35             }
36             
37             stage.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent) {
38                 isDown = true;
39                 ballArr.forEach(function(elem) {
40                     moveBall(elem, e.stageX, e.stageY);
41                 });
42             } );
43             
44             stage.addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent) {
45                 isDown = false;
46                 ballArr.forEach(function(elem) {
47                     TweenLite.to(elem, 1, {x:elem.oldX,y:elem.oldY } );
48                 } );
49             } );
50             
51             stage.addEventListener(MouseEvent.MOUSE_MOVE, function(e:MouseEvent) {
52                 if (isDown) {
53                     ballArr.forEach(function(elem) {
54                         moveBall(elem, e.stageX, e.stageY);
55                     });
56                 }
57             } );
58             addEventListener(Event.ENTER_FRAME, checkHits);
59             
60         }
61         
62         private function moveBall(mc:MovieClip,eX:Number,eY:Number):void {
63             var elX:Number = mc.x;
64             var elY:Number = mc.y;
65             var disX:Number = eX - elX;
66             var disY:Number = eY - elY;
67             var dis:Number = Math.sqrt(disX * disX + disY * disY);
68             if (dis < isMoveDis) {
69                 var randomX:Number = (disX > 0? -1:1* Math.random() * moveDis + elX;
70                 var randomY:Number = (disY > 0? -1:1* Math.random() * moveDis + elY;
71                 TweenLite.to(mc, 0.5, {x:randomX,y:randomY } );
72             }
73         }
74         
75         private function checkHits(e:Event) {
76             var hitNum:uint = 0;
77             ballArr.forEach(function(elem) {
78                 if (elem.hitTestObject(box)) {
79                     hitNum++;
80                 }
81             });
82             if (hitNum <= numLeft) {
83                 box.alpha = 1;
84                 gotoAndStop(2);
85             }
86         }
87         
88         public function setBox() {
89             box.x = Math.random() * (stage.stageWidth-box.width);
90             box.y = Math.random() * (stage.stageHeight-box.height);
91             box.alpha = boxAlpha;
92         }
93     }
94     
95 }

Ball类:
ContractedBlock.gif ExpandedBlockStart.gif Code
 1 package
 2 {
 3     import flash.display.*;
 4     
 5     public class Ball extends MovieClip
 6     {
 7         private var _oldX:Number;
 8         private var _oldY:Number;
 9         
10         public function Ball(x:Number,y:Number) {
11             _oldX = this.x = x;
12             _oldY = this.y = y;
13         }
14         
15         public function get oldX():Number {
16             return _oldX;
17         }
18         
19         public function get oldY():Number {
20             return _oldY;
21         }
22     }
23 }
24 

转载于:https://www.cnblogs.com/noidea/archive/2009/01/02/1367262.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值