--------------------------------------
场景:
1、两个动态文本框 分别命名show_txt和result_txt(show_txt用来显示随机数result_txt用来显示抽取的数字)
2、三个按钮,分别命名为start_btn和next_btn和reset_btn(start_btn为开始抽号,next_btn继续抽号,reset_btn重新抽号)
代码:
//定义抽取号码范围
var hao:Array = new Array()
for(var i:int = 1i<49i++)
{
hao.push(i)
}
var num:int = 0
var add_num:int = 0//第几次抽奖
//抽取的号分类
//随机显示被抽号码
var timer = new Timer(100, 0)
timer.addEventListener(TimerEvent.TIMER, timerHandler)
timer.start()
//开始抽取号码
start_btn.addEventListener(MouseEvent.CLICK,yaohaoF)
function timerHandler(e:TimerEvent):void
{
//取得hao数组的长度,里现在是1000
num = Math.floor(Math.random()*hao.length)
show_txt.text = hao[num]
}
function yaohaoF(event:MouseEvent):void
{
add_num++
//result_txt.text="幸运号"+xu_array[add_num-1]+":"+Math.floor(Math.random()*hao.length)+"\r"
result_txt.text="抽取号码为:"+show_txt.text+"\r"
timer.stop()
//关键在这里,把上面抽出的号码从数组中删除,它就不会再出现了
hao.splice(num, 1)//删除已经选中的号码
//如果已经抽出49就停止
if (add_num>49)
{
timer.stop()
show_txt.text = "全部号码已抽取完成"
result_txt.text =""
start_btn.enabled = false
next_btn.enabled = true
}
}
next_btn.addEventListener(MouseEvent.CLICK,nextF)
function nextF(event:MouseEvent):void
{
timer.start()
result_txt.text=""
}
reset_btn.addEventListener(MouseEvent.CLICK,resetF)
function resetF(event:MouseEvent):void
{
timer.start()
start_btn.enabled = true
next_btn.enabled = false
add_num = 0
}
利用数组来进行随机,每次获取数组中随机位置的一个值var randomAry:Array=["A","B","C"]
function beginRandom():Array
{
var returnAry:Array=new Array()
for(var i:int=0i<4i++)
{
var randStr:String=String(randomAry[int(randomAry.length*Math.random())])
returnAry.push(randStr)
}
return returnAry
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)