, ayrıca Vector2.UnitX kullanabilirsiniz:
ballSprite.Position.X += 1; // error
ballSprite.Position += Vector2.UnitX; // solution
Eğer yönlü taşımak istediğinizde çok kullanışlıdır.Örneğin, yatay sadece taşımak istiyorsanız: Bu örnekte
Vector2 offset = new Vector2(2, 4);
ballsprite.Position += offset * Vector2.UnitX;
,
speed.Y
değeri sprite pozisyona eklenmez. Niye ya?
offset == new Vector2(2, 4)
UnitX == new Vector2(1, 0)
--------------------------------
The above Vectors are multiplied
which results into the following
--------------------------------
offset * UnitX
(X: 2, Y: 4) * (X: 1, Y: 0)
(X: 2 * 1, ... Y: 4 * 0)
(X: 2, Y: 0) <-- result
bu şekilde yapmanın bir diğer avantajı okunabilirliği olduğunu. En azından düşündüğüm bu. Kendiniz için bakınız: Elbette
// multiple ways of incrementing ballspeeds X coordinate.
ballSprite.Position += Vector2.UnitX;
ballSprite.Position += new Vector2(1, 0);
ballSprite.Position = new Vector2(ballSprite.Position.X + 1, ballSprite.Position.Y);
Vector2 temp = ballSprite.Position;
temp.X++;
ballSprite.Position = temp;
, dikey hareketi için de bir Vector2.UnitY yoktur. Bu statik alanları Vector2.Zero ve Vector2.One ile birleştirin ve anlaşılması kolay bir kod yazabilirsiniz.
I Vector2
s ve yön ile çalışıyorum, ben aşağıdaki tablo:
<--- X coordinate --->
-1 0 +1
| |
-Vector2.One | -Vector2.UnitY | -1
(X:-1, Y:-1) | (X: 0, Y:-1) |
| | ^
----------------+----------------+---------------- |
| |
-Vector2.UnitX | Vector2.Zero | +Vector2.UnitX 0 Y coordinate
(X:-1, Y: 0) | (X: 0, Y: 0) | (X:+1, Y: 0)
| | |
----------------+----------------+---------------- V
| |
| +Vector2.UnitY | +Vector2.One +1
| (X: 0, Y:+1) | (X:+1, Y:+1)
| |
ballSprite.Position.X = ballSprite.Position.X + 1; ya çalışmıyor - aynı hata. :(Bu tam olarak aynı ballSprite.Position.X ++ ile aynı değil mi? –
Üzgünüz, ben orada düz düşünmüyordum.Onu çözdüm. –
Tamam, çözülmüş gibi görünüyor. Bu sorun biraz sinir bozucu. Teşekkürler, muhtemelen sadece iki vektör ekleyeceğim. :) –