上一节,我们的棋子就是一个canvas,里面add进了一个ellipse圆圈和textblock字
想想我们是怎么下棋的,要先选中棋子吧,选中后,随便找个地方点,棋就会自动移过去。
所以,这里就产生了两件事,一是选中,二是移动。
要选中,其实就是选中棋子,选中棋子就是选中canvas了。
于是,我们为canvas增加一个鼠标点击事件。
让我们回到棋子类chessman的draw方法里,为chessman添加一个mouseleftbuttondown事件,于是代码变成了
private void draw()
{
//这里实现画啦
ellipse elp = new ellipse()
{
width = radius * 2,
height = radius * 2,
stroke = new solidcolorbrush(color),
fill = new solidcolorbrush(color),
opacity = 15
};
textblock text = new textblock()
textalignment = textalignment.center,
foreground = new solidcolorbrush(colors.white),
text = name,
fontfamily = new fontfamily("宋体"),
fontsize = radius,
fontweight = fontweights.bold,
margin = new thickness(radius / 2 - 2, radius / 2 - 2, 0, 0)
chessman = new canvas();
//----这里新加一个事件啦-----
chessman.mouseleftbuttondown += new mousebuttoneventhandler(chessman_mouseleftbuttondown);
point pixel = parent.switchpixelarray(initpoint);
chessman.margin = new thickness(pixel.x - radius, pixel.y - radius, 0, 0);
chessman.children.add(elp);
chessman.children.add(text);
container.children.add(chessman);
}
//新加的事件方法
void chessman_mouseleftbuttondown(object sender, mousebuttoneventargs e)
messagebox.show("你选中的是:" + name);
其实就一共新增加了三行代码,运行看看效果。
好了,选中是ok了,那我们怎么移动?
silverlight里有几种移动方法,这里挑了storyboard故事板来移动棋子。
按原始想法,棋子自己不会动,所以新建一个chessaction类来实现棋子的移动。
啥也不说,对着类库右键-》添加类—》chessaction.cs就新建了。
/// <summary>
/// 棋子动作类 by 路过秋天
/// </summary>
public class chessaction
{
}
想想棋子是怎么动的?其实就两个动作,一个是吃子,另一个没子吃直接移动。于是呢,就先产生两个方法:
/// <summary>
/// 吃子
/// </summary>
/// <param name="movechessman">移动的棋子</param>
/// <param name="eatchessman">被吃的棋子</param>
public void eatchessman(chessman movechessman, chessman eatchessman)
/// <summary>
/// 移动棋子
/// <param name="chessman">棋子</param>
/// <param name="tox">移动到x坐标</param>
/// <param name="toy">移动到y坐标</param>
public bool moveto(chessman chessman, point moveto)
return true;
再想想,其实吃子,就是移动棋子,让后叫被吃的那个子离开自己的位置gotodead。
-_-..这里顺便为chessman棋子类自己加个方法叫gotodead好了。
/// <summary>
/// 销亡
public void gotodead()
container.children.remove(chessman);//从控件中移除
parent.chessmanlist.remove(this);//从棋子列表移除
ok,我们可以为吃子方法写完它了。先移动棋子,然后叫被吃的自己gotodead了。
public void eatchessman(chessman movechessman, chessman eatchessman)
if (moveto(movechessman, eatchessman.movepoint))
eatchessman.gotodead();
}
说来说去,就剩下要完成moveto的时候,棋子要移动了。
我们把移动的动作封成一个函数叫playmove,所以在moveto里轻松调用playmove就搞定了。
public bool moveto(chessman chessman, point moveto)
playmove(chessman, moveto);
void playmove(chessman chessman, point moveto)
//这里完成移动啦
移动的代码的故事版,先上代码,再解说
void playmove(chessman chessman, point moveto)
moveto = parent.switchpixelarray(moveto);
point initpixel = parent.switchpixelarray(chessman.initpoint);
point movepixel = parent.switchpixelarray(chessman.movepoint);
storyboard sb = new storyboard();//创建动画版
//创建x方向的动画
doubleanimation dax = new doubleanimation();
dax.duration = new duration(timespan.frommilliseconds(200));
dax.from = movepixel.x - initpixel.x;
dax.to = moveto.x - initpixel.x;
//创建y方向的动画
doubleanimation day = new doubleanimation();
day.duration = new duration(timespan.frommilliseconds(200));
day.from = movepixel.y - initpixel.y;
day.to = moveto.y - initpixel.y;
//设置动画版上的目标
storyboard.settarget(dax, chessman.chessman);
storyboard.settarget(day, chessman.chessman);
//设置动画版上的目标要变化的属性
storyboard.settargetproperty(dax, new propertypath("(canvas.left)"));
storyboard.settargetproperty(day, new propertypath("(canvas.top)"));
sb.children.add(dax);
sb.children.add(day);
sb.begin();
sb = null;
其实storyboard的类用法,比较死的,记住怎么用就行了,记不住就copy多几次也就差不多了。
实在看不懂,多在博客园里多搜搜该类的用法,看来看去基本就是这么用,看的多了习惯了就也是这么一回事了。
这里说几个注意点:
1。parent哪来的?我们的棋子类不是也有一个parent么,其实就是chess类对象了。
所以呢,我们要为chessaction添加一个属性和构造函数,让chess对象传进来啦。
/// chess对象
public chess parent
get;
set;
public chessaction(chess ownchess)
parent = ownchess;
2。就是那个坐标要先转换成像素坐标,比较滑动的时候是用像素来算的
3。这个重要了:故事板滑动的几个from,to的像素,要减去棋子本身的初始相对坐标,才能滑的正确。
关于这个,我调试了好久,才让它滑的正确,默认博客园里的相关文章都没有相对物体一开始就相对存在的情况做说明。
4。最后一点就是,在chess类的构造函数里默认实例化一下这个chessaction对象。
其实chess类的构造函数加了一行代码,同时多了一个属性
/// 棋子动作,新加的属性
public chessaction action
public chess(panel control)
container = control;
chessmanlist = new list<chessman>(32);
action = new chessaction(this);//新增加的
ok,该写的都写的差不多完了。可是目前运行的话,还是没有效果可看。
我们还是赶紧弄一下吃子的效果出来先。吃子,就是选中一颗棋子,然后再点另一颗棋子。
所以在棋子被点击的时候,我们要判断,是不是已经有了一颗棋子被点击了,如果有,就执行吃子动作了。
这里要判断,怎么判断有没有已经被选中的,这里我倒有一个方法:
为棋子增加一个readymove属性
/// 待移动
public bool readymove
这样,我们被点击的时候,只要设置一下自己的readymove就行了。
那我们怎么找哪一颗棋子曾经被选中??这个简单了,遍历棋子列表,找出readymove=true的棋子就行了。
我们为chess类增加一个readymovechessman属性,返回被激活readymove为true的棋子。
/// 激活的棋子,就是选中要下的棋子。
public chessman readymovechessman
get
foreach (chessman chessman in chessmanlist)
{
if (chessman.readymove)
{
return chessman;
}
}
return null;
ok,现在我们可以回到棋子点击事件里,加if判断如下:
//新加的事件方法
if (readymove)//取消激活
readymove = false;
chessman.background = null;
else if (parent.readymovechessman == null)//激活
readymove = true;
chessman.background = new solidcolorbrush(colors.blue);//选中时加个背景色
else//吃子
if (parent.readymovechessman.color == this.color)//同颜色,切换
parent.readymovechessman.chessman.background = null;
parent.readymovechessman.readymove = false;
readymove = true;
chessman.background = new solidcolorbrush(colors.blue);
else
parent.action.eatchessman(parent.readymovechessman, this);
ok,按f5执行。
1。点击棋子,发现棋子背景色没变?
2。点另一个棋子,发现吃棋子动作完成了。这个很好。
3。再点另一个棋子,还是继续吃棋?
小小调试了一下发现:
1。背景色没变,原来是忘记了给canvas弄个宽和高。
到chessman类里,在draw函数里找到canvas,设置一下宽和高就行了。
private void draw()
chessman = new canvas()
width = elp.width,//新增加的宽
height = elp.height//新增加的高
于是点击时候的背景色出来了。
3.怎么一直吃子?那是移动后呢?有手尾要做的
a.设置棋子的readymove=false;
b.去掉棋子的背景色。
c.移动后,棋子的move坐标要换成被吃棋子的坐标。
于是,我们回到moveto函数里,新增加这几个手尾:
chessman.readymove = false;
chessman.chessman.background = null;
chessman.movepoint = moveto;
好了,手尾弄好了,现在移动棋子就变成吃子了:
ok,到现在棋子终于可以走了,不过目前只是吃子,而且是随便吃的。。。
下节说不吃子,让棋子走到线的交叉点上。
ok,打完收工
版权声明:本文原创发表于博客园,作者为路过秋天,原文链接:
http://www.cnblogs.com/cyq1162/archive/2010/07/08/1773840.html