练习--五子棋
import flash.events.MouseEvent;
rest_btn.buttonMode = true;//指定鼠标指针经过该按钮时触发手形光标的显示
huiqi_btn.buttonMode = true;
var qi:Boolean = true; //声明一个布尔变量
var step:Array = new Array(0);//声明一个名为step的新数组并赋给它一个初始值0
var bushu:uint;//声明一个无符号整型bushu
for (var i:uint = 0; i<10; i++)//声明棋盘X轴的格数
{
for (var j:uint = 0; j<10; j++)//声明棋盘Y轴的格数
{
var q:qizi = new qizi();//新声明变量q
q.x = 40 * i;
q.y = 40 * j;
stage.addChild(q);//在stage上绘制一个10*10的棋盘
q.buttonMode = true;//指定鼠标指针经过该按钮时触发手形光标的显示
q.addEventListener(MouseEvent.CLICK,onXiaQi);//定义一个q的鼠标侦听事件
}
}
function onXiaQi(e:MouseEvent ):void
{
if (qi == true)
{
e.currentTarget.gotoAndStop(2);//侦听事件q并跳转到第二帧
e.currentTarget.buttonMode = false;//指定鼠标指针经过该按钮时不触发手形光标的显示
qi = false;//为变量qi赋一个新值
}
else
{
e.currentTarget.gotoAndStop(3);//侦听事件q并跳转到第三帧
e.currentTarget.buttonMode = false;//指定鼠标指针经过该按钮时不触发手形光标的显示
qi = true;//为变量qi赋一个新值
}
e.currentTarget.removeEventListener(MouseEvent.CLICK,onXiaQi);//清除侦听事件
step.push(e.currentTarget);//给数组step添加新元素
bushu++;
xianshi_txt.text =String("第"+ bushu +"步");
}
rest_btn.addEventListener(MouseEvent.CLICK,onRest);
function onRest(e:MouseEvent ):void
{
if (step.length == 0)//当数组step长度为0时
{
xianshi_txt.text = "您还未开局!";
}
for each (var m:* in step)
{
m.gotoAndStop(1);//用foreach循环使棋盘恢复初始状态
m.buttonMode = true;
m.addEventListener(MouseEvent.CLICK,onXiaQi);
qi = ! qi;
}
bushu = 0;
xianshi_txt.text =String("第"+ bushu +"步");
step = [];
}
huiqi_btn.addEventListener(MouseEvent.CLICK,onHuiQi);
function onHuiQi(e:MouseEvent ):void
{
if (step.length == 0)
{
xianshi_txt.text = "您还未开局!";
}
else
{
var lastqizi = step.pop();//声明一个新变量并把数组step的最后一个元素删除后赋值给它
lastqizi.gotoAndStop(1);
lastqizi.buttonMode = true;
lastqizi.addEventListener(MouseEvent.CLICK,onXiaQi);
qi = !qi;//给qi循环赋值
bushu--;
xianshi_txt.text =String("第"+ bushu +"步");
}
}