WEB开发网
开发学院软件开发C语言 C#开发WPF/Silverlight动画及游戏系列教程(Game C... 阅读

C#开发WPF/Silverlight动画及游戏系列教程(Game Course):(二十七)战斗前夜之构建动态障碍物系统

 2010-09-30 20:47:32 来源:WEB开发网   
核心提示: 第一步,创建这两个重要对象: //设置游戏窗体辅助线程 AuxiliaryThread = new DispatcherTimer(DispatcherPriority.Normal); AuxiliaryThread.Tick += new EventHandler(AuxiliaryThre

第一步,创建这两个重要对象:

            //设置游戏窗体辅助线程

            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);       

}

上一页  1 2 3 4 5  下一页

Tags:开发 WPF Silverlight

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接