C# 雪碧振动。怎么了?

C# 雪碧振动。怎么了?,c#,xna,C#,Xna,我的敌人精灵在到达可变末端位置后一直振动。怎么了?为什么它会振动? 此外,变量next_position永远不会为真。但是为什么呢?我希望敌人的精灵在到达当前末端位置后移动到一个新的随机末端位置 public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D enemy; Vector2 po

我的敌人精灵在到达可变末端位置后一直振动。怎么了?为什么它会振动? 此外,变量next_position永远不会为真。但是为什么呢?我希望敌人的精灵在到达当前末端位置后移动到一个新的随机末端位置

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D enemy;
    Vector2 position, endposition;
    bool next_position = false;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferWidth = 1280;
        graphics.PreferredBackBufferHeight = 720;
    }

    protected override void Initialize()
    {          
        base.Initialize();
    }

    protected override void LoadContent()
    {         
        spriteBatch = new SpriteBatch(GraphicsDevice);
        enemy = Content.Load<Texture2D>("zombie");
        Random randomstart = new Random();
        position = new Vector2(randomstart.Next(100, 200), randomstart.Next(100, 200));
        endposition = new Vector2(randomstart.Next(100, 600), randomstart.Next(100, 400));
    }

    protected override void Update(GameTime gameTime)
    {
        float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;         
        Random random = new Random();
        position.X += 5 * delta;
        position.Y += 3 * delta;

        if (next_position == true)
        {
          endposition = new Vector2(random.Next(100, 600), random.Next(100, 400));
          next_position = false;
        }

        if (Vector2.Dot(endposition - position, endposition - position) > 0 || Vector2.Dot(endposition - position, endposition - position) < 0) 
        {
          Vector2 enemyDirection = Vector2.Normalize(endposition - position) * 100f;
          position += enemyDirection * delta;
        }
        else
        {
            next_position = true;
        }

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        spriteBatch.Draw(enemy, position, Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
敌人;
向量2位置,结束位置;
bool next_位置=错误;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
graphics.PreferredBackBufferWidth=1280;
graphics.PreferredBackBufferHeight=720;
}
受保护的覆盖无效初始化()
{          
base.Initialize();
}
受保护的覆盖void LoadContent()
{         
spriteBatch=新spriteBatch(图形设备);
敌人=内容加载(“僵尸”);
随机开始=新随机();
位置=新矢量2(randomstart.Next(100200),randomstart.Next(100200));
endposition=newvector2(randomstart.Next(100600),randomstart.Next(100400));
}
受保护覆盖无效更新(游戏时间游戏时间)
{
浮点增量=(浮点)gameTime.ElapsedGameTime.TotalSeconds;
随机=新随机();
位置X+=5*delta;
位置Y+=3*delta;
如果(下一个位置==真)
{
endposition=newvector2(random.Next(100600),random.Next(100400));
下一个位置=假;
}
如果(向量2.点(端位-位置,端位-位置)>0 | |向量2.点(端位-位置,端位-位置)<0)
{
Vector2 enemyDirection=Vector2.规格化(端位-位置)*100f;
位置+=灌肠方向*三角形;
}
其他的
{
下一个位置=真;
}
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
绘制(敌人,位置,颜色,白色);
spriteBatch.End();
基础。抽签(游戏时间);
}
}

我对这句话有点困惑

if (Vector2.Dot(endposition - position, endposition - position) > 0 || Vector2.Dot(endposition - position, endposition - position) < 0)

其实就是这么简单,不需要其他检查。让我知道这是否适合你

我对这句话有点困惑

if (Vector2.Dot(endposition - position, endposition - position) > 0 || Vector2.Dot(endposition - position, endposition - position) < 0)

其实就是这么简单,不需要其他检查。让我知道这是否适合你

现在好多了,但还不够完美。敌人的精灵不再振动,但是如果它到达了末端位置,它总是向下移动一些像素。为什么它总是向右移动一些像素? 其余的工作正常

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D enemy;
    Vector2 position, endposition;
    bool next_position = false;
    int count_nextposition;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferWidth = 1280;
        graphics.PreferredBackBufferHeight = 720;
    }

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        enemy = Content.Load<Texture2D>("zombie");
        Random randomstart = new Random();
        position = new Vector2(randomstart.Next(100, 200), randomstart.Next(100, 200));
        endposition = new Vector2(randomstart.Next(100, 600), randomstart.Next(100, 400));
        count_nextposition = randomstart.Next(90, 121);
    }

    protected override void Update(GameTime gameTime)
    {
        float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
        count_nextposition -= 1;
        Random random = new Random();
        position.X += 5 * delta;
        position.Y += 3 * delta;

        if (count_nextposition <= 0)
        {
        if (next_position == true)
        {
            endposition = new Vector2(random.Next(100, 600), random.Next(100, 400));
            next_position = false;
        }
        float max_distance = 100f * delta;
        if ((position - endposition).LengthSquared() > max_distance * max_distance)
        {
           Vector2 enemyDirection = Vector2.Normalize(endposition - position) * 100f;
           position += enemyDirection * delta;
        }
        else
        {

            next_position = true;
            count_nextposition = random.Next(90, 121);
        }
        }

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        spriteBatch.Draw(enemy, position, Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
敌人;
向量2位置,结束位置;
bool next_位置=错误;
int count_nextposition;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
graphics.PreferredBackBufferWidth=1280;
graphics.PreferredBackBufferHeight=720;
}
受保护的覆盖无效初始化()
{
base.Initialize();
}
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
敌人=内容加载(“僵尸”);
随机开始=新随机();
位置=新矢量2(randomstart.Next(100200),randomstart.Next(100200));
endposition=newvector2(randomstart.Next(100600),randomstart.Next(100400));
count_nextposition=randomstart.Next(90121);
}
受保护覆盖无效更新(游戏时间游戏时间)
{
浮点增量=(浮点)gameTime.ElapsedGameTime.TotalSeconds;
count_nextposition-=1;
随机=新随机();
位置X+=5*delta;
位置Y+=3*delta;
if(计数下一个位置最大距离*最大距离)
{
Vector2 enemyDirection=Vector2.规格化(端位-位置)*100f;
位置+=灌肠方向*三角形;
}
其他的
{
下一个位置=真;
count_nextposition=random.Next(90121);
}
}
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
绘制(敌人,位置,颜色,白色);
spriteBatch.End();
基础。抽签(游戏时间);
}
}

现在好多了,但还不够完美。敌人的精灵不再振动,但是如果它到达了末端位置,它总是向下移动一些像素。为什么它总是向右移动一些像素? 其余的工作正常

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D enemy;
    Vector2 position, endposition;
    bool next_position = false;
    int count_nextposition;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferWidth = 1280;
        graphics.PreferredBackBufferHeight = 720;
    }

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        enemy = Content.Load<Texture2D>("zombie");
        Random randomstart = new Random();
        position = new Vector2(randomstart.Next(100, 200), randomstart.Next(100, 200));
        endposition = new Vector2(randomstart.Next(100, 600), randomstart.Next(100, 400));
        count_nextposition = randomstart.Next(90, 121);
    }

    protected override void Update(GameTime gameTime)
    {
        float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
        count_nextposition -= 1;
        Random random = new Random();
        position.X += 5 * delta;
        position.Y += 3 * delta;

        if (count_nextposition <= 0)
        {
        if (next_position == true)
        {
            endposition = new Vector2(random.Next(100, 600), random.Next(100, 400));
            next_position = false;
        }
        float max_distance = 100f * delta;
        if ((position - endposition).LengthSquared() > max_distance * max_distance)
        {
           Vector2 enemyDirection = Vector2.Normalize(endposition - position) * 100f;
           position += enemyDirection * delta;
        }
        else
        {

            next_position = true;
            count_nextposition = random.Next(90, 121);
        }
        }

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        spriteBatch.Draw(enemy, position, Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
敌人;
向量2位置,结束位置;
bool next_位置=错误;
int count_nextposition;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
graphics.PreferredBackBufferWidth=1280;
graphics.PreferredBackBufferHeight=720;
}
受保护的覆盖无效初始化()
{
base.Initialize();
}
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
敌人=内容加载(“僵尸”);
随机开始=新随机();
位置=新矢量2(randomstart.Next(100200),randomstart.Next(100200));
endposition=newvector2(randomstart.Next(100600),randomstart.Next(100400));
count_nextposition=randomstart.Next(90121);
}
受保护覆盖无效更新(游戏时间游戏时间)
{