如何在WPF中创建掉落的对象 [英] How to create falling objects in WPF

查看:111
本文介绍了如何在WPF中创建掉落的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在尝试构建一个简单的应用程序,以模拟屏幕上的雨水.我不是高级程序员,所以我想出了这种算法:

Hello,

I''m trying to build a simple application that will simulate rain on the screen. I''m not an advanced programer so I came up with this algorithm for one drop:

public partial class MainWindow : Window
    {
        #region Member Variables
        public float x, y, xvel, yvel;
        int gamewidth = 640;
        int gameHeight = 480;
        Random r = new Random();
        public System.Windows.Threading.DispatcherTimer timer1;
        #endregion Member Variables
        
        public MainWindow()
        {
            InitializeComponent();
            timer1 = new System.Windows.Threading.DispatcherTimer();
            timer1.Interval = TimeSpan.FromSeconds(0.01); // 1 second interval 
            timer1.Tick += TimerTicked; // Event for handling the fetching data 
        }

        public void MoveRain()
        {
           
            if (y < gameHeight - rainDrop.ActualHeight)
            {
                y += yvel;
                Canvas.SetLeft(rainDrop, x);
                Canvas.SetTop(rainDrop, y);
                timer1.Start();
            }
            else
            {
                x = r.Next(gamewidth);
                y = 0;
                yvel = r.Next(4) + 1;
                timer1.Start();
            }
        }

        private void SetStartPosition()
        {
            x = r.Next(gamewidth);
            y = 0;
            yvel = r.Next(4) + 1;
            timer1.Start();
        }
        
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            SetStartPosition();
        }

        private void TimerTicked(object sender, EventArgs args)
        {
            timer1.Stop();
            MoveRain();
        } 

    }



但是我想要的是让多个液滴出现在屏幕上,并在不同的时间掉落.你有什么想法或建议吗? Thx!



But what I want is to have several drops appear on the screen and fall at different times. Do you have any ideas or suggestions? Thx!

推荐答案

如果我正确理解,为此目的,您使用WPF平台.如果有这项权利,对我而言,最好使用XAML创建这样的动画.
您可以借助
If i rightly understood, for this purpose you using WPF platform. If that rights, for me, it would be preferable to create such annimation using XAML.
You can implement it with help of
Storyboard's

来实现和Storyboard中的KeyFrameAnnimation.
您可以访问此链接并从中获得一些有用的信息:
^ ]

and KeyFrameAnnimation inside it Storyboard.
You can visit this link and get some useful things from that:
http://xceed.com/CS/blogs/xamlhero/archive/2009/02/16/animation-in-xaml-to-get-you-started.aspx[^]


这篇关于如何在WPF中创建掉落的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆