简体   繁体   English

C#Sprite运动pos-x double

[英]C# sprite movement pos-x double

Im working on a XNA project. 我正在研究XNA项目。 I have drawn a background(skyline) and i want to move a cloud from left to right. 我画了一个背景(天际线),我想从左到右移动云。 This works whenever i use int, but the movement is too fast so i want to use a double. 每当我使用int时,此方法都有效,但是运动速度太快,因此我想使用double。 Is it possible to move the cloud on the x with a double? 是否可以双倍移动x上的云?

public int GetRandomSpeedX() // Random speed pos-x
{            
        int speedX = r.Next(1, 100);
        return speedX;
}

protected override void Update(GameTime gameTime)
{
        if (cloudX <= 500)
        {
            cloudX += GetRandomSpeedX();
        }
        else
        {
            cloudX = 0;                
        }

        base.Update(gameTime);
}

If i use a double it says: cannot implicitly convert type double to int 如果我使用double,它会说:无法将double类型隐式转换为int

and i cant change the cloudX to a double because the spriteBatch function wants int only! 而且我无法将cloudX更改为double,因为spriteBatch函数仅需要int!

any help? 有什么帮助吗?

My XNA might be a bit rusty, but you will need two variables for your Cloud, Position and Speed. 我的XNA可能有点生锈,但是您的Cloud需要两个变量,Position和Speed。 You will change the Speed each update to increase the Position of the cloud. 您将更改每次更新的速度以增加云的位置。

Here is a basic implementation. 这是一个基本的实现。 You'll need to setup up the initial Position and Speed in your game initialization. 您需要在游戏初始化时设置初始位置和速度。

private float NextRandomFloat(double max, double min)
{
    var number = r.NextDouble() * (max - min) + min;

    return (float) number;
}

private Vector2 GetRandomSpeed(float dx, float dy)
{
    var speedX = NextRandomFloat(2.0, 0.5) * dx;
    var speedY = NextRandomFloat(2.0, 0.5) * dy;
    var vector = new Vector2(speedX, speedY);

    return vector;
}

private Vector2 cloudSpeed;
private Vector2 cloudPosition;

protected override void Update(GameTime gameTime)
{
        if (cloudPosition.X <= 500)
        {
            // Tinker with the dx to manage acceleration
            // Consider using MathHelper.Clamp for a maximum speed.
            cloudSpeed += GetRandomSpeed(1.0f, 0);
        }
        else
        {
            cloudSpeed.X = 0;
        }

        cloudPosition += cloudSpeed;

        base.Update(gameTime);
}

private void DrawCloud()
{
    spriteBatch.Draw(cloudTexture, cloudPosition, Color.White);
}

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

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