[英]XNA - how can i increase my sprite speed
SO i'm a beginner in programming and i need help with how to make my sprites speed increase (Add a sprint feature) 所以我是编程的初学者,我需要如何提高Sprite速度的帮助(添加sprint功能)
i have tried myself but i failed, if someone could explain how i could do it i would be very grateful. 我已经尝试过自己,但我失败了,如果有人可以解释我该怎么做,我将非常感激。
All i want done is if i press the right trigger on my Xbox controller while moving my left thumb stick right change the speed from 4 to 6. 我想要做的就是,如果在我的Xbox手柄上按下向右扳机的同时向左移动右摇杆,将速度从4更改为6。
This is the thing i tried , i realized this wouldn't work because it would just make the sprite move when i press he trigger. 这是我尝试过的事情,我意识到这是行不通的,因为当我按下他触发时,它只会使精灵移动。
if (pad1.Triggers.Right < 0.0f)
position += new Vector2(-6, 0);
if (pad1.Triggers.Right >= 0.4f)
position += new Vector2(6, 0);
I'm out of options so someone please help. 我没有选择,所以请有人帮忙。
This is fundamentally a physics problem. 从根本上讲,这是一个物理问题。 Adding a constant amount to the position is only going to move the sprite more.
向该位置添加恒定量只会使子画面移动更多。 What you're probably looking for is a way to increase the velocity over brief a period of time which is known as acceleration.
您可能正在寻找的是一种在短时间内提高速度的方法,称为加速。
const float AccelerationForce = 0.ff;
const float MaxAccelerationForce = 0.8f;;
static readonly Vector2 MaxAcceleration
= new Vector2(MaxAccelerationForce, MaxAccelerationForce);
static readonly Vector2 Acceleration = new Vector2(AccelerationForce, 0);
static readonly Vector2 Deceleration = new Vector2(-AccelerationForce, 0);
Vector _currentAcceleration = new Vector2(0, 0);
Vector _currentVelocity = new Vector2(0, 0);
Vector2 Clamp(Vector2 value, Vector2 min, Vector2 max)
{
var x = MathHelper.Clamp(value.X, min.X, max.X);
var y = MathHelper.Clamp(value.Y, min.Y, max.Y);
return new Vector2(x, y);
}
bool RequiresAcceleration(float currentForce)
{
return currentForce >= AccelerationForce;
}
var currentForce = GetCurrentForce();
// Calculate whether we need to accelerate or decelerated
var acceleration = RequiresAcceleration(currentForce)
? Acceleration
: Deceleration;
var newAcceleration = _currentAcceleration + acceleration;
// acceleration is bounded MaxAcceleration > _currentAcceleration > 0
_currentAcceleration = Clamp(newAcceleration, Vector2.Zero, MaxAcceleration);
_currentVelocity += _currentAcceleration;
// Code to move the player
if (pad1.Triggers.Right < 0.0f)
position += _currentVelocity);
if (pad1.Triggers.Right >= 0.4f)
position -= _currentVelocity;
I believe this is what you are trying to achieve. 我相信这是您要实现的目标。 Also editing a post is rather easy please edit posts rather than commenting with an update.
同样,编辑帖子也很容易,请编辑帖子,而不要评论更新。
if (pad1.Triggers.Right < 0)
// This checks if left thumbstick is less than 0.
// If it is less than zero it sets position.X += -6.
// If it is not less then it sets position.X += -4.
position.X -= (pad1.ThumbSticks.Left.X < 0) ? 6 : 4;
else if (pad1.Triggers.Right >= 0.4f) // shouldn't this be > 0.0f or > 0 ?
// This is the same as above just in the right direction.
position.X += (pad1.ThumbSticks.Left.X > 0) ? 6 : 4;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.