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點纔會被添加。
聽起來像一個DetectCollisionLeft中的錯誤,您沒有發佈代碼。 – 2011-04-30 19:10:03
對不起現在更新了。 – Lokie 2011-04-30 20:51:29
不需要在標題中加入「C#XAML」。只需將其留在標籤中。 – 2011-04-30 21:40:57