简体   繁体   中英

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. 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.

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. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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