唔哇哈哈,拉霸机
demo: www.jd85.net/labaji.html
最近做转职系统,需要一个抽奖拉霸机来随机获取职业相关的物品。这里是不是在快速运动中直接停下,而是在做减速运动。不过没有用到神马恶心的物理变加速运动公式,原理其实就两个容器来回切,然后逐帧减速最后停下。
主要靠状态来区分,状态,微妙的状态~~
1 package
2 {
3 import flash.display.MovieClip;
4 import flash.display.SimpleButton;
5 import flash.display.Sprite;
6 import flash.events.Event;
7 import flash.events.MouseEvent;
8
9 /**
10 * 从当前格子走到目标格子的走法
11 *
12 * 条件:
13 * 1,确保从当前格子走到目标格子中间的格子数不得低于LOWEST_ITEM_COUNT,不能出现急撒车;
14 * 2,确保在LOWEST_ITEM_COUNT这最后几个格子的运动过程中是在减速运动;
15 * 3,确保停在目标格子时的速度不得低于LOWEST_SPEED
16 *
17 * 收到目标格子的位置后就走到状态f
18 *
19 *
20 * 状态
21 * a: idle
22 * b: running
23 * c:就在这一圈内判断当到达目标item时停下 ,判断剩下要走的距离低于LOWEST_ITEM_COUNT时这时开始减速
24 * d: 走完这一圈,然后切到c,判断剩下要走的距离低于LOWEST_ITEM_COUNT时这时开始减速
25 * e: 走完这一圈,然后切到d,判断剩下要走的距离低于LOWEST_ITEM_COUNT时这时开始减速
26 * f: 走完这一圈,然后切到e,判断剩下要走的距离低于LOWEST_ITEM_COUNT时这时开始减速
27 *
28 *
29 * @author ding.jia
30 *
31 */
32 public class RandomAwardMachine extends Sprite
33 {
34 //--------------------------------------------------------------------------
35 // Constructor
36 //--------------------------------------------------------------------------
37 public function RandomAwardMachine()
38 {
39 init();
40 }
41
42 //--------------------------------------------------------------------------
43 // Variables
44 //--------------------------------------------------------------------------
45
46 //--------------------------------------------------------------------------
47 // Propertise
48 //--------------------------------------------------------------------------
49 private const ICON_WIDTH:int = 120;
50 private const ICON_HEIGHT:int = 113;
51 private const VERTICAL_GAP:int = 5;
52
53 private const STATUS_A:String = "a";
54 private const STATUS_B:String = "b";
55 private const STATUS_C:String = "c";
56 private const STATUS_D:String = "d";
57 private const STATUS_E:String = "e";
58 private const STATUS_F:String = "f";
59
60 // private const LOWEST_ITEM_COUNT:int = 28;
61 // private const LOWEST_SPEED:Number = 2;
62 // private const SPEED_subtract:Number = 1;
63
64 private const LOWEST_ITEM_COUNT:int = 17;
65 private const LOWEST_SPEED:Number = 2;
66 private const SPEED_subtract:Number = 1;
67
68 private var crtStatus:String; //当前机器运行状态
69 private var speed:Number;
70 private var iconList:Array; //所有要显示出的icon列表
71 private var iconListLength:int;
72 private var bgAsset:AwardBg;
73 private var bg:MovieClip; //背景
74 private var btn:SimpleButton;
75 private var containerMask:Sprite; //两个icon容器所在容器的遮罩
76 private var iconContainerA:Sprite; //第一张icon容器
77 private var iconContainerB:Sprite; //第二张icon容器
78 private var crtBottomIconContainer:Sprite; //当前处在下方的icon容器
79 private var container:Sprite; //上下两个icon所在的容器
80 private var crtLatestIconIndex:int; //当前最新的icon的数组索引值
81 private var targetIconIndex:int;
82
83 //--------------------------------------------------------------------------
84 // Methods
85 //--------------------------------------------------------------------------
86 public function receiveTargat(index:int):void
87 {
88 targetIconIndex = index;
89 // crtStatus = STATUS_F;
90 crtStatus = STATUS_E;
91 }
92 //--------------------------------------------------------------------------
93 // Event Handler
94 //--------------------------------------------------------------------------
95 protected function btnClickHandler(event:MouseEvent):void
96 {
97 readyRun();
98 }
99
100 protected function efHandler(event:Event):void
101 {
102 switch(crtStatus)
103 {
104 case STATUS_B:
105 doStatusB();
106 break;
107 case STATUS_C:
108 doStatusC();
109 break;
110 case STATUS_D:
111 doStatusD();
112 break;
113 case STATUS_E:
114 doStatusE();
115 break;
116 // case STATUS_F:
117 // doStatusF();
118 break;
119 default:
120 break;
121 }
122 }
123
124 //--------------------------------------------------------------------------
125 // Private
126 //--------------------------------------------------------------------------
127 private function doStatusB():void
128 {
129 runUP();
130 }
131
132 //是否是走到了c段之后走到了目标,当由d切换到c时,可能正好就是在目标位置,但这时还未走到c段类
133 private var specialC:int;
134 private function doStatusC():void
135 {
136 //c段里目标点的距离
137 var c:int = Math.abs(crtLatestIconIndex - targetIconIndex);
138 if(c <= LOWEST_ITEM_COUNT)
139 {
140 slowDown();
141 }
142
143 if(crtLatestIconIndex != targetIconIndex)
144 {
145 specialC = 1;
146 }
147
148 //判断在这一圈内到达目标格子就停下
149 if(crtLatestIconIndex == targetIconIndex)
150 {
151 if(specialC == 1)
152 {
153 if(crtBottomIconContainer.y <= 3)
154 {
155 finished();
156 }
157 else
158 {
159 runUP();
160 }
161 }
162 else
163 {
164 runUP();
165 }
166 }
167 else
168 {
169 runUP();
170 }
171 }
172 private var finishRoundD:Boolean;
173 private function doStatusD():void
174 {
175 if(crtLatestIconIndex == iconListLength - 1)
176 {
177 if(finishRoundD)
178 {
179 finishRoundD = false;
180 crtStatus = STATUS_C;
181 }
182 else
183 {
184 runUP();
185 }
186 }
187 else
188 {
189 finishRoundD = true;
190 runUP();
191 }
192
193 //c段走到目标点的距离
194 var c:int = targetIconIndex;
195 //d段开始降速时到d端最后一张还剩下的距离
196 var dLong:int = LOWEST_ITEM_COUNT - c;
197 //求出d段减速点的索引
198 var dIndex:int = iconListLength - dLong;
199 if(crtLatestIconIndex >= dIndex)
200 {
201 slowDown();
202 }
203 }
204
205 //在状态e下是否走完了一整圈后才开始算到达最后一张
206 private var finishRoundE:Boolean;
207 private function doStatusE():void
208 {
209 // //先要知道减速点是否在e段
210 // var d:int = iconListLength;
211 // var c:int = targetIconIndex;
212 //
213 //
214 // //判断是否到达了减速点 e(未必满) + d(满) + c(未必满)
215 // var e:int = iconListLength - crtLatestIconIndex;
216 // var tempNum:int = e + d + c;
217 // if(tempNum <= LOWEST_ITEM_COUNT)
218 // {
219 // slowDown();
220 // }
221
222 if(crtLatestIconIndex == iconListLength - 1)
223 {
224 if(finishRoundE)
225 {
226 finishRoundE = false;
227 crtStatus = STATUS_D;
228 }
229 else
230 {
231 runUP();
232 }
233 }
234 else
235 {
236 finishRoundE = true;
237 runUP();
238 }
239
240 }
241
242 // private function doStatusF():void
243 // {
244 // //判断是否到达了减速点 e(满) + d(满) + c(未必满) + f(未必满)
245 // var e:int = iconListLength;
246 // var d:int = iconListLength;
247 // var c:int = iconListLength - targetIconIndex;
248 // var f:int = iconListLength - crtLatestIconIndex;
249 // var tempNum:int = e + d + c + f;
250 // if(tempNum <= LOWEST_ITEM_COUNT)
251 // {
252 // slowDown();
253 // }
254 //
255 // if(crtLatestIconIndex == iconListLength - 1) //判断是否走完了一圈
256 // {
257 // crtStatus = STATUS_E;
258 // }
259 // else
260 // {
261 // runUP();
262 // }
263 // }
264
265 private function slowDown():void
266 {
267 //第一次开始降速是的降速点索引,目标点索引,和当前状态
268 trace("crtLatestIconIndex:",crtLatestIconIndex,"targetIconIndex:",targetIconIndex,"crtstatus:",crtStatus);
269
270 speed -= SPEED_subtract;
271 if(speed <= LOWEST_SPEED)
272 {
273 speed = LOWEST_SPEED;
274 }
275 trace("speed:",speed);
276 }
277
278 private function finished():void
279 {
280 crtBottomIconContainer.y = 0;
281 removeEventListener(Event.ENTER_FRAME,efHandler);
282 crtStatus = STATUS_A;
283 specialC = 0;
284 finishRoundD = false;
285 finishRoundE = false;
286 speed = 65;
287 }
288
289 private function runUP():void
290 {
291 if(iconContainerA.y < iconContainerB.y)
292 {
293 if(iconContainerB.y <= 0)
294 {
295 iconContainerA.y = iconContainerB.y + ICON_HEIGHT;
296 crtBottomIconContainer = iconContainerA;
297 addNextIcon();
298 }
299 }
300 else if(iconContainerA.y > iconContainerB.y)
301 {
302 if(iconContainerA.y <= 0)
303 {
304 iconContainerB.y = iconContainerA.y + ICON_HEIGHT;
305 crtBottomIconContainer = iconContainerB;
306 addNextIcon();
307 }
308 }
309
310 iconContainerA.y -= speed;
311 iconContainerB.y -= speed;
312 }
313
314 private function addNextIcon():void
315 {
316 crtLatestIconIndex++;
317 if(crtLatestIconIndex > iconListLength - 1)
318 {
319 crtLatestIconIndex = 0;
320 }
321
322 if(crtBottomIconContainer.numChildren > 0)
323 {
324 crtBottomIconContainer.removeChildAt(0);
325 }
326 crtBottomIconContainer.addChild(iconList[crtLatestIconIndex]);
327 }
328
329 private function init():void
330 {
331 crtStatus = STATUS_A;
332 speed = 65;
333
334 setIconsAsset();
335 addBgAssst();
336 addContainer();
337 addContainerMask();
338 addIconContainers();
339
340 addChild(btn);
341
342 }
343
344 private function addContainer():void
345 {
346 container = new Sprite();
347 container.graphics.beginFill(0xff0000,0);
348 container.graphics.drawRect(0,0,bg.width,bg.height);
349 addChild(container);
350 }
351
352 private function addContainerMask():void
353 {
354 containerMask = new Sprite();
355 containerMask.graphics.beginFill(0x00ff33,0);
356 containerMask.graphics.drawRect(0,0,bg.width,bg.height - 8);
357 containerMask.y = 4;
358 addChild(containerMask);
359
360 // container.mask = containerMask;
361 }
362
363 /**
364 * 刚最后一张显示时,把第一张icon移动最下面
365 *
366 */
367 private function resetIconsPos():void
368 {
369
370 }
371
372 private function addIconContainers():void
373 {
374 iconContainerA = new Sprite();
375 iconContainerB = new Sprite();
376
377 iconContainerA.graphics.beginFill(0x000000,0);
378 iconContainerA.graphics.drawRect(0,0,bg.width,bg.height);
379 iconContainerA.x = 0;
380 iconContainerA.y = ICON_HEIGHT;
381 crtLatestIconIndex = 0;
382 iconContainerA.addChild(iconList[crtLatestIconIndex]);
383 container.addChild(iconContainerA);
384
385 iconContainerB.graphics.beginFill(0xffff00,0);
386 iconContainerB.graphics.drawRect(0,0,bg.width,bg.height);
387 iconContainerB.x = 0;
388 iconContainerB.y = ICON_HEIGHT * 2;
389 crtLatestIconIndex = 1;
390 iconContainerB.addChild(iconList[crtLatestIconIndex]);
391 container.addChild(iconContainerB);
392
393 crtBottomIconContainer = iconContainerB;
394 }
395
396 private function addBgAssst():void
397 {
398 bgAsset = new AwardBg();
399 addChild(bgAsset);
400 bg = bgAsset._bg;
401 btn = bgAsset._btn;
402 btn.addEventListener(MouseEvent.CLICK,btnClickHandler);
403 }
404
405 private function readyRun():void
406 {
407 if(crtStatus == STATUS_A)
408 {
409 crtStatus = STATUS_B;
410 addEventListener(Event.ENTER_FRAME,efHandler);
411 }
412 }
413
414 private function setIconsAsset():void
415 {
416 iconList = [];
417 var icon1:AwardIcon = new AwardIcon(new ICON_1(),AwardIconIndex.ICON_1);
418 var icon2:AwardIcon = new AwardIcon(new ICON_2(),AwardIconIndex.ICON_2);
419 var icon3:AwardIcon = new AwardIcon(new ICON_3(),AwardIconIndex.ICON_3);
420 var icon4:AwardIcon = new AwardIcon(new ICON_4(),AwardIconIndex.ICON_4);
421 var icon5:AwardIcon = new AwardIcon(new ICON_5(),AwardIconIndex.ICON_5);
422 var icon6:AwardIcon = new AwardIcon(new ICON_6(),AwardIconIndex.ICON_6);
423 var icon7:AwardIcon = new AwardIcon(new ICON_7(),AwardIconIndex.ICON_7);
424 var icon8:AwardIcon = new AwardIcon(new ICON_8(),AwardIconIndex.ICON_8);
425 var icon9:AwardIcon = new AwardIcon(new ICON_9(),AwardIconIndex.ICON_9);
426 var icon10:AwardIcon = new AwardIcon(new ICON_10(),AwardIconIndex.ICON_10);
427 var icon11:AwardIcon = new AwardIcon(new ICON_11(),AwardIconIndex.ICON_11);
428 var icon12:AwardIcon = new AwardIcon(new ICON_12(),AwardIconIndex.ICON_12);
429 var icon13:AwardIcon = new AwardIcon(new ICON_13(),AwardIconIndex.ICON_13);
430 var icon14:AwardIcon = new AwardIcon(new ICON_14(),AwardIconIndex.ICON_14);
431 var icon15:AwardIcon = new AwardIcon(new ICON_15(),AwardIconIndex.ICON_15);
432 var icon16:AwardIcon = new AwardIcon(new ICON_16(),AwardIconIndex.ICON_16);
433 var icon17:AwardIcon = new AwardIcon(new ICON_17(),AwardIconIndex.ICON_17);
434 var icon18:AwardIcon = new AwardIcon(new ICON_18(),AwardIconIndex.ICON_18);
435 var icon19:AwardIcon = new AwardIcon(new ICON_19(),AwardIconIndex.ICON_19);
436 var icon20:AwardIcon = new AwardIcon(new ICON_20(),AwardIconIndex.ICON_20);
437 var icon21:AwardIcon = new AwardIcon(new ICON_21(),AwardIconIndex.ICON_21);
438
439 iconList.push(icon1,icon2,icon3,
440 icon4,
441 icon5,
442 icon6,icon7,
443 icon8,icon9,
444 icon10,icon11,icon12,icon13,icon14,icon15,icon16,icon17,icon18,icon19,icon20,icon21);
445
446 iconListLength = iconList.length;
447
448 }
449
450 //--------------------------------------------------------------------------
451 // Dispose
452 //--------------------------------------------------------------------------
453 }
454 }
package
{
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.utils.Timer;
/**
*
* @author ding.jia
*
*/
[SWF(width="1200",height="1000",frameRate="24")]
public class Main extends Sprite
{
//--------------------------------------------------------------------------
// Constructor
//--------------------------------------------------------------------------
public function Main()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
machine = new RandomAwardMachine();
machine.x = 200;
machine.y = 200;
addChild(machine);
textField = new TextField();
textField.x = 350;
textField.y = 200;
textField.text = "点击走到目标";
addChild(textField);
textField.addEventListener(MouseEvent.CLICK,tfClickHandler);
}
//--------------------------------------------------------------------------
// Variables
//--------------------------------------------------------------------------
private var machine:RandomAwardMachine;
private var textField:TextField;
//--------------------------------------------------------------------------
// Propertise
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// Methods
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// Event Handler
//--------------------------------------------------------------------------
private function tfClickHandler(event:MouseEvent):void
{
machine.receiveTargat(AwardIconIndex.ICON_1);
textField.text = "icon1";
}
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// Dispose
//--------------------------------------------------------------------------
}
}