欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Flash教程:模拟幸运号抽奖机

程序员文章站 2022-04-26 15:20:22
各位看官记得要回帖哦!教学中经常要用到抽号,名称随便取的,也叫“幸运号”吧。做了一个,不是很成熟,有那么点效果而已,仅供新手参考(程序中运用了一些常用的基础语法... 08-10-06...
各位看官记得要回帖哦!教学中经常要用到抽号,名称随便取的,也叫“幸运号”吧。做了一个,不是很成熟,有那么点效果而已,仅供新手参考(程序中运用了一些常用的基础语法),抛砖引玉而已。



第一帧上代码:
/* sxl001 http://sxl001.07dns.com qq: 285510591 */
//============= 初始区 ================================
stop();
//----- 全屏与隐菜单区
fscommand("fullscreen", true);
fscommand("showmenu", false);
//----- 变量区
/* total_num 记录输入总数 | txtlong_num 输入字符长度 | num_array 记录输入的所有数
如输入 119 时:
total_num=119 | txtlong_num=3 | num_array=[001,002,003,....]
*/
var total_num:number = 0;
var txtlong_num:number = 0;
var num_array:array = [];
//=======================================================
//============= 按钮与声音区 ============================
var startmp3:sound = new sound();
startmp3.attachsound("start");
startmp3.start();
var loopmp3:sound = new sound();
loopmp3.attachsound("loop");
startmp3.onsoundcomplete = function() {
startmp3.start();
};
start_mc.onrollover = function() {
_sound();
this.play();
};
start_mc.onpress = function() {
submit();
};
start_mc.onrollout = function() {
this.play();
};
quit_btn.onrollover = function() {
_sound();
};
quit_btn.onpress = function() {
fscommand("quit", true);
};
//----- 文本区
selection.setfocus(input_txt);
input_txt.maxchars = 4; // 控制4位(最多9999人)足够用了
input_txt.restrict = "0-9";
input_txt.border = true;
input_txt.bordercolor = 0xff00ff;
//==========================================================
//============= 函数区 =====================================
//----- 提交
function submit() {
if (input_txt.text != "" && input_txt.text.substr(0, 1) != "0") {
total_num = number(input_txt.text);
txtlong_num = input_txt.text.length;
distribution(input_txt.text);
startmp3.stop();
play();
}
key.removelistener(keylistener);
}
//-------- num_array存贮从0到最大数值
function distribution(str:string) {
var long:number = str.length;
var maxnum:number = number(str);
for (var i:number = 1; i<=maxnum; i ) {
var l:number = string(i).length;
var zha:number = long-l;
switch (zha) {
case 0 :
num_array.push(i);
break;
case 1 :
num_array.push("0" i);
break;
case 2 :
num_array.push("00" i);
break;
case 3 :
num_array.push("000" i);
break;
}
}
}
//----- 数组乱序
function randomarray(target_array:array) {
target_array.sort(function () {
return math.round(math.random()) ? 1 : -1;
});
}
//----- 按钮声音
function _sound() {
var btns:sound = new sound();
btns.attachsound("btnsound");
btns.start();
}
//----- 回车键提交
var keylistener:object = new object();
keylistener.onkeydown = function() {
if (key.getcode() == "13") {
if (input_txt.text != "" && input_txt.text.substr(0, 1) != "0") {
total_num = number(input_txt.text);
txtlong_num = input_txt.text.length;
distribution(input_txt.text);
startmp3.stop();
_root.play();
key.removelistener(keylistener);
}
}
};
key.addlistener(keylistener);
//================================================ 第二帧上代码:
stop();
//============= 初始区 ===========================
//------ 加点投影效果
import flash.filters.dropshadowfilter;
var d_filtersropshadowfilter = new dropshadowfilter(5, 45, 0x336633, 80, 8, 8);
//------ 数组乱序
randomarray(num_array);
//------ loop声音
loopmp3.start();
loopmp3.onsoundcomplete = function() {
loopmp3.start();
};
/*
如输入 119 时:
total_num=119 | txtlong_num=3 | num_array=[001,....]
randomnum 停止工作时显示未位上的数字相同的一组文本框(0~3) | c_array 用于删除已抽的号码函数 |
getno_array 存贮获取的当前一组文本中的数字 | jieguo_mcy0 弹出的显示已抽号的窗口的初始y坐标
*/
var randomnum:number, c_array:array = [], getno_array:array = [], jieguo_mcy0:number = jieguo_mc._y;
var ko:boolean = true;
total_txt.text = "参与选号总数是: " total_num;
//=====================================================
//============= 函数区 ===============================
//------ 根据第一帧上输入的数值复制文本框个数与各位数上的数量
this.createemptymovieclip("main", 0);
body();
function body() {
for (var i:number = 0; i<txtlong_num; i ) {
for (var j:number = 0; j<4; j ) {
main["m" i j] = main.attachmovie("cell", "m" i j, main.getnexthighestdepth(), {_x:i*80, _y:-j*85});
main["m" i j].v = true;
main["m" i j].filters = [d_filters];
//main["m" i j].num 决定再次升到顶端时其中的数值从数组num_array中进行取值的位置
main["m" i j].num = j;
main["m" i j].t.text = num_array[j].tostring().substr(i, 1);
rolling(main["m" i j]);
}
}
}
//------- 文本框居中排列
posimain();
function posimain() {
main._x = stage.width/2-main._width/2;
main._y = stage.height/2-40;
}
//------- 滚动函数
function rolling(target:movieclip) {
target.onenterframe = function() {
if (this.v) {
this._y = 40;
if (this._y>=80) {
this.num = 4;
if (this.num>total_num) {
this.num = number(this._name.substr(2, 1));
}
this.t.text = num_array[number(this.num)].tostring().substr(this._name.substr(1, 1), 1);
this._y = -255;
}
} else {
delecell();
}
};
}
main.setmask(mask_mc);
//------- 停止滚动和显示结果区
function delecell() {
for (var i in main) {
// 如果文本框未位上的数字与停止按钮给出的数字不同时就被删除
if (main._name.substr(2, 1) != randomnum) {
main.t.text = "";
main._visible = false;
main.removemovieclip();
delete main.onenterframe;
} else {
// 如果文本框未位上的数字与停止按钮给出的数字相同时就作为显示对象
if (main._y != 0) {
main._y = (0-main._y)/10;
} else {
//--- 删除num_array中已抽的号码
c_array.push(main);
if (c_array.length == txtlong_num) {
var n = main.num;
getno_array.push(num_array[n]);
jieguo_mc.t.text = "";
for (var index:number = (getno_array.length-1); index>=0; index--) {
jieguo_mc.t.text = "第" (index 1) "次抽取的号码是: " getno_array[index] "\n";
}
var tw1 = new mx.transitions.tween(jieguo_mc, ’_y’, mx.transitions.easing.back.easeout, jieguo_mcy0, stage.height-jieguo_mc._height, 1, true);
num_array.splice(n, 1);
delete main.onenterframe;
}
}
}
}
}
//===================================================
//============= 按钮与声音区 ========================
stop_mc.t.text = "停止";
reset_mc.t.text = "再选";
// 控制按钮切换变量
stop_mc.onoff = true;
reset_mc.onoff = false;
stop_mc.onrollover = function() {
_sound();
this.play();
};
stop_mc.onpress = function() {
if (this.onoff) {
// randomnum决定哪组文本框被保留或被删除
randomnum = random(4);
loopmp3.stop();
c_array = [];
for (var i in main) {
//main.v决定执行停止滚动和显示结果区
main.v = false;
}
reset_mc.onoff = true;
this.onoff = false;
ko = false;
}
};
stop_mc.onrollout = function() {
this.play();
};
reset_mc.onrollover = function() {
_sound();
this.play();
};
reset_mc.onpress = function() {
if (this.onoff) {
loopmp3.start();
for (var i in main) {
delete main.onenterframe;
main.removemovieclip();
}
body();
stop_mc.onoff = true;
this.onoff = false;
ko = true;
}
};
reset_mc.onrollout = function() {
this.play();
};
jieguo_mc.press_mc.onrollover = function() {
_sound();
};
jieguo_mc.press_mc.onpress = function() {
var tw2 = new mx.transitions.tween(jieguo_mc, ’_y’, mx.transitions.easing.back.easeout, jieguo_mc._y, jieguo_mcy0, 1, true);
};
//----- 回车键控制
var keyobj:object = new object();
keyobj.onkeydown = function() {
if (key.getcode() == key.enter) {
switch (ko) {
case true :
// randomnum决定哪组文本框被保留或被删除
randomnum = random(4);
loopmp3.stop();
c_array = [];
for (var i in main) {
//main.v决定执行停止滚动和显示结果区
main.v = false;
}
reset_mc.onoff = true;
stop_mc.onoff = false;
ko = false;
break;
case false :
loopmp3.start();
for (var i in main) {
delete main.onenterframe;
main.removemovieclip();
}
body();
stop_mc.onoff = true;
reset_mc.onoff = false;
ko = true;
break;
}
}
};
key.addlistener(keyobj);
//===========================================