[英]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.