游戏视口焦点与y轴上的碰撞检测相对于重力(game viewport focus vs collision detection on y axis against gravity)
通过增加'y'值来减少球。
y = 100时有一面墙。
球应该与墙碰撞,两者都应该停止。
另外,我需要一个专注于球的视口。 我尝试沿球的相反方向移动墙壁。
function update() { ballupdate(); // opposite direction var viewportY = - gety('#ball'); wallupdate(viewportY); collision(); requestAnimationFrame(update); } function ballupdate() { var top = gety('#ball'); top+= 3; top = Math.min(top, 300); sety('#ball', top); } function wallupdate(viewportY) { sety('#wall', 100 + viewportY); }
如果发生碰撞,则球在墙壁上方移动。
function collision() { var balltop = gety('#ball'); var walltop = gety('#wall'); if (balltop + getheight('#ball') >= walltop) { //console.log(balltop); ballhit(walltop - getheight('#ball')); } } function ballhit(y) { sety('#ball', y); }
这导致球和墙都上下摇晃。 这是一个JSFIDDLE 。
There is a ball dropping by increasing its 'y' value.
There is a wall at y = 100.
Ball should collide with wall and both should stop.
Also, i need a viewport with a focus on ball. I try moving the wall in the opposite direction of the ball.
function update() { ballupdate(); // opposite direction var viewportY = - gety('#ball'); wallupdate(viewportY); collision(); requestAnimationFrame(update); } function ballupdate() { var top = gety('#ball'); top+= 3; top = Math.min(top, 300); sety('#ball', top); } function wallupdate(viewportY) { sety('#wall', 100 + viewportY); }
if there is a collision, ball is moved above the wall.
function collision() { var balltop = gety('#ball'); var walltop = gety('#wall'); if (balltop + getheight('#ball') >= walltop) { //console.log(balltop); ballhit(walltop - getheight('#ball')); } } function ballhit(y) { sety('#ball', y); }
This results in both ball and wall shaking up and down. Here is a JSFIDDLE.
原文:https://stackoverflow.com/questions/23727779
最满意答案
为什么不将值放在枚举本身上然后枚举?
例如,使用
System.ComponentModel
Description
属性,我们可以将该信息添加到枚举本身,例如:public enum cardCreate { [Description("General Response")] MsgType = 0, [Description("V2.0")] WSName = 2, [Description("OmegaMan")] ReplyTo = 3, [Description("Windows 10")] SourceSystem = 4, }
因此,当我们调用一个特殊的方法来枚举枚举时,我们可以在其中提取该文本并在以后适当地使用它,例如:
myextensions.GetEnumValues<cardCreate>() .Select (ceEnum => new { Original = ceEnum, IndexValue = (int)ceEnum, Text = ceEnum.GetAttributeDescription() })
投影(选择)后,动态实体将如下所示:
甜! 现在,我们将所有信息都放在易于消费的实体中,提供所需的所有信息。
什么? 您需要的不仅仅是字符串描述吗? 然后在
enum
上创建自定义属性,并根据需要返回所有项目/数据类型。 为此,请参阅我的博客文章C#在对象上使用扩展属性信息 。
以下是上例中使用的扩展方法:
public static class myextensions { public static IEnumerable<T> GetEnumValues<T>() { Type type = typeof( T ); if (!type.IsEnum) throw new Exception( string.Format("{0} is not an enum.", type.FullName )); FieldInfo[] fields = type.GetFields( BindingFlags.Public | BindingFlags.Static ); foreach (var item in fields) yield return (T)item.GetValue( null ); } /// <summary>If an attribute on an enumeration exists, this will return that /// information</summary> /// <param name="value">The object which has the attribute.</param> /// <returns>The description string of the attribute or string.empty</returns> public static string GetAttributeDescription( this object value ) { string retVal = string.Empty; try { retVal = value.GetType() .GetField( value.ToString() ) .GetCustomAttributes( typeof( DescriptionAttribute ), false ) .OfType<DescriptionAttribute>() .First() .Description; } catch (NullReferenceException) { //Occurs when we attempt to get description of an enum value that does not exist } finally { if (string.IsNullOrEmpty( retVal )) retVal = "Unknown"; } return retVal; } }
Why not place the values onto the enum itself and then enumerate?
For example using
System.ComponentModel
Description
attribute we can add that information to the enum itself such as:public enum cardCreate { [Description("General Response")] MsgType = 0, [Description("V2.0")] WSName = 2, [Description("OmegaMan")] ReplyTo = 3, [Description("Windows 10")] SourceSystem = 4, }
So when we call a special method to enumerate the enum where we can extract that text and use it appropriately later such as:
myextensions.GetEnumValues<cardCreate>() .Select (ceEnum => new { Original = ceEnum, IndexValue = (int)ceEnum, Text = ceEnum.GetAttributeDescription() })
The dynamic entity will look like this after the projection (the select):
Sweet! Now we have all the information in a easy consumable entity which provides all the information needed.
What? You need more than a string description? Then create a custom attribute on the
enum
and have all items/types of data to return as needed. For that see my blog article C# Using Extended Attribute Information on Objects.
Here are the extension methods used in the above example:
public static class myextensions { public static IEnumerable<T> GetEnumValues<T>() { Type type = typeof( T ); if (!type.IsEnum) throw new Exception( string.Format("{0} is not an enum.", type.FullName )); FieldInfo[] fields = type.GetFields( BindingFlags.Public | BindingFlags.Static ); foreach (var item in fields) yield return (T)item.GetValue( null ); } /// <summary>If an attribute on an enumeration exists, this will return that /// information</summary> /// <param name="value">The object which has the attribute.</param> /// <returns>The description string of the attribute or string.empty</returns> public static string GetAttributeDescription( this object value ) { string retVal = string.Empty; try { retVal = value.GetType() .GetField( value.ToString() ) .GetCustomAttributes( typeof( DescriptionAttribute ), false ) .OfType<DescriptionAttribute>() .First() .Description; } catch (NullReferenceException) { //Occurs when we attempt to get description of an enum value that does not exist } finally { if (string.IsNullOrEmpty( retVal )) retVal = "Unknown"; } return retVal; } }
相关问答
更多-
在C#中枚举转换的字符串(String to enum conversion in C#)[2023-07-18]
我建议建立一个Dictionary将友好的名字映射为枚举常量,并在元素本身中使用正常的命名约定。 enum Operation{ Equals, NotEquals, LessThan, GreaterThan }; var dict = new Dictionary { { "Equals", Operation.Equals }, { "Not Equals", Operation.NotEquals }, ... -
是的,您可以使用GetValues方法 var values = Enum.GetValues(typeof(Foos)); 或打字版本 var values = Enum.GetValues(typeof(Foos)).Cast
(); 很久以前,我刚刚在私人图书馆增加了一个帮助函数 public static class EnumUtil { public static IEnumerable GetValues () { return Enum.GetValues( ... -
您可以使用Enum.GetValues(typeof(YourEnum))来获取您的枚举的所有值。 然后你可以使用它为你的适配器创建你的数组。 var enumValues = Enum.GetValues(typeof(YOUR_ENUM)); var arrayForAdapter = enumValues.Cast
().Select(e => e.ToString()).ToArray(); var adapter = new ArrayAdapter (conte ... -
您只需要Enum.GetNames方法, Enum.GetValues将结果作为EnumType而不是字符串。 string[] names = Enum.GetNames(typeof (MPriority)); 我建议你只使用GetNames ,不要调用GetValues并将其转换为注释中建议的字符串。 You just need Enum.GetNames method, Enum.GetValues gives the result as EnumType rather than string. ...
-
使用mod运算符 。 string[] assignments = new string[] {"A", "B", "C", "D", "E", "F"}; for (int i = 0; i < 99; i++) { Console.WriteLine(assignments[i % assignments.Length]); } .net小提琴 Use the mod operator. string[] assignments = new string[] {"A", "B", "C", " ...
-
为什么在CI中的结构中分配字段会得到垃圾字符?(Why on assigning fields in structures in C I get rubbish characters?)[2023-01-11]
语句struct包含指向其他代码分配的内存的指针。 函数mysql_free_result(result)释放了分配的内存。 您应该在调用之前将结果复制到您自己的存储中。 The statement struct contains pointers to memory allocated by other code. The function mysql_free_result(result) frees that allocated memory. You should copy the results ... -
我可以在字符串数组中分配我的集合中的值吗?(Can I assign values from my collection in a string array..?? (c#))[2023-01-16]
GetValue不是NameValueCollection的成员。 该函数称为Get 。 如果要使用[]运算符,则不必调用Get函数。 所以打电话 arr[i] = DataRowColl[i]; 要么: arr[i] = DataRowColl.Get(i); GetValue is not a member of NameValueCollection. The function is called Get. If you want to use the [] operator, you don't ... -
c#遍历枚举的所有字段,从字符串数组中分配值(c# loop through all fields of enum assigning values from string array)[2023-10-01]
为什么不将值放在枚举本身上然后枚举? 例如,使用System.ComponentModel Description属性,我们可以将该信息添加到枚举本身,例如: public enum cardCreate { [Description("General Response")] MsgType = 0, [Description("V2.0")] WSName = 2, [Description("OmegaMan")] ReplyTo = 3, [Description(" ... -
你可以使用这样的东西 else if (fieldType.IsArray) { string[] values = ReadUntil(']'); var elementType = fieldType.GetElementType(); if (elementType == typeof(string)) thisField.SetValue(newObj, values); else { var actualValues = Ar ...
-
在Enum值上使用Where(Using Where on Enum Values)[2022-12-14]
太多的LINQ思维方式,所以有时候我们会忘记它们来自哪里:)从第一个.NET版本开始, Enum.GetValues(typeof(MyEnum))实际上返回MyEnum[] ,所以你需要的不是OfType ,也不是Cast ,但简单的C# cast : string s = string.Join(", ", ((MyEnum[])Enum.GetValues(typeof(MyEnumType))) .Where(x => (MyIntValue & (int)x) != 0)); 由于这种类 ...