简体   繁体   English

如何使此方法使用计时器和参数?

[英]How do I make this method use timers and params?

Using the same code as Awful nested timers, how do I refactor? 使用与Awful嵌套计时器相同的代码,如何重构? , though the question is substantially different enough for me to ask a new one. ,尽管这个问题本质上足以让我提出一个新问题。

Basically, I have an array of 'Movement' classes, and I want to 'Run' them all after eachother; 基本上,我有一系列的“运动”类,我想彼此“运行”。 when they're all done, I want to finally set the image to something specific. 当它们全部完成后,我想最终将图像设置为特定的图像。

I was thinking of using a foreach loop and then putting a timer on the end of the foreach before it can continue? 我当时正在考虑使用一个foreach循环,然后将计时器放在foreach的末尾才能继续? I can't get it to work though. 我不能让它工作。 Could someone help me, how do I get this method to be usable with as long a list of 'movements' as I want? 有人可以帮我吗,我如何才能在需要的“动作”列表中使用这种方法?

What I want is to have a method 我想要的是一种方法

public void SetPlayerAnimation(int location, string endsprite, params Movement[] parts)
{
    //Get the sprite object to be animated
    TranslateTarget = "Sprite" + location.ToString();
    OnPropertyChanged("TranslateTarget");

    ...stuff here that can have as many 'Movement parts' as are passed along.
    ...and waits with the next iteration until the previous one is done.
    ...but doesn't spin around in this method, so preferably using events, maybe?

    //End with a final sprite
    SetPlayerSprite(location, endsprite);
}

What I have is the code below. 我所拥有的是下面的代码。

//Three part animation
public void SetPlayerAnimation(int location, string endsprite, Movement part1, Movement part2, Movement part3)
{
    //Get the sprite object to be animated
    TranslateTarget = "Sprite" + location.ToString();
    OnPropertyChanged("TranslateTarget");

    //Start first part
    part1.Run(location);

    //Wait till its done to start the second part.
    var timer = new DispatcherTimer();
    timer.Interval = part1.duration;
    timer.Start();
    timer.Tick += (s, args) =>
        {
            //Start second part
            part2.Run(location);

            timer.Stop();

            //Wait till its done to start the third part.
            var timer2 = new DispatcherTimer();
            timer2.Interval = part2.duration;
            timer2.Start();
            timer2.Tick += (s2, args2) =>
               {
                   //Start third part
                   part3.Run(location);
                   timer2.Stop();

                   //When we're through all parts, wait till its done and set the endsprite.
                   var timer3 = new DispatcherTimer();
                   timer3.Interval = part3.duration;
                   timer3.Start();
                   timer3.Tick += (s3, args3) =>
                       {
                           //End with a final sprite
                           SetPlayerSprite(location, endsprite);
                           timer3.Stop();
                       };
               };
        };
}

maybe you can try something like this: 也许您可以尝试这样的事情:

private void RepetitionRoutine(Movement part, Action next, int location)
{
    part.Run(location);

    var timer = new DispatcherTimer();
    timer.Interval = part.duration;
    timer.Start();
    timer.Tick += (s, args) =>
        {
            next();
            timer.Stop();
        }
}

public class MovementChain
{
    Action _next;
    Movement _part;
    int _location;
    public MovementChain(Movement part, int location)
    {
        _part = part;
        _location = location;
    }

    public void setNext(Action next)
    {
        _next = next;
    }

    public void execute()
    {
        RepetitionRoutine(_part, _next, _location);
    }
}

public void SetPlayerAnimation(int location, string endsprite, params Movement[] parts)
{
    //Get the sprite object to be animated
    if(parts.Count() == 0)
        return;
    TranslateTarget = "Sprite" + location.ToString();
    OnPropertyChanged("TranslateTarget");   

    MovementChain first;
    MovementChain last;
    foreach(Movement part in parts)
    {
        MovementChain current = new MovementChain(part, location);
        if(first == null)
        {
            first = current;
            last = first;
        } 
        else
        {
            last.setNext(current.execute);
            last = current;
        }       
    }
    last.setNext(()=>SetPlayerSprite(location, endsprite));
    first.execute();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM