不能修改XNA矢量组成 [英] Can't modify XNA Vector components

查看:215
本文介绍了不能修改XNA矢量组成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为雪碧类,ballSprite是类的一个实例。雪碧有一个Vector2属性调用的位置。

I have a class called Sprite, and ballSprite is an instance of that class. Sprite has a Vector2 property called Position.

我试图增加Vector的X成分,像这样:

I'm trying to increment the Vector's X component like so:

ballSprite.Position.X++;



但它会导致这个错误:

but it causes this error:

Cannot modify the return value of 'WindowsGame1.Sprite.Position' because it is not a variable

这难道不是可以设置这样的组件? X和Y字段的工具提示说获取或设置......所以我不明白为什么这是行不通的。

Is it not possible to set components like this? The tooltip for the X and Y fields says "Get or set ..." so I can't see why this isn't working.

推荐答案

的问题是, ballSprite.Position 返回一个结构,所以当你访问它,它因值语义创建它的一个副本。 ++试图修改该值,但是它会修改临时副本 - 而不是存储在您的雪碧实际结构

The problem is that ballSprite.Position returns a struct, so when you access it, it creates a copy of it due to the value semantics. ++ attempts to modify that value, but it'll be modifying a temporary copy - not the actual struct stored in your Sprite.

您必须采取从阅读价值的位置,并把它变成一个局部变量,改变局部变量,然后分配局部变量回原来的位置,或者用做(也许隐藏它的一些 IncrementX 法)

You have to take the value from reading the Position and put it into a local variable, change that local variable, and then assign the local variable back into Position, or use some other, similar way of doing it (maybe hiding it as some IncrementX method).

Vector2D v;
v = ballSprite.Position;
v.X++;
ballSprite.Position = v;



另外一个更通用的解决方案,可能是另外一个Vector2添加到您的位置。 +运算符重载,所以你可以创建一个你想要的变化的新载体,然后添加到载体,而不是在一个时间更新indiviudal组件之一。

Another, more generic solution, might be to add another Vector2 to your Position. The + operator is overloaded, so you could create a new vector with the change you want, and then add that to the vector instead of updating the indiviudal components one at a time.

这篇关于不能修改XNA矢量组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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