Reputation: 13
I want to add to this game: https://www.mooict.com/wpf-c-tutorial-create-a-fun-balloon-popping-game-in-visual-studio/
A function ColorKeyIsDown() that is activated when some of keys: Y,R,B,G,O are pressed. So, if any of these keys is pressed, a specific balloon will be removed from the canvas. I made 5 possible tags for every newBalloon that is made, because ballonSkins integer value is from 1-5, every number declares a different background of the newBallon.
In ColorKeyIsDown() I want to identify the balloons by their tags, But it does not work.
How can I identify each different balloon(yellow/red/blue balloon)? Can I identify the balloon by it's background?
*In XAML:
<Canvas Name="MyCanvas" Focusable="True" MouseLeftButtonDown="popBalloons" Background="White" KeyDown="ColorKeyIsDown">
<Label Name="scoreLabel" FontSize="24" Content="Score: 0" Foreground="black" FontWeight="ExtraBold" Canvas.Top="527" />
</Canvas>
*In C# Code:
private void ColorKeyIsDown(object sender, KeyEventArgs e)
{
if (gameisactive)
{
foreach(var x in MyCanvas.Children.OfType<Rectangle>())
{
switch (e.Key)
{
case Key.Y:
if ((string)x.Tag == 3.ToString()) { MyCanvas.Children.Remove(x); score++; }
break;
case Key.R:
if ((string)x.Tag == 1.ToString()) { MyCanvas.Children.Remove(x); score++; }
break;
case Key.B:
if ((string)x.Tag == 5.ToString()) { MyCanvas.Children.Remove(x); score++; }
break;
case Key.G:
if ((string)x.Tag == 4.ToString()) { MyCanvas.Children.Remove(x); score++; }
break;
case Key.O:
if ((string)x.Tag == 2.ToString()) { MyCanvas.Children.Remove(x); score++; }
break;
}
}
}
}
*In C#- in gameEngine() function:
// check which skin number is selected and change them to that number
switch (balloonSkins)
{
case 1:
balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon1.png"));
break;
case 2:
balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon2.png"));
break;
case 3:
balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon3.png"));
break;
case 4:
balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon4.png"));
break;
case 5:
balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon5.png"));
break;
}
// make a new rectangle called new balloon
// inside this it has a tag called bloon, height 70 pixels and width 50 pixels and balloon image as the background
Rectangle newBalloon = new Rectangle
{
Tag = balloonSkins.ToString(),
Height = 70,
Width = 50,
Fill=balloonImage
};
Upvotes: 0
Views: 272
Reputation: 13
Someone wrote that instead of doing the KeyDown event in Canvas, to do it in the window browser. solution: onkeydown event not working on canvas?
*I changed it, and it works!
Upvotes: 1