开发者

Collision detection to just add +1 once not several times the object collides

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

I basically have a cat colliding with an object (myZero) and when this happens my score should add +1 this kind of works however once the (myZero) is removed the user can still go over the location the object was and receive more points.

How can I make it so only 1 point will only ever be added.


Why would you expect this to do anything different after you've removed myZero from the canvas? Your collision-detection method just reads the properties that determine myZero's bounding box (properties that won't change just because you removed it from a collection), compare that to myCat's bounding box, and decides whether they intersect. Nothing in DetectCollisionZero will behave differently based on whether myZero is still in the LayoutRoot.Children collection.

If you want it to do something different, you're going to have to write some code that checks for the condition you're interested in (myZero is no longer supposed to be present as part of the game board) and reacts appropriately (no longer returning true when checking for collisions).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜