0

我基本上希望一个角色朝一个方向走一会儿,停下来,然后朝另一个随机方向走。现在我的精灵看起来但不动,随机快速地向各个方向移动,然后等待并再次癫痫发作。如果有用,我将发布到目前为止的代码。

class NPC: Mover
{
    int movementTimer = 0;

    public override Vector2 direction
    {
        get
        {
            Random rand = new Random();
            int randDirection = rand.Next(8);

            Vector2 inputDirection = Vector2.Zero;


            if (movementTimer >= 50)
            {
                if (randDirection == 4)
                {
                    inputDirection.X -= 1;
                    movingLeft = true;
                }
                else movingLeft = false;

                if (randDirection == 1)
                {
                    inputDirection.X += 1;
                    movingRight = true;
                }
                else movingRight = false;

                if (randDirection == 2)
                {
                    inputDirection.Y -= 1;
                    movingUp = true;
                }
                else movingUp = false;

                if (randDirection == 3)
                {
                    inputDirection.Y += 25;
                    movingDown = true;
                }
                else movingDown = false;

                if (movementTimer >= 100)
                {
                    movementTimer = 0;
                }
            }

            return inputDirection * speed;
        }
    }

    public NPC(Texture2D textureImage, Vector2 position,
        Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
        Vector2 speed)
        : base(textureImage, position, frameSize, collisionOffset, currentFrame,
        sheetSize, speed)
    {
    }

    public NPC(Texture2D textureImage, Vector2 position,
        Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
        Vector2 speed, int millisecondsPerframe)
        : base(textureImage, position, frameSize, collisionOffset, currentFrame,
        sheetSize, speed, millisecondsPerframe)
    {
    }

    public override void Update(GameTime gameTime, Rectangle clientBounds)
    {

        movementTimer++;
        position += direction;

        if (position.X < 0)
            position.X = 0;
        if (position.Y < 0)
            position.Y = 0;
        if (position.X > clientBounds.Width - frameSize.X)
            position.X = clientBounds.Width - frameSize.X;
        if (position.Y > clientBounds.Height - frameSize.Y)
            position.Y = clientBounds.Height - frameSize.Y;


        base.Update(gameTime, clientBounds);
    }
}
4

2 回答 2

2

您如何创建一种方法来获得随机方向

Vector2 GetRandomDirection()
{
    Random random = new Random();
    int randomDirection = random.Next(8);

    switch (randomDirection)
    {
        case 1:
            return new Vector2(-1, 0);
        case 2:
            return new Vector2(1, 0);
        case 3:
            return new Vector2(0, -1);
        case 4:
            return new Vector2(0, 1);
        //plus perhaps additional directions?
        default:
            return Vector2.Zero;
    }
}

然后,当设定的时间已经过去时,您调用该方法来改变方向:

double totalElapsedSeconds = 0;
const double MovementChangeTimeSeconds = 2.0; //seconds

public override void Update(GameTime gameTime, Rectangle clientBounds)
{
    totalElapsedSeconds += gameTime.ElapsedGameTime.TotalSeconds;

    if (totalElapsedSeconds >= MovementChangeTimeSeconds)
    {
        totalElapsedSeconds -= MovementChangeTimeSeconds;
        this.direction = GetRandomDirection();
    }

    position += direction;

    //...
}

使用不同的代码来检测 NPC 移动的方向(布尔值movingLeftmovingRight等)。根据direction向量检测这些值。这样您就不必分配多余的值

enum MoveDirection
{
    Up, Down, Left, Right, UpLeft, UpRight, DownLeft, DownRight, None
}

public MoveDirection GetMoveDirection(Vector2 direction)
{
    if (direction.Y < 0)
    {
        if (direction.X < 0)
            return MoveDirection.UpLeft;
        else if (direction.X > 0)
            return MoveDirection.UpRight;
        else
            return MoveDirection.Up;
    }
    else if (direction.Y > 0)
    {
        if (direction.X < 0)
            return MoveDirection.DownLeft;
        else if (direction.X > 0)
            return MoveDirection.DownRight;
        else
            return MoveDirection.Down;
    }
    else
    {
        if (direction.X < 0)
            return MoveDirection.Left;
        else if (direction.X > 0)
            return MoveDirection.Right;
        else
            return MoveDirection.None;
    }
}

我认为这用于旋转精灵(或者可能绘制不同的精灵),所以现在你只需要一个开关:

public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
    MoveDirection moveDirection = GetMoveDirection(this.direction);
    switch(moveDirection)
    {
        case MoveDirection.Up:
            //Draw up-facing sprite, or assign a value to a rotation variable.
            break;
        case MoveDirection.UpLeft:
            //...
    }
}
于 2012-11-11T22:45:34.377 回答
0

您可以检查 Platformer Starter Kit 以了解敌人的移动方式。

由于 Microsoft 停止了针对 Windows 8 的 XNA 开发,他删除了所有下载 XNA 4 入门工具包的链接。

我将原始 Platformer 源代码上传到 bitbucket。

在这里您可以下载 XNA 4 的原始 Platformer Starter Kit Source Code http://vackup.blogspot.com.ar/2012/11/download-original-platformer-starter.html

于 2012-11-12T04:39:20.597 回答