经大神启发后,才知道设置几个变量尤其是bool类型的方向,之后就是简单的判断了。 // 当用户导航到此页面时执行。
protected override void OnNavigatedTo(NavigationEventArgs e)
{ } bool m_fx_x = true; //方向
bool m_fx_y = true;
int m_speed_x = ; //速度
int m_speed_y = ;
public void move(object sender,EventArgs e)
{ if (m_fx_y == true)
{
double sp_s = Canvas.GetTop(ep_1) + m_speed_y;
Canvas.SetTop(ep_1,sp_s);
}
else
{
double sp_s = Canvas.GetTop(ep_1) - m_speed_y;
Canvas.SetTop(ep_1, sp_s);
} if (m_fx_x == true)
{
double sp_s = Canvas.GetLeft(ep_1) + m_speed_x;
Canvas.SetLeft(ep_1,sp_s);
}
else
{
double sp_s = Canvas.GetLeft(ep_1) - m_speed_x;
Canvas.SetLeft(ep_1,sp_s);
} if (Canvas.GetTop(ep_1) <= || Canvas.GetTop(ep_1) >= ) //Y轴运动
{
m_fx_y = !m_fx_y;
// Random rand = new Random(); 随机y方向速度
// m_speed_y = 3 + rand.Next(0, 5);
}
if (Canvas.GetLeft(ep_1) <= || Canvas.GetLeft(ep_1) >= ) //X轴运动
{
m_fx_x = !m_fx_x;
// Random rand = new Random(); 随机x方向速度
// m_speed_x = 3 + rand.Next(0, 5);
} } private void Page_Loaded(object sender, RoutedEventArgs e)
{ DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds();
timer.Tick += new EventHandler(move);
timer.Start();
}
}