简体   繁体   English

在C#中实现类似jQuery的animate()函数的功能

[英]Implementing something like jQuery's animate() function in C#

I'm new to C# and using it to write my next game. 我是C#的新手,并用它编写下一个游戏。 Coming from a background in C++, Java and JavaScript, I'm excited to use C#'s anonymous functions to simplify tasks that weren't so simple in C++ and Java. 来自C ++,Java和JavaScript的背景,我很高兴使用C#的匿名函数来简化在C ++和Java中不那么简单的任务。

Particularly, I'd like to implement a system in my code that works similar to jQuery's animate() function: easing a set of variables from their initial values to a new set of values, and then executing a callback upon completion. 特别是,我想在我的代码中实现一个类似于jQuery animate()函数的系统:将一组变量从其初始值简化为一组新值,然后在完成时执行回调。 I understand how to use anonymous functions and callbacks in C#, so that's no problem, but I'm not sure how I'd go about passing properties and their associated values to a function. 我知道如何在C#中使用匿名函数和回调,所以这没问题,但是我不确定如何将属性及其相关值传递给函数。

As an example, my goal would be something like this, to ease a player's x coordinate from the initial value to the target value of 500, while easing his scale value to 2.0f, over the course of 10.0 seconds: 举例来说,我的目标是这样的:在10.0秒的过程中,将玩家的x坐标从初始值缓和到目标值500,同时将其比例值减小到2.0f:

game.animate( { player.x: 500, player.scale: 2.0f }, 10.0f, delegate() { Console.WriteLine("Callback has been called!"); }

(That won't compile, of course, but the point of the example is to describe the syntax I'm hoping to approximate.) (当然,这不会编译,但是示例的要点是描述我希望近似的语法。)

According to other Stack Overflow questions and answers, it's not possible to pass property references to functions in C#, so passing player.x and player.scale to the function would apparently be impossible in this way, which means I have to rethink my strategy for implementing this. 根据其他Stack Overflow问答,无法将属性引用传递给C#中的函数,因此以这种方式将player.x和player.scale传递给该函数显然是不可能的,这意味着我必须重新考虑自己的策略实现这一点。

Is there any other way I could go about implementing a system like this, given C#'s constraints about passing property references to functions? 考虑到C#关于将属性引用传递给函数的约束,我还有其他方法可以实现这样的系统吗? Or do I need to just give up on this idea and use less flexible means of easing property values over time? 还是我只需要放弃这个想法,并使用不太灵活的方式随着时间的推移缓和属性值?

With a little experimentation and help from Shoe's comments, I came up with a solution. 经过一些试验和Shoe评论的帮助,我提出了一个解决方案。

First, the assumption is made that all properties will be floats. 首先,假设所有属性均为浮点型。 Secondly, each property (player.x, player.scale, etc.) will need an explicitly-defined setter (player.SetX(), player.SetScale(), etc.) which takes in a single float and returns void. 其次,每个属性(player.x,player.scale等)将需要一个显式定义的setter(player.SetX(),player.SetScale()等),该setter带有单个float并返回void。

After that, the main code can be defined like so: 之后,可以这样定义主要代码:

public delegate void PropertySetter(float newVal);

public void Animate(PropertySetter setter, float time, float valToSet, Action callback=null)
{
   // the implementation-specific code for interpolating values will go here.  and call "callback" when complete, if it's non-null.
}

And to use the Animate() function, all we need to do is call it like so: 要使用Animate()函数,我们需要做的就是这样调用它:

Animate(player.SetX, 10.0f, 500.0f);
Animate(player.SetScale, 10.0f, 2.0f);

I'm going to change it later to allow those calls to happen in a single call, but this serves as a simple example. 我稍后将对其进行更改,以允许这些调用在单个调用中发生,但这只是一个简单的示例。

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

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