[英]C# image collision
嗨,大家好,基本上這是僵屍行走的代碼,現在我想做的就是單擊僵屍消失后單擊該怎么辦? 或者,如果僵屍在與其他圖像碰撞時消失,該怎么辦? 就像子彈擊中他們一樣。
public class Gaston {
public double X { get; set; }
public double Y { get; set; }
public Image Image { get; set; }
public int Colspan { get; set; }
public int Rowspan { get; set; }
}
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
DispatcherTimer dispatcherTimer;
List<Gaston> Gastons;
int timesTicked = 0;
int timesToTick = 10;
Image zombieImg;
public MainPage()
{
this.InitializeComponent();
Gastons = new List<Gaston>();
DispatcherTimerSetup();
Zombie();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Zombie()
{
Gaston newGaston = new Gaston
{
Colspan = -10,
Image = new Image(),
Rowspan = -10,
X = -10,
Y = -10
};
BitmapImage bmp;
bmp = new BitmapImage(new Uri (this.BaseUri, "/Assets/zombie.png"));
newGaston.Image.Source = bmp;
Gastons.Add(newGaston);
Grid.Children.Add(newGaston.Image);
}
private void ZombieWalks()
{
foreach (Gaston me in Gastons)
{
me.Image.Margin = new Thickness(me.Image.Margin.Left - 10, me.Image.Margin.Bottom - 10, me.Image.Margin.Right + 10, me.Image.Margin.Top + 10);
}
}
public void DispatcherTimerSetup()
{
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
}
void dispatcherTimer_Tick(object sender, object e)
{
AddZombie();
timesTicked++;
ZombieWalks();
if (timesTicked > timesToTick)
{
}
}
private void AddZombie()
{
if (timesTicked == 5)
{
Zombie();
DispatcherTimerSetup();
}
}
}
}
在圖像上掛接Tapped事件(並將IsTapEnabled設置為true)。 然后,在這種情況下,請刪除僵屍。
BitmapImage bmp;
bmp = new BitmapImage(new Uri (this.BaseUri, "/Assets/zombie.png"));
newGaston.Image.Source = bmp;
newGaston.Image.Tapped += GastonTapped;
....
private void GastonTapped(object sender, TappedRoutedEventArgs e)
{
// remove the zombie.
}
另外-我強烈建議您使用畫布來定位元素。 容易得多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.