C#开发WPF/Silverlight动画及游戏系列教程(Game Course):(二十七)战斗前夜之构建动态障碍物系统
2010-09-30 20:47:32 来源:WEB开发网第一步,创建这两个重要对象:
//设置游戏窗体辅助线程
AuxiliaryThread = new DispatcherTimer(DispatcherPriority.Normal);
AuxiliaryThread.Tick += new EventHandler(AuxiliaryThread_Tick);
AuxiliaryThread.Interval = TimeSpan.FromMilliseconds(1000);
AuxiliaryThread.Start();
//设置后台工作者
BackWorker = new BackgroundWorker();
BackWorker.DoWork += new DoWorkEventHandler(BackWorker_DoWork);
BackWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackWorker_RunWorkerCompleted);
第二步,在辅助线程(AuxiliaryThread)计时器中启动异步后台处理:
private void AuxiliaryThread_Tick(object sender, EventArgs e) {
//异步刷新面板及障碍物
if (!BackWorker.IsBusy) { BackWorker.RunWorkerAsync(); }
}
第三步,定义工作委托:delegate void WorkDelegate();
然后在已经注册的后台处理事件(BackWorker_DoWork)中执行异步委托(RefreshFace),从而实现刷新动态障碍物数组:
private void RefreshFace() {
//将当前动态障碍物重置为原始固定值
VaryObstruction = (byte[,])FixedObstruction.Clone();
//重新填充动态障碍物
for (int i = 0; i < Carrier.Children.Count; i++) {
if (Carrier.Children[i] is QXSpirit) {
QXSpirit spirit = Carrier.Children[i] as QXSpirit;
if (spirit != Leader) {
int x = (int)(spirit.X / GridSizeX);
int y = (int)(spirit.Y / GridSizeY);
for (int m = x - spirit.HoldWidth; m <= x + spirit.HoldWidth; m++) {
for (int n = y - spirit.HoldHeight; n <= y + spirit.HoldHeight; n++) {
VaryObstruction[m, n] = 0;
}
}
}
}
}
}
private void BackWorker_DoWork(object sender, DoWorkEventArgs e) {
//跨线程异步刷新障碍物
this.Dispatcher.BeginInvoke(new WorkDelegate(RefreshFace), DispatcherPriority.Normal, null);
}
Tags:开发 WPF Silverlight
编辑录入:爽爽 [复制链接] [打 印]- ››开发Android 日历教程
- ››开发学院总结 Win 8实用技巧大全
- ››开发学院原创教程:把win8的IE10放桌面上方法(非...
- ››silverlight全屏显示图片
- ››Silverlight MVVM 模式(一) 切近实战
- ››开发者眼中的Windows Phone和Android
- ››开发学院教你用SQL 语句最快速清空MySQL 数据表的...
- ››WPF学习笔记12. Binding (5)
- ››WPF学习笔记11. Binding (4)
- ››WPF学习笔记10. Binding (3)
- ››WPF学习笔记9. Binding (2)
- ››WPF学习笔记8. Binding
更多精彩
赞助商链接