浏览 3529 次
锁定老帖子 主题:猜数字小游戏[附图和源码]
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-01-11
最后修改:2011-01-11
还是很简单的, 当时是为了用一下Flex的拖拽功能. code中引用的gif图片没有上传, 随便下载一套, 按照路径放置之后, 编译就可以了. <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" horizontalAlign="center" verticalAlign="middle" backgroundColor="#FFFFFF" backgroundGradientColors="[#ffffff, #e1e1e1]" initialize="init()"> <mx:Script> <![CDATA[ import mx.skins.halo.DateChooserIndicator; import mx.charts.DateTimeAxis; import mx.formatters.DateBase; import mx.accessibility.DateChooserAccImpl; import flash.utils.getTimer; import mx.charts.AreaChart; import mx.events.CollectionEvent; import mx.rpc.events.AbstractEvent; import mx.events.DragEvent; import mx.events.ListEvent; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.events.ItemClickEvent; import mx.controls.TabBar; import mx.collections.ICollectionView; import mx.managers.DragManager; import mx.core.IUIComponent; [Bindable][Embed("img/0.gif")] public var num0:Class; [Bindable][Embed("img/1.gif")] public var num1:Class; [Bindable][Embed("img/2.gif")] public var num2:Class; [Bindable][Embed("img/3.gif")] public var num3:Class; [Bindable][Embed("img/4.gif")] public var num4:Class; [Bindable][Embed("img/5.gif")] public var num5:Class; [Bindable][Embed("img/6.gif")] public var num6:Class; [Bindable][Embed("img/7.gif")] public var num7:Class; [Bindable][Embed("img/8.gif")] public var num8:Class; [Bindable][Embed("img/9.gif")] public var num9:Class; [Bindable] public var numSelection_dragable:Boolean = false; [Bindable] public var numSelection_dropable:Boolean = false; [Bindable] public var resultArea_dragable:Boolean = false; [Bindable] public var resultArea_dropable:Boolean = false; [Bindable] public var nowTime : String ="init...."; [Bindable] public var passedTime : Number = 0; [Bindable] public var resultAreaArr:ArrayCollection = new ArrayCollection(); [Bindable] private var guessTrackArr : ArrayCollection =new ArrayCollection(); private var currentStr : String =""; private var finalResStr : String =""; private var globalTimer : Timer =new Timer(1000,0); private var playTimer : Timer =new Timer(1000,0); private var checkCount: int = 0; private function init():void{ resultAreaArr.addEventListener(CollectionEvent.COLLECTION_CHANGE, collectionChangeEvnHander); globalTimer.addEventListener(TimerEvent.TIMER,doTimer);globalTimer.start(); playTimer.addEventListener(TimerEvent.TIMER,doPlayTimer); guessTrackArr.addItem("--- guess track ---"); } private function resetProvderAreaArr():void{ numSelection.dataProvider = [ {label : "zero" , icon : num0}, {label : "one" , icon : num1}, {label : "two" , icon : num2}, {label : "three", icon : num3}, {label : "four" , icon : num4}, {label : "five" , icon : num5}, {label : "six" , icon : num6}, {label : "seven", icon : num7}, {label : "eight", icon : num8}, {label : "nine" , icon : num9} ]; resultAreaArr.removeAll(); } private function clickEvt(event:ItemClickEvent):void { var targetComp:TabBar = TabBar(event.currentTarget); } private function canDragOrNot() : void{ if (resultArea_dropable==false){ Alert.show("Please click Start button first."); } if (this.numSelection.dataProvider.length>=7){ this.numSelection_dragable =true; }else{ this.numSelection_dragable =false; } } private function convertEngToNum(input : String) : String{ var resultStr : String; if (input == "one"){ resultStr = "1"; }else if (input == "two"){ resultStr = "2"; }else if (input == "three"){ resultStr = "3"; }else if (input == "four"){ resultStr = "4"; }else if (input == "five"){ resultStr = "5"; }else if (input == "six"){ resultStr = "6"; }else if (input == "seven"){ resultStr = "7"; }else if (input == "eight"){ resultStr = "8"; }else if (input == "nine"){ resultStr = "9"; }else if (input == "zero"){ resultStr = "0"; } return resultStr; } private function btnCheckOnclick() : void{ if (currentStr!="" && currentStr.length==4){ if (finalResStr.length==4 && currentStr.length==4){ checkCount++; guessTrackArr.addItemAt(checkCount+"- "+currentStr+" -["+ compareStr(finalResStr,currentStr)+"]",0); cb_guessTrack.focusEnabled=true; cb_guessTrack.selectedIndex=0; } } } private function generateFinalResultStr(): String{ var resultStr: String=""; var tmpArr:ArrayCollection = new ArrayCollection(); var tmpNum : Number; while (tmpArr.length<4){ tmpNum = randRange(0,9); if (!tmpArr.contains(tmpNum)){ tmpArr.addItem(tmpNum); } } for(var i: int = 0; i<tmpArr.length;i++){ resultStr += tmpArr[i].toString(); } return resultStr; } private function randRange(min:Number, max:Number):Number { var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min; return randomNum; } public function collectionChangeEvnHander(event : CollectionEvent) : void{ var tmpStr:String = ""; for (var i:int =0; i< this.resultAreaArr.length;i++){ tmpStr += convertEngToNum(resultAreaArr[i].label); } currentStr = tmpStr; } public function startGame():void{ resetProvderAreaArr(); finalResStr = generateFinalResultStr(); passedTime = 0; checkCount =0; playTimer.start(); guessTrackArr.removeAll(); guessTrackArr.addItem("-view track here-"); numSelection_dragable = true; numSelection_dropable = true; resultArea_dragable = true; resultArea_dropable = true; } private function doTimer(event:TimerEvent):void{ nowTime = new Date().toLocaleTimeString(); } private function doPlayTimer(event:TimerEvent):void{ passedTime += 1; } public function compareStr(finalStr:String,guessStr:String):String{ var tmpArr : ArrayCollection = new ArrayCollection(); var resultStr : String = ""; var countA : int = 0; var countB : int = 0; if (finalStr.length==4 && guessStr.length==4){ for(var i:int=0; i<finalStr.length; i++){ tmpArr.addItem(finalStr.charAt(i)); if (finalStr.charAt(i) == guessStr.charAt(i)){ countA += 1; } } for (var j:int=0;j<guessStr.length;j++){ if (tmpArr.contains(guessStr.charAt(j))){ countB += 1; } } var tmpNum : int = countB - countA; resultStr = "A:"+countA.toString()+" B:"+tmpNum.toString(); } if (resultStr=="A:4 B:0"){ Alert.show("Gratulation ! The right answer is :["+guessStr+"] you totally used "+passedTime.toString()+" s and "+checkCount+" tries"); playTimer.stop(); } return resultStr; } ]]> </mx:Script> <mx:Panel height="400" layout="vertical" horizontalAlign="center" verticalAlign="middle" width="590" id="mainPanel" title="RIDDLE GAME by Lori"> <mx:TabBar itemClick="clickEvt(event);" width="566"> <mx:dataProvider>mainArea</mx:dataProvider> </mx:TabBar> <mx:ViewStack id="mainArea" width="100%" height="100%"> <mx:Panel label="- {nowTime} - The GOAL is [A:4 B:0]" title="GAME rules: [A] means count of right location and value, [B] means only right value." layout="vertical" height="333"> <mx:VBox height="100%" width="549" horizontalAlign="center" verticalAlign="top"> <mx:TileList id="numSelection" mouseMove="canDragOrNot()" dropEnabled="{numSelection_dropable}" dragMoveEnabled="true" dragEnabled="{numSelection_dragable}" height="79" maxRows="1" maxColumns="10" width="549" allowMultipleSelection="false"> <mx:dataProvider> <mx:ArrayCollection id="arr"> <mx:Object label="zero" icon="{num0}"/> <mx:Object label="one" icon="{num1}"/> <mx:Object label="two" icon="{num2}"/> <mx:Object label="three" icon="{num3}"/> <mx:Object label="four" icon="{num4}"/> <mx:Object label="five" icon="{num5}"/> <mx:Object label="six" icon="{num6}"/> <mx:Object label="seven" icon="{num7}"/> <mx:Object label="eight" icon="{num8}"/> <mx:Object label="nine" icon="{num9}"/> </mx:ArrayCollection> </mx:dataProvider> </mx:TileList> <mx:Label text="Please drag & drop the number to below box and click the CHECK button"/> <mx:HBox width="100%" verticalAlign="middle" horizontalAlign="center"> <mx:TileList id="resultArea" maxColumns="4" dragEnabled="{resultArea_dragable}" dragMoveEnabled="true" maxRows="1" dropEnabled="{resultArea_dropable}" height="81" width="216" allowMultipleSelection="false" dataProvider="{resultAreaArr}"/> <mx:VBox height="100%"> <mx:Panel width="160" height="78" layout="vertical" title="Time has passed away" horizontalAlign="center" verticalAlign="middle"> <mx:Label id="l_gameTimer" text="{passedTime} s"/> </mx:Panel> </mx:VBox> </mx:HBox> <mx:HBox width="100%" verticalAlign="middle" horizontalAlign="center"> <mx:ComboBox id="cb_guessTrack" dataProvider="{guessTrackArr}" selectedIndex="0"/> <mx:Button label="CHECK " enabled="true" id="btn_check" width="111" click="btnCheckOnclick()"/> </mx:HBox> <mx:HBox width="100%" verticalAlign="middle" horizontalAlign="center"> <mx:Button label="Start" click="startGame()"/> <mx:Button label="reset" click="resetProvderAreaArr()"/> </mx:HBox> </mx:VBox> </mx:Panel> <!--mx:Panel label="statistics" width="100%" height="323"> <mx:HBox width="100%" height="100%"> <mx:VBox height="285" width="100%" horizontalAlign="center" verticalAlign="middle"> <mx:Label text="statistics of your last playing"/> </mx:VBox> </mx:HBox> </mx:Panel--> </mx:ViewStack> </mx:Panel> </mx:Application> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |