[Actionscript]互动小游戏Demo
写了一个Demo,可能最近的一个项目需要用到,要实现的功能是用鼠标拨开上面的小球来让下面的物品呈现出来。
Demo演示如下:
http://www.kxbd.com/article.asp?id=245
文档类:
Ball类:
Demo演示如下:
http://www.kxbd.com/article.asp?id=245
文档类:
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 }
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类:
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
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