C#问题中对象之间的通信

时间:2015-02-26 00:31:05

标签: c# silverlight animation windows-phone

我试图提供两个对象之间的通信,这些对象可以最好地描述为回合制。我的意思是第一个对象(一个Game类)有多个目标在数据库中指定,第二个对象代表一个用于从精灵表中播放动画的类。我目前的目标是在Game类(我已经完成)中展示一个介绍性动画,向玩家展示他的第一个目标。然后该类期望以动画参数的形式输入用户,然后将其作为动画列表传递。 动画完成后,我会检查是否符合目标。如果它被满足,并且它不是最后一个,我需要回到Game类,显示下一个目标,进行排序解释,然后期望用户输入并重复直到所有目标都满足。

这是Windows Phone 8.1 Silverlight。

以下是Game类的代码(最相关的代码段):

 public Game(GamePage gp, int chapterNum)
   {
       currentClickedCommand = null;
       currentClickedSlot = null;
       gamePage = gp;
       chapterNumber = chapterNum;
       isAnimationComplete = false;
       //chosenCommands = new string[5];
       gamePage.characterRectangle.Visibility = System.Windows.Visibility.Collapsed;
       Thread AnimationThread = new Thread(playIntroAnimation);
       AnimationThread.Start();
       Thread loadingThread = new Thread(loadControls);
       loadingThread.Start();
   }

这两个线程用于播放动画(动画线程),另一个线程等待将静态变量设置为true以加载用户输入控件。代码:

private void loadControls()
       {
           while (!InformationObject.isSequenceComplete) { }
           Deployment.Current.Dispatcher.BeginInvoke(() =>
           {
              // loadGoals();
               showCurrentGoal();
               gamePage.CommandGrid.Visibility = System.Windows.Visibility.Visible;
               gamePage.SlotGrid.Visibility = System.Windows.Visibility.Visible;
               gamePage.mainCanvas.Background = null;
               gamePage.characterRectangle.Visibility = Visibility.Collapsed;
               loadCommands();
               loadSlots();
           });
       }

这是播放用户选择的动画序列的方法:

public void AnimateCurrentAnimationInSequence(Rectangle container) {
            AnimationParams currentParams = sequenceToAnimate[currentAnimationInSequence];
            DoubleAnimation fadeInAnimation = new DoubleAnimation();
            fadeOutAnimation = new DoubleAnimation();
            if (sequencePlayerInteractions.Count != 0 && sequencePlayerInteractions != null)
            {
                PlayerInteraction currentInteraction = sequencePlayerInteractions[CurrentInteraction];
                TalkBubble interactionBubble = new TalkBubble(currentInteraction.Value);
                interactionBubble.setWidthHeight((int)container.Width, (int)container.Height);
                talkBubble = interactionBubble;
                onCanvas.Children.Add(talkBubble);
                Canvas.SetLeft(talkBubble, Canvas.GetLeft(container) + 30);
                Canvas.SetTop(talkBubble, Canvas.GetTop(container) + 30);
            }

            // ovdje razdvojiti po tipu
            if (!currentParams.IsMoveType)
            {
                ImageBrush imageBrush = new ImageBrush() { Stretch = Stretch.None, AlignmentX = AlignmentX.Left, AlignmentY = AlignmentY.Top };
                imageBrush.Transform = new CompositeTransform();
                imageBrush.ImageSource = currentParams.Sheet;
                container = setRectangleProperties(container, currentParams.Width, currentParams.Height, imageBrush);

                // Storyboard za animaciju
                Storyboard sb = new Storyboard();
                if (currentParams.Loop) sb.RepeatBehavior = RepeatBehavior.Forever; // odredjujem da li je na repeat

                // Animacija za talk bubble
                if (sequencePlayerInteractions.Count != 0 && sequencePlayerInteractions != null)
                {
                    fadeInAnimation.From = 0.0;
                    fadeInAnimation.To = 1.0;
                    fadeInAnimation.Completed += new EventHandler(fadeIn_complete);
                    fadeInAnimation.FillBehavior = FillBehavior.HoldEnd;
                    Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
                    Storyboard.SetTarget(fadeInAnimation, talkBubble);
                }

                // Animacija sheeta preko frameova
                int time = 0; // vrijeme kojim odredjujem trajanja frameova    
                ObjectAnimationUsingKeyFrames frm = createFrames(currentParams.Repetitions, time, currentParams.Width, currentParams.NumberOfFrames); // inicijaliziram frameove animacije sheeta                
                frm.BeginTime = new TimeSpan(0, 0, 0);
                fadeInAnimation.Duration = TimeSpan.FromMilliseconds(400);
                Storyboard.SetTarget(frm, container.Fill);
                Storyboard.SetTargetProperty(frm, new PropertyPath("(ImageBrush.Transform).(CompositeTransform.TranslateX)"));
                sb.Children.Add(frm);
                if (sequencePlayerInteractions.Count != 0 && sequencePlayerInteractions != null) sb.Children.Add(fadeInAnimation);
                sb.Completed += new EventHandler(animationInSequence_Complete);
                sb.Begin();
            }
            else {
                cameFromSequenceAnimate = true;
                AnimateMove(container, currentParams.Direction, currentParams.Steps);
            }
        }

        private void animationInSequence_Complete(object sender, EventArgs e)
        {
            if (!(currentAnimationInSequence + 1 == animationCount))
            {
                if (sequencePlayerInteractions.Count != 0 && sequencePlayerInteractions != null) { if (!(CurrentInteraction + 1 == interactionCount)) CurrentInteraction++; }
                currentAnimationInSequence++;
                AnimateCurrentAnimationInSequence(currentContainer);
            }
            else
            {
                // Goal satisfaction 
                if (CurrentCommandIDs != null && CurrentCommandIDs.Count != 0)
                {
                    bool has = true;
                    List<string> sequenceID = new List<string>();
                    foreach (AnimationParams ap in sequenceToAnimate) sequenceID.Add(ap.AnimationID);
                    for (int i = 0; i < CurrentCommandIDs.Count; i++)
                    {
                        if (!sequenceID.Contains(CurrentCommandIDs[i])) has = false;
                    }
                    if (has) { InformationObject.isCriteriaMet = true;
                               InformationObject.setCurrentGoal(InformationObject.currentGoal + 1);
                    }
                    else InformationObject.isCriteriaMet = false;
                }
                KidCode.Game.isSequenceComplete = true;
                InformationObject.isSequenceComplete = true;
                currentAnimationInSequence = 0;
                CurrentInteraction = 0;

            }
        }

现在,动画完成后,我需要以某种方式返回游戏类,基本上重复此操作。我尝试使用静态类(InformationObject) 存储有关当前目标的信息,但我无法利用它

感谢您的帮助!

0 个答案:

没有答案