2011-04-30 123 views
1
<UserControl x:Class="CatGame.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="480" d:DesignWidth="640" KeyDown="UserControl_KeyDown"> 
    <Canvas x:Name="LayoutRoot" Background="white"> 
    <Image Source="level1.jpg"></Image> 
    <TextBlock FontSize="24" Canvas.Left="700" Canvas.Top="90" Name="score">/TextBlock>   
    </Canvas> 
</UserControl> 



if (DetectCollisionZero(myCat, myZero)) 
      { 
      int scoreAsInt; 
      if (Int32.TryParse(score.Text, out scoreAsInt) != null) 
       { 
        scoreAsInt = scoreAsInt + 1; 
        score.Text = scoreAsInt.ToString(); 
       } 
       LayoutRoot.Children.Remove(myZero); 
      } 

    public bool DetectCollisionZero(ContentControl myCat, ContentControl myZero) 
      { 

       Rect myCatRect = new Rect(
         new Point(Convert.ToDouble(myCat.GetValue(Canvas.LeftProperty)), 
               Convert.ToDouble(myCat.GetValue(Canvas.TopProperty))), 
             new Point((Convert.ToDouble(myCat.GetValue(Canvas.LeftProperty)) + myCat.ActualWidth), 
               (Convert.ToDouble(myCat.GetValue(Canvas.TopProperty)) + myCat.ActualHeight)) 
          ); 

       Rect myZeroRect = new Rect(
      new Point(Convert.ToDouble(myZero.GetValue(Canvas.LeftProperty)), 
              Convert.ToDouble(myZero.GetValue(Canvas.TopProperty))), 
            new Point((Convert.ToDouble(myZero.GetValue(Canvas.LeftProperty)) + myZero.ActualWidth), 
              (Convert.ToDouble(myZero.GetValue(Canvas.TopProperty)) + myZero.ActualHeight)) 
          ); 

       myCatRect.Intersect(myZeroRect); 
       return !(myCatRect == Rect.Empty); 
      } 

我基本上有一只猫与一个对象(myZero),当发生这种情况我的分数应该添加+1这类工程然而,一旦(碰撞myZero)被删除,用户仍然可以越过对象所在的位置并获得更多的点数。碰撞检测只需添加+1一次不是几倍的对象碰撞

我怎样才能让它只有1点才会被添加。

+0

听起来像一个DetectCollisionLeft中的错误,您没有发布代码。 – 2011-04-30 19:10:03

+0

对不起现在更新了。 – Lokie 2011-04-30 20:51:29

+0

不需要在标题中加入“C#XAML”。只需将其留在标签中。 – 2011-04-30 21:40:57

回答

0

从画布中删除myZero后,为什么你会期望这样做会有所不同?您的碰撞检测方法只是读取确定myZero边界框的属性(不会因为您将其从集合中移除而不会改变的属性),将其与myCat的边界框进行比较,并确定它们是否相交。根据myZero是否仍在LayoutRoot.Children集合中,DetectCollisionZero中的任何内容的行为都不相同。

如果你希望它做一些不同的事情,你将不得不编写一些代码来检查你感兴趣的条件(myZero已不再作为游戏板的一部分)和作出适当反应(检查冲突时不再返回true)。