简体   繁体   English

在类之间加载X位置会给出“字段从未分配给”警告

[英]Loading an X position between classes gives a “field is never assigned to” warning

So I created a small game in XNA and I am at the stage of coding AI. 因此,我在XNA上创建了一个小游戏,目前正处于AI编码阶段。 The problem I have currently is that I can't load the X position of the redNinja (player's character) and have the blueNinja (AI) read it and walk towards it. 我目前遇到的问题是,我无法加载redNinja(玩家角色)的X位置,并且无法让blueNinja(AI)读取并走向它。

Every time I try to reference it via redNinja.Position.X, it gives me the field is never assigned to, and will always have it's default value null. 每次我尝试通过redNinja.Position.X进行引用时,它都会为我提供field is never assigned to, and will always have it's default value null.field is never assigned to, and will always have it's default value null. warning. 警告。

namespace Ninja_DM
{
class AI
{
    Texture2D blueNinja;
    Ninjas redNinja;
    float timer = 0f;
    float interval = 130f;
    int currentFrame = 0;
    int spriteSpeed = 2;
    int spriteWidth = 32;
    int spriteHeight = 28;
    public Rectangle sourceRect;
    Vector2 position_b;
    Vector2 origin;

    public Vector2 Position
    {
        get { return position_b; }
        set { position_b = value; }
    }

    public Vector2 Origin
    {
        get { return origin; }
        set { origin = value; }
    }

    public Texture2D Texture
    {
        get { return blueNinja; }
        set { blueNinja = value; }
    }

    public Rectangle SourceRect
    {
        get { return sourceRect; }
        set { sourceRect = value; }
    }

    public AI(Texture2D texture, int currentFrame, int spriteWidth, int spriteHeight)
    {
        this.blueNinja = texture;
        this.currentFrame = currentFrame;
        this.spriteWidth = spriteWidth;
        this.spriteHeight = spriteHeight;
    }

    public void AIMovement(GameTime gameTime)
    {
        sourceRect = new Rectangle(30 * currentFrame, 0, 30, 37);

        if (position_b.X > redNinja.Position.X)
        {
            AnimateLeftAI(gameTime);
            if (position_b.X < 20)
            {
                position_b.X += spriteSpeed;
            }
        }

        if (position_b.X < redNinja.Position.X)
        {
            AnimateRightAI(gameTime);
            if (position_b.X < 1100)
            {
                position_b.X += spriteSpeed;
            }
        }

    }

    public void AnimateLeftAI(GameTime gameTime)
    {

        timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

        if (timer > interval)
        {
            currentFrame++;

            if (currentFrame > 4)
            {
                currentFrame = 3;
            }
            timer = 0f;
        }
    }

    public void AnimateRightAI(GameTime gameTime)
    {

        timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

        if (timer > interval)
        {
            currentFrame++;

            if (currentFrame > 4)
            {
                currentFrame = 3;
            }
            timer = 0f;
        }
    }
}
}

you would need to bring the player's ninja as an argument when calling AIMovement(), something like this: 您需要在调用AIMovement()时将玩家的忍者作为参数,如下所示:

public void AIMovement(GameTime gameTime, Ninjas playerNinja)
{
    sourceRect = new Rectangle(30 * currentFrame, 0, 30, 37);

    if (position_b.X > playerNinja.Position.X)
    {
        AnimateLeftAI(gameTime);
        if (position_b.X < 20)
        {
            position_b.X += spriteSpeed;
        }
    }

    if (position_b.X < playerNinja.Position.X)
    {
        AnimateRightAI(gameTime);
        if (position_b.X < 1100)
        {
            position_b.X += spriteSpeed;
        }
    }

}

Notice you really don't need to declare a field variable called redNinja anymore because you bring the Ninjas in that you are testing against as arguments/parameters of the method. 注意,您实际上不再需要声明一个名为redNinja的字段变量,因为您将Ninjas作为测试的对象作为方法的参数/参数。

暂无
暂无

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

相关问题 “字段从未分配给”警告 - “Field is never assigned to” warning 对象被实例化但仍然收到警告:“字段&#39;x&#39;永远不会分配给...” - Object is instantiated but still getting Warning: “Field 'x' is never assigned to …” Visual Studio警告:x永远不会分配给 - Visual studio warning: x is never assigned to 我收到警告“从未分配字段‘Enemy.AI.animator’” - I get the warning "Field 'Enemy.AI.animator' is never assigned" 警告:永远不会将字段分配给,并且始终将其默认值设置为null - Warning: Field is never assigned to, and will always have its default value null C# readonly 字段从未分配警告 - 可以将其配置为错误 - C# readonly field is never assigned warning - can it be configured as an error 为什么编译器为赋值的变量提供错误而不是警告,但是从未使用过它的值 - Why do compiler gives error instead of warning for variable which is assigned but its value is never used c#中给出警告的结构字段永远不会分配给,并且始终具有默认值0 - Struct in c# giving warning Field is never assigned to,and will always have its default value0 警告CS0649:字段 <fieldname> 永远不会分配给,并且将始终将其默认值设置为null - warning CS0649: Field <fieldname> is never assigned to, and will always have its default value null 变量未声明或从未分配警告 - The variable is either undeclared or was never assigned warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM