我使用UWP制作了一个meme游戏,但在我改变了某些东西之后,游戏总是在选择了两张牌后崩溃并带我到App.g.i.cs那个地方:
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached)global::System.Diagnostics.Debugger.Break();
};
我不知道是什么导致它,这是按钮背后的代码:
if (IsFirstOrSecondCard && IsThirdCardChosed == false)
{
SecondCardChosen(sender);
}
else if (IsThirdCardChosed == false)
{
FirstCardChosen(sender);
}
else
ThirdPlusButton();
这是传播给整个16个按钮的方法:
public async void FirstCardChosen(object sender)
{
MemoButton[0] = (Button)sender;
IsFirstOrSecondCard = !IsFirstOrSecondCard;
if (MemoButton[0] == R1C1) { R1C1img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R1C1img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R1C1img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R1C2) { R1C2img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R1C2img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R1C2img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R1C3) { R1C3img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R1C3img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R1C3img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R1C4) { R1C4img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R1C4img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R1C4img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R2C1) { R2C1img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R2C1img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R2C1img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R2C2) { R2C2img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R2C2img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R2C2img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R2C3) { R2C3img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R2C3img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R2C3img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R2C4) { R2C4img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R2C4img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R2C4img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R3C1) { R3C1img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R3C1img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R3C1img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R3C2) { R3C2img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R3C2img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R3C2img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R3C3) { R3C3img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R3C3img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R3C3img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R3C4) { R3C4img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R3C4img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R3C4img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R4C1) { R4C1img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R4C1img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R4C1img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R4C2) { R4C2img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R4C2img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R4C2img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R4C3) { R4C3img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R4C3img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R4C3img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
else if (MemoButton[0] == R4C4) { R4C4img.Visibility = Visibility.Visible; await System.Threading.Tasks.Task.Delay(4000, SecondButtonTriggered.Token); R4C4img.Visibility = Visibility.Collapsed; TempImageLocation[0] = R4C4img; IsFirstOrSecondCard = !IsFirstOrSecondCard; }
Score.Text = "Score" + Success_Counter.ToString();
}
第二张牌:
public async void SecondCardChosen(object sender)
{
IsFirstOrSecondCard = false;
IsThirdCardChosed = true;
Dictionary<Button, string> HiddenCardDiscovery = new Dictionary<Button, string>();
HiddenCardDiscovery.Add(R1C1, "OriginalPepe");
HiddenCardDiscovery.Add(R1C2, "PixelPepe");
HiddenCardDiscovery.Add(R1C3, "IronManPepe");
HiddenCardDiscovery.Add(R1C4, "CoolPepe");
HiddenCardDiscovery.Add(R2C1, "UrselfPepe");
HiddenCardDiscovery.Add(R2C2, "WinkPepe");
HiddenCardDiscovery.Add(R2C3, "OriginalPepe");
HiddenCardDiscovery.Add(R2C4, "HappyPepe");
HiddenCardDiscovery.Add(R3C1, "PixelPepe");
HiddenCardDiscovery.Add(R3C2, "MonsterPepe");
HiddenCardDiscovery.Add(R3C3, "CoolPepe");
HiddenCardDiscovery.Add(R3C4, "IronManPepe");
HiddenCardDiscovery.Add(R4C1, "WinkPepe");
HiddenCardDiscovery.Add(R4C2, "UrselfPepe");
HiddenCardDiscovery.Add(R4C3, "MonsterPepe");
HiddenCardDiscovery.Add(R4C4, "HappyPepe");
MemoButton[1] = (Button)sender;
if (MemoButton[1] == R1C1) { TempImageLocation[1] = R1C1img; }
else if (MemoButton[1] == R1C2) { TempImageLocation[1] = R1C2img; }
else if (MemoButton[1] == R1C3) { TempImageLocation[1] = R1C3img; }
else if (MemoButton[1] == R1C4) { TempImageLocation[1] = R1C4img; }
else if (MemoButton[1] == R2C1) { TempImageLocation[1] = R2C1img; }
else if (MemoButton[1] == R2C2) { TempImageLocation[1] = R2C2img; }
else if (MemoButton[1] == R2C3) { TempImageLocation[1] = R2C3img; }
else if (MemoButton[1] == R2C4) { TempImageLocation[1] = R2C4img; }
else if (MemoButton[1] == R3C1) { TempImageLocation[1] = R3C1img; }
else if (MemoButton[1] == R3C2) { TempImageLocation[1] = R3C2img; }
else if (MemoButton[1] == R3C3) { TempImageLocation[1] = R3C3img; }
else if (MemoButton[1] == R3C4) { TempImageLocation[1] = R3C4img; }
else if (MemoButton[1] == R4C1) { TempImageLocation[1] = R4C1img; }
else if (MemoButton[1] == R4C2) { TempImageLocation[1] = R4C2img; }
else if (MemoButton[1] == R4C3) { TempImageLocation[1] = R4C3img; }
else if (MemoButton[1] == R4C4) { TempImageLocation[1] = R4C4img; }
string FirstButtonImageName = "";
string SecondButtonImageName = "";
HiddenCardDiscovery.TryGetValue(MemoButton[0], out FirstButtonImageName);
HiddenCardDiscovery.TryGetValue(MemoButton[1], out SecondButtonImageName);
if (FirstButtonImageName == SecondButtonImageName)
{
Success_Counter += 1;
Score.Text = "Score: " + Success_Counter.ToString();
TempImageLocation[0].Visibility = Visibility.Visible;
TempImageLocation[1].Visibility = Visibility.Visible;
MemoButton[0].IsEnabled = !MemoButton[0].IsEnabled;
MemoButton[1].IsEnabled = !MemoButton[1].IsEnabled;
if (Success_Counter == 8)
{
Frame.Navigate(typeof(WonPage));
}
}
else
{
TempImageLocation[0].Visibility = Visibility.Visible;
TempImageLocation[1].Visibility = Visibility.Visible;
await System.Threading.Tasks.Task.Delay(2000);
TempImageLocation[0].Visibility = Visibility.Collapsed;
TempImageLocation[1].Visibility = Visibility.Collapsed;
}
Score.Text = "Score: " + Success_Counter.ToString();
IsThirdCardChosed = false;
}
第三名:
public void ThirdPlusButton ()
{
Score.Text = "Please don't press 3 buttons at the same time!"; // Add flyouut box everytime a third button gets clicked at the next update!
}
全局变量:
int Success_Counter = 0;
Button[] MemoButton = new Button[2];
bool IsFirstOrSecondCard = false;
bool IsThirdCardChosed = false;
Image[] TempImageLocation = new Image[2];
CancellationTokenSource SecondButtonTriggered = new CancellationTokenSource();
在您发表评论之前,我尝试过并帮助过: 在第二种方法(SecondCardChosen)中,在它开始之前将延迟设置为5秒:
public async void SecondCardChosen(object sender)
{
IsFirstOrSecondCard = false;
IsThirdCardChosed = true;
await System.Threading.Tasks.Task.Delay(5000);
但是,如果我没有激活一个大的延迟,游戏会崩溃并把我带到那里,尝试使用谷歌尝试寻找一个闷热,我只是没有找到任何,任何人都可以指示我正确的方向?
编辑:忘了添加xaml代码:
<Page
x:Class="MemoryGame.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MemoryGame"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<Style TargetType="Image" x:Key="HideableImages">
<Setter Property="Visibility" Value="Collapsed" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="Stretch" Value="Fill" />
<Setter Property="Margin" Value="3" />
</Style>
<Style x:Key="QuesionMarkButton" TargetType="Button">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="FontFamily" Value="Segoe MDL2 Assets"/>
<Setter Property="Content" Value=""/>
<Setter Property="FontSize" Value="72"/>
</Style>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<TextBlock Name="Title" FontSize="36" Text="Memory game by Ido levi" Grid.ColumnSpan="4"/>
<Button Name="R1C1" BorderThickness ="0,0,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="1" Grid.Column="0" Click="R1C1_Click" />
<Button Name="R1C2" BorderThickness="3,0,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="1" Grid.Column="1" Click="R1C2_Click_1" />
<Button Name="R1C3" BorderThickness="3,0,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="1" Grid.Column="2" Click="R1C3_Click" />
<Button Name="R1C4" BorderThickness="3,0,0,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="1" Grid.Column="3" Click="R1C4_Click" />
<Button Name="R2C1" BorderThickness="0,3,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="2" Grid.Column="0" Click="R2C1_Click" />
<Button Name="R2C2" BorderThickness="3,3,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="2" Grid.Column="1" Click="R2C2_Click"/>
<Button Name="R2C3" BorderThickness="3,3,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="2" Grid.Column="2" Click="R2C3_Click"/>
<Button Name="R2C4" BorderThickness="3,3,0,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="2" Grid.Column="3" Click="R2C4_Click" />
<Button Name="R3C1" BorderThickness="0,3,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="3" Grid.Column="0" Click="R3C1_Click"/>
<Button Name="R3C2" BorderThickness="3,3,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="3" Grid.Column="1" Click="R3C2_Click" />
<Button Name="R3C3" BorderThickness="3,3,3,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="3" Grid.Column="2" Click="R3C3_Click" />
<Button Name="R3C4" BorderThickness="3,3,0,3" Style="{StaticResource QuesionMarkButton}" Grid.Row="3" Grid.Column="3" Click="R3C4_Click" />
<Button Name="R4C1" BorderThickness="0,3,3,0" Style="{StaticResource QuesionMarkButton}" Grid.Row="4" Grid.Column="0" Click="R4C1_Click"/>
<Button Name="R4C2" BorderThickness="3,3,3,0" Style="{StaticResource QuesionMarkButton}" Grid.Row="4" Grid.Column="1" Click="R4C2_Click" />
<Button Name="R4C3" BorderThickness="3,3,3,0" Style="{StaticResource QuesionMarkButton}" Grid.Row="4" Grid.Column="2" Click="R4C3_Click"/>
<Button Name="R4C4" BorderThickness="3,3,0,0" Style="{StaticResource QuesionMarkButton}" Grid.Row="4" Grid.Column="3" Click="R4C4_Click"/>
<Image Name="R1C1img" Grid.Row="1" Grid.Column="0" Style="{StaticResource HideableImages}" Source="Assets/OriginalPepe.jpg"/>
<Image Name="R1C2img" Grid.Row="1" Grid.Column="1" Style="{StaticResource HideableImages}" Source="Assets/PixelPepe.jpg"/>
<Image Name="R1C3img" Grid.Row="1" Grid.Column="2" Style="{StaticResource HideableImages}" Source="Assets/IronManPepe.jpg"/>
<Image Name="R1C4img" Grid.Row="1" Grid.Column="3" Style="{StaticResource HideableImages}" Source="Assets/CoolPepe.jpg"/>
<Image Name="R2C1img" Grid.Row="2" Grid.Column="0" Style="{StaticResource HideableImages}" Source="Assets/UrselfPepe.png"/>
<Image Name="R2C2img" Grid.Row="2" Grid.Column="1" Style="{StaticResource HideableImages}" Source="Assets/WinkPepe.jpg"/>
<Image Name="R2C3img" Grid.Row="2" Grid.Column="2" Style="{StaticResource HideableImages}" Source="Assets/OriginalPepe.jpg"/>
<Image Name="R2C4img" Grid.Row="2" Grid.Column="3" Style="{StaticResource HideableImages}" Source="Assets/HappyPepe.jpg"/>
<Image Name="R3C1img" Grid.Row="3" Grid.Column="0" Style="{StaticResource HideableImages}" Source="Assets/PixelPepe.jpg"/>
<Image Name="R3C2img" Grid.Row="3" Grid.Column="1" Style="{StaticResource HideableImages}" Source="Assets/MonsterPepe.jpg"/>
<Image Name="R3C3img" Grid.Row="3" Grid.Column="2" Style="{StaticResource HideableImages}" Source="Assets/CoolPepe.jpg"/>
<Image Name="R3C4img" Grid.Row="3" Grid.Column="3" Style="{StaticResource HideableImages}" Source="Assets/IronManPepe.jpg"/>
<Image Name="R4C1img" Grid.Row="4" Grid.Column="0" Style="{StaticResource HideableImages}" Source="Assets/WinkPepe.jpg"/>
<Image Name="R4C2img" Grid.Row="4" Grid.Column="1" Style="{StaticResource HideableImages}" Source="Assets/UrselfPepe.png"/>
<Image Name="R4C3img" Grid.Row="4" Grid.Column="2" Style="{StaticResource HideableImages}" Source="Assets/MonsterPepe.jpg"/>
<Image Name="R4C4img" Grid.Row="4" Grid.Column="3" Style="{StaticResource HideableImages}" Source="Assets/HappyPepe.jpg"/>
<TextBlock Name="Score" Text="Score:" Grid.Column="3" FontSize="30" />
</Grid>