Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 移动敌人的精灵取决于它产生的那一边。c单对策_C#_Monogame - Fatal编程技术网

C# 移动敌人的精灵取决于它产生的那一边。c单对策

C# 移动敌人的精灵取决于它产生的那一边。c单对策,c#,monogame,C#,Monogame,所以我有一个代码,精灵在窗口外产卵,我希望它在一条特定的直线上移动,这取决于它产卵的位置,比如说,如果精灵在顶部产卵,它会在底部产卵,反之亦然,如果精灵产卵到左边,它会向右移动,反之亦然 Game1.cs代码: public class Game1 : Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Player player; List<Enemy> enemies =

所以我有一个代码,精灵在窗口外产卵,我希望它在一条特定的直线上移动,这取决于它产卵的位置,比如说,如果精灵在顶部产卵,它会在底部产卵,反之亦然,如果精灵产卵到左边,它会向右移动,反之亦然

Game1.cs代码:

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Player player;
    List<Enemy> enemies = new List<Enemy>();


    public Vector2 ecords
    {
        get { return ecords; }
        set { ecords = value; }
    }



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


    protected override void Initialize()
    {


        base.Initialize();
    }


    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        //player
        Texture2D playertexture = Content.Load<Texture2D>("Player");
        player = new Player(new Vector2(350, 175), playertexture);

        //enemy
        Texture2D enemytexture = Content.Load<Texture2D>("Enemy");
        Random random = new Random();
        var width = GraphicsDevice.Viewport.Width;
        var height = GraphicsDevice.Viewport.Height;

        for (int i = 0; i < 20; i++)
        {
            enemies.Add(new Enemy(new Vector2(random.Next(width, width + 100), random.Next(0, height)), enemytexture));
            enemies.Add(new Enemy(new Vector2(random.Next(0 - 100, 0), random.Next(0, height)), enemytexture));
            enemies.Add(new Enemy(new Vector2(random.Next(0, width), random.Next(-100, 0)), enemytexture));
            enemies.Add(new Enemy(new Vector2(random.Next(0, width), random.Next(height, height + 100)), enemytexture));
        }





    }

    protected override void UnloadContent()
    {

    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();
        foreach (Enemy enemy in enemies)
        {
            enemy.Update(gameTime);
        }
        player.Update(gameTime);



        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        foreach (Enemy enemy in enemies)
        {
            enemy.Draw(spriteBatch);
        }
        player.Draw(spriteBatch);

        spriteBatch.End();

        base.Draw(gameTime);
    }
}

一个选项是添加一个速度,或者您可以将其命名为“运动/方向”,以参数形式传递给构造函数,就像您现有的enemyposition一样。然后通过在敌人的更新方法中添加速度来更新当前位置:

可以选择使用单位矢量乘以速度作为速度值:

var (left, right) = (-Vector2.UnitX, Vector2.UnitX);
var (up, down) = (-Vector2.UnitY, Vector2.UnitY);
float enemySpeed = 5.0f;

enemies.Add(new Enemy(
    new Vector2(random.Next(width, width + 100), random.Next(0, height)), 
    enemytexture, velocity: left * enemySpeed));
// etc.

我不知道你说的向量2左,右=-Vector2.UnitX,Vector2,UnitX是什么意思@这是一个C7元组解构,与两个独立的变量声明相同:var left=-Vector2.UnitX;和var right=Vector2.UnitX;。这对我的理解来说太难了,但谢谢你的发帖和帮助基本上,在默认情况下,水平移动向右移动,因此要向左移动,需要将速度设置为负值。您可以使用例如var left、right一次定义多个相同的变量,但也可以使用相同的方法单独设置。
public void Update(GameTime gameTime)
{
    this.enemyposition += this.velocity;
}
var (left, right) = (-Vector2.UnitX, Vector2.UnitX);
var (up, down) = (-Vector2.UnitY, Vector2.UnitY);
float enemySpeed = 5.0f;

enemies.Add(new Enemy(
    new Vector2(random.Next(width, width + 100), random.Next(0, height)), 
    enemytexture, velocity: left * enemySpeed));
// etc.