我们每天都面临着选择与被选择的问题,但选择又有诸多限制,在这里做了个模型。有四个店,每个店最多容纳5个客户,有的客户只能进一个特定的店,有的客户能进两个店,但是不管客户是谁,只要店里人满了,即不能再被拖入,实现效果:
实现代码:
SelectDrag类:
1 package 2 { 3 import flash.display.DisplayObject; 4 import flash.display.Sprite; 5 import flash.events.MouseEvent; 6 7 [SWF(width="1200", height="800", frameRate="20")] 8 public class SelectDrag extends Sprite 9 { 10 public var cir:circle; 11 public var rec:Rect; 12 public var sto:storeState; 13 public var oldX:int; 14 public var oldY:int; 15 public var mov:move = new move(); 16 public var voi:VoicePlay = new VoicePlay(); 17 public var recArr:Array=new Array(); 18 public var stoArr:Array=new Array(); 19 public var errArr:Array=new Array(); 20 public var recName:Array=[1,3,2,3]; 21 public var numArr:Array=[0,0,0,0]; 22 public var countTol:int = 0; 23 24 public function SelectDrag() 25 { 26 init(); 27 } 28 29 public function init():void 30 { 31 createState(4); 32 createRect(4); 33 createCircle(40); 34 } 35 36 public function createCircle(num:int):void 37 { 38 39 for(var i:int = 0; i<40;i++) 40 { 41 if (i<10) 42 { 43 cir = new circle(1); 44 cir.x = 50 + Math.round(Math.random() * 900); 45 cir.y = 50 + Math.round(Math.random() * 400); 46 cir.buttonMode = true; 47 cir.name="1"; 48 cir.addEventListener(MouseEvent.MOUSE_DOWN,onDown); 49 cir.addEventListener(MouseEvent.MOUSE_UP,onUp); 50 addChild(cir); 51 } 52 else if(i>9 && i<20) 53 { 54 cir = new circle(2); 55 cir.x = 50 + Math.round(Math.random() * 900); 56 cir.y = 50 + Math.round(Math.random() * 400); 57 cir.buttonMode = true; 58 cir.name="2"; 59 cir.addEventListener(MouseEvent.MOUSE_DOWN,onDown); 60 cir.addEventListener(MouseEvent.MOUSE_UP,onUp); 61 62 addChild(cir); 63 } 64 else 65 { 66 cir = new circle(3); 67 cir.x = 50 + Math.round(Math.random() * 900); 68 cir.y = 50 + Math.round(Math.random() * 400); 69 cir.buttonMode = true; 70 cir.name="3"; 71 cir.addEventListener(MouseEvent.MOUSE_DOWN,onDown); 72 cir.addEventListener(MouseEvent.MOUSE_UP,onUp); 73 74 addChild(cir); 75 } 76 77 } 78 } 79 80 81 public function createState(num:int):void 82 { 83 for(var j:int=0;j4)150 {151 mov.MoveCall(spr,oldX,oldY,mouseX,mouseY);152 voi.init(3);153 break ;154 }155 if (numArr[0] == 4)156 {157 countTol++;158 stoArr[0].visible = false;159 errArr[0].visible = false;160 e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);161 e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,onUp);162 voi.init(1);163 164 }165 else166 {167 stoArr[0].visible = true;168 e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);169 e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,onUp);170 voi.init(1);171 }172 numArr[0]++;173 174 175 break;176 case 1:177 178 if (numArr[1] > 4)179 {180 mov.MoveCall(spr,oldX,oldY,mouseX,mouseY);181 voi.init(3);182 break ;183 }184 if (numArr[1] == 4)185 {186 countTol++;187 stoArr[1].visible = false;188 errArr[1].visible = false;189 e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);190 e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,onUp);191 voi.init(1);192 }193 else194 {195 stoArr[1].visible = true;196 e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);197 e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,onUp);198 voi.init(1);199 }200 201 numArr[1]++;202 break;203 case 2:204 205 if (numArr[2] > 4)206 {207 mov.MoveCall(spr,oldX,oldY,mouseX,mouseY);208 voi.init(3);209 break ;210 }211 if (numArr[2] == 4)212 {213 countTol++;214 stoArr[2].visible = false;215 errArr[2].visible = false;216 e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);217 e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,onUp);218 voi.init(1);219 220 }221 else222 {223 stoArr[2].visible = true;224 e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);225 e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,onUp);226 voi.init(1);227 }228 numArr[2]++;229 230 break;231 case 3:232 if (numArr[3] > 4)233 {234 mov.MoveCall(spr,oldX,oldY,mouseX,mouseY);235 voi.init(3);236 break ;237 }238 if (numArr[3] == 4)239 {240 countTol++;241 stoArr[3].visible = false;242 errArr[3].visible = false;243 e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);244 e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,onUp);245 voi.init(1);246 }247 else248 {249 e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);250 e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,onUp);251 stoArr[m].visible = true;252 voi.init(1);253 }254 255 numArr[3]++;256 257 break;258 259 }260 261 if (countTol == 4)262 {263 trace("游戏结束!!!");264 var suc:SuccessOver = new SuccessOver();265 suc.x = 400;266 suc.y = 200;267 voi.init(2);268 addChild(suc);269 }270 }271 else272 {273 mov.MoveCall(spr,oldX,oldY,mouseX,mouseY);274 stoArr[m].visible = false;275 voi.init(3);276 return ;277 }278 279 }280 }281 282 if(judge)283 {284 mov.MoveCall(spr,oldX,oldY,mouseX,mouseY);285 voi.init(3);286 }287 288 289 }290 291 292 }293 }
小球实现类,circle:
package { import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; public class circle extends Sprite { private var xzuobiao:uint; private var yzuobiao:uint; private var spr:Sprite = new Sprite(); public function circle(num:uint) { init(num); } private function init(num:uint):void { spr.graphics.beginFill(0xffffff*Math.random()); spr.graphics.lineStyle(1);//设置线条粗细 spr.graphics.drawCircle(0,0,20); spr.graphics.endFill(); //设置数字 var txt_format:TextFormat=new TextFormat(); txt_format.size=30; var txt_num:TextField=new TextField(); txt_num.selectable=false;//设置文本字段不可选 txt_num.text=""+num; txt_num.width=40; txt_num.height=40; txt_num.x=-20; txt_num.y=-18; txt_num.setTextFormat(txt_format); txt_num.autoSize=TextFieldAutoSize.CENTER; spr.addChild(txt_num); addChild(spr); } } }
门店实现类,Rect:
package { import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; public class Rect extends Sprite { private var xzuobiao:uint; private var yzuobiao:uint; public function Rect(num:uint) { init(num); } private function init(num:uint) { this.graphics.beginFill(0xffffff*Math.random()); this.graphics.lineStyle(1);//设置线条粗细 this.graphics.drawRect(0,0,200,200); this.graphics.endFill(); //设置数字 var txt_format:TextFormat=new TextFormat(); txt_format.size=30; var txt_num:TextField=new TextField(); txt_num.selectable=false;//设置文本字段不可选 txt_num.text=""+num; txt_num.width=40; txt_num.height=40; txt_num.x=0; txt_num.y=0; txt_num.setTextFormat(txt_format); txt_num.autoSize=TextFieldAutoSize.CENTER; this.addChild(txt_num); } } }
门店状态类,storeState:
package{ import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; public class storeState extends Sprite { public function storeState(count:uint) { init(count); } private function init(cou:uint) { this.graphics.beginFill(0xffffff*Math.random()); this.graphics.lineStyle(1);//设置线条粗细 this.graphics.drawRect(0,0,200,40); this.graphics.endFill(); //设置数字 var txt_format:TextFormat=new TextFormat(); txt_format.font="黑体"; txt_format.size=40; var txt_num:TextField=new TextField(); txt_num.selectable=false;//设置文本字段不可选 if(cou==0) { txt_num.text="欢迎光临"; } else if(cou==1) { txt_num.text="本店客满"; } else { txt_num.text="门票不对"; } txt_num.width=40; txt_num.height=40; txt_num.x=85; txt_num.y=0; txt_num.setTextFormat(txt_format); txt_num.autoSize=TextFieldAutoSize.CENTER; this.addChild(txt_num); } }}
声音播放类,VoicePlay:
package{ import flash.display.Sprite; import flash.media.Sound; import flash.net.URLRequest; public class VoicePlay extends Sprite { public var arr:Array = ['voice/misstone.mp3','voice/righttone.mp3','voice/great.mp3','voice/move.mp3']; public function VoicePlay() { } public function init(count:int):void { var snd:Sound = new Sound(new URLRequest(arr[count])); snd.play(); } }}
拖拽缓动类,move:
package{ import flash.display.DisplayObject; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.net.URLLoader; import flash.net.URLRequest; import flash.utils.getTimer; [SWF(width="1600",height="800",frameRate="60")] public class move extends Sprite { private var obj:Object; private var t:Number; private var start:Object; // 起点的属性 集合 private var to:Object; // 终点的属性集合 private var startMove:Boolean; private var recordTime:Number; // 记录起始的时间点 public function move() { addEventListener(Event.ENTER_FRAME,onFrame); } /** * 当点击的时候 * @param e * */ public function MoveCall(spr:Sprite,oldX:int,oldY:int,newX:int,newY:int):void { var wid:int = spr.width; var hei:int = spr.height; moveTo(spr,{ start:{x:newX,y:newY,width:wid,height:wid}, to:{x:oldX,y:oldY,width:wid,height:wid} },500); } /** * 运动开始 * @param obj 谁要运动 * @param mp 起点状态和终点状态 * @param t 总花费的时间 ms * */ private function moveTo(obj:DisplayObject,mp:Object,t:Number):void{ this.obj = obj; this.t = t; this.start = mp.start; this.to = mp.to; startMove = true; recordTime = getTimer(); for (var key:String in start){ obj[key] = start[key]; } } /** * 用于计算时间点我的状态并设置当前的状态 * @param e * */ private function onFrame(e:Event):void{ if(!startMove)return; var nowTime:Number = getTimer(); var dTime:Number = nowTime - recordTime; // 表示我的移动抵达了终点,不再移动了。 // 过程 var per:Number = dTime/t; if(per>=1){ startMove = false; per=1; } for (var key:String in to){ var dx:Number = to[key] - start[key]; obj[key] = dx*per + start[key]; } } }}
结束提示类,SuccessOver:
package{ import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.net.URLRequest; [SWF(width="1200", height="800", frameRate="20")] public class SuccessOver extends Sprite { public function SuccessOver() { var loader:Loader = new Loader(); loader.load(new URLRequest("file/001.swf")); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function():void { addChild(loader); }); } }}
编程资源百度网盘:https://pan.baidu.com/s/1i5pGCE5#list/path=%2F 密码:4vyn