Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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# xna方法不接受null 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用Microsoft.Xna.Framework; 使用Microsoft.Xna.Framework.Audio; 使用Microsoft.Xna.Framework.Content; 使用Microsoft.Xna.Framework.GamerServices; 使用Microsoft.Xna.Framework.Graphics; 使用Microsoft.Xna.Framework.Input; 使用Microsoft.Xna.Framework.Media; 命名空间WindowsGame3 { 公共类游戏1:Microsoft.Xna.Framework.Game { 图形管理器图形; SpriteBatch SpriteBatch; 纹理2D飞船,子弹,外星人; Texture2D[]外星人=新的Texture2D[20]; Texture2D[]项目符号=新的Texture2D[10]; Vector2 shipPos=新Vector2(200540); 向量2 alienPos=新向量2(200400); Vector2 bulletPos=新Vector2(200450); Vector2[]aliensPos=新Vector2[20]; Vector2[]bulletsPos=新Vector2[10]; int alienCount=0; int bullettcount=0; KeyboardState=Keyboard.GetState(); 公共游戏1() { graphics=新的GraphicsDeviceManager(此); Content.RootDirectory=“Content”; graphics.PreferredBackBufferWidth=400; graphics.PreferredBackBufferHeight=600; } 受保护的覆盖无效初始化() { base.Initialize(); } 受保护的覆盖void LoadContent() { spriteBatch=新spriteBatch(图形设备); 外星人=Content.Load(“外星人”); 装运=装载量(“装运”); 对于(int x=0;x_C#_Xna - Fatal编程技术网

C# xna方法不接受null 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用Microsoft.Xna.Framework; 使用Microsoft.Xna.Framework.Audio; 使用Microsoft.Xna.Framework.Content; 使用Microsoft.Xna.Framework.GamerServices; 使用Microsoft.Xna.Framework.Graphics; 使用Microsoft.Xna.Framework.Input; 使用Microsoft.Xna.Framework.Media; 命名空间WindowsGame3 { 公共类游戏1:Microsoft.Xna.Framework.Game { 图形管理器图形; SpriteBatch SpriteBatch; 纹理2D飞船,子弹,外星人; Texture2D[]外星人=新的Texture2D[20]; Texture2D[]项目符号=新的Texture2D[10]; Vector2 shipPos=新Vector2(200540); 向量2 alienPos=新向量2(200400); Vector2 bulletPos=新Vector2(200450); Vector2[]aliensPos=新Vector2[20]; Vector2[]bulletsPos=新Vector2[10]; int alienCount=0; int bullettcount=0; KeyboardState=Keyboard.GetState(); 公共游戏1() { graphics=新的GraphicsDeviceManager(此); Content.RootDirectory=“Content”; graphics.PreferredBackBufferWidth=400; graphics.PreferredBackBufferHeight=600; } 受保护的覆盖无效初始化() { base.Initialize(); } 受保护的覆盖void LoadContent() { spriteBatch=新spriteBatch(图形设备); 外星人=Content.Load(“外星人”); 装运=装载量(“装运”); 对于(int x=0;x

C# xna方法不接受null 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用Microsoft.Xna.Framework; 使用Microsoft.Xna.Framework.Audio; 使用Microsoft.Xna.Framework.Content; 使用Microsoft.Xna.Framework.GamerServices; 使用Microsoft.Xna.Framework.Graphics; 使用Microsoft.Xna.Framework.Input; 使用Microsoft.Xna.Framework.Media; 命名空间WindowsGame3 { 公共类游戏1:Microsoft.Xna.Framework.Game { 图形管理器图形; SpriteBatch SpriteBatch; 纹理2D飞船,子弹,外星人; Texture2D[]外星人=新的Texture2D[20]; Texture2D[]项目符号=新的Texture2D[10]; Vector2 shipPos=新Vector2(200540); 向量2 alienPos=新向量2(200400); Vector2 bulletPos=新Vector2(200450); Vector2[]aliensPos=新Vector2[20]; Vector2[]bulletsPos=新Vector2[10]; int alienCount=0; int bullettcount=0; KeyboardState=Keyboard.GetState(); 公共游戏1() { graphics=新的GraphicsDeviceManager(此); Content.RootDirectory=“Content”; graphics.PreferredBackBufferWidth=400; graphics.PreferredBackBufferHeight=600; } 受保护的覆盖无效初始化() { base.Initialize(); } 受保护的覆盖void LoadContent() { spriteBatch=新spriteBatch(图形设备); 外星人=Content.Load(“外星人”); 装运=装载量(“装运”); 对于(int x=0;x,c#,xna,C#,Xna,我想在ships位置创建一个项目符号,并将该项目符号存储在一个数组中,但是在我的draw方法中,当我从数组中绘制项目符号时,会收到错误消息,该方法不接受此参数的null。 参数名称:纹理这是因为您试图在制作子弹之前绘制子弹。解决办法就是在你制造子弹之前不要画子弹 有很多种方法可以做到这一点,但下面是最快和最脏的方法: 改变 using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.

我想在ships位置创建一个项目符号,并将该项目符号存储在一个数组中,但是在我的draw方法中,当我从数组中绘制项目符号时,会收到错误消息,该方法不接受此参数的null。
参数名称:纹理

这是因为您试图在制作子弹之前绘制子弹。解决办法就是在你制造子弹之前不要画子弹

有很多种方法可以做到这一点,但下面是最快和最脏的方法:

改变

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame3
{
    public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Texture2D ship, bullet, alien;

    Texture2D[] aliens = new Texture2D[20];
    Texture2D[] bullets = new Texture2D[10];

    Vector2 shipPos = new Vector2(200, 540);
    Vector2 alienPos = new Vector2(200, 400);
    Vector2 bulletPos = new Vector2(200, 450);

    Vector2[] aliensPos = new Vector2[20];
    Vector2[] bulletsPos = new Vector2[10];

    int alienCount = 0;
    int bulletCount = 0;

    KeyboardState state = Keyboard.GetState();

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

        graphics.PreferredBackBufferWidth = 400;
        graphics.PreferredBackBufferHeight = 600;
    }

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        alien = Content.Load<Texture2D>("alien");
        ship = Content.Load<Texture2D>("ship");

        for (int x = 0; x < 5; x++)
        {
            for (int y = 0; y < 4; y++)
            {
                aliens[alienCount] = Content.Load<Texture2D>("alien");
                aliensPos[alienCount].X = 100 + (x * 40);
                aliensPos[alienCount].Y = 20 + (y * 40);
                alienCount++;
            }  
        }
    }

    protected override void UnloadContent()
    {

    }

    protected override void Update(GameTime gameTime)
    {
        if (state.IsKeyDown(Keys.Right) && shipPos.X < 360)
        {
            shipPos.X += 5;
        }

        if (state.IsKeyDown(Keys.Left) && shipPos.X > 0)
        {
            shipPos.X -= 5;
        }

        if (state.IsKeyDown(Keys.Space))
        {
                bullets[bulletCount] = Content.Load<Texture2D>("bullet");
                bulletsPos[bulletCount].X = shipPos.X;
                bulletsPos[bulletCount].Y = shipPos.Y;
                bulletCount++;
        }

        base.Update(gameTime);
    }

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

        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

        spriteBatch.Draw(ship, shipPos, Color.White);
        spriteBatch.Draw(alien, alienPos, Color.White);

        for (int alienCount = 0; alienCount < 20; alienCount++)
        {
            spriteBatch.Draw(aliens[alienCount], aliensPos[alienCount], Color.White);
        }

        for (int bulletCount = 0; bulletCount < 10; bulletCount++)
        {
            spriteBatch.Draw(bullets[bulletCount], bulletsPos[bulletCount], Color.White);
        }

        spriteBatch.End();
        base.Draw(gameTime);
    }
}
}
for(int-bulletCount=0;bulletCount<10;bulletCount++)
{
spriteBatch.Draw(子弹[bulletCount],子弹[bulletCount],颜色.白色);
}

for(int-bulletCount=0;bulletCount<10;bulletCount++)
{
如果(项目符号[bulletCount]!=null)
spriteBatch.Draw(子弹[bulletCount],子弹[bulletCount],颜色.白色);
}

在XNA中,最好在需要之前加载所有资源,这就是LoadContent()方法的作用。您应该在其中加载bullet纹理,然后在必要时在Update()中使用它

每个图像只需要一个Texture2D对象,即使要多次绘制。 您不需要使用纹理2D阵列,其中包含完全相同的图像

以你为例:

    for (int bulletCount = 0; bulletCount < 10; bulletCount++)
    {
        if(bullets[bulletCount] != null)
            spriteBatch.Draw(bullets[bulletCount], bulletsPos[bulletCount], Color.White);
    }
然后,当加载并准备好所有纹理后,可以绘制它:

if (state.IsKeyDown(Keys.Space))
{
        bulletsPos[bulletCount].X = shipPos.X;
        bulletsPos[bulletCount].Y = shipPos.Y;
        bulletCount++;
}

您正在传入一个空参数。这是绝对不允许的。看。。。有。。。代码中有很多错误,我不知道从哪里开始。我只希望这是一段经过编辑的代码片段。我可以看出您对XNA和编程相当陌生。虽然能够制作自己的视频游戏的诱惑是相当令人兴奋的,但我郑重地建议你在处理类似问题之前,先做一些关于C#的基本教程,学习一些编程原则,否则你将在几周内撞到墙。我在自学,当然这不会是完美的。但请随时给我一些建议,告诉我哪些方面可以做得更好
protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);

    alien = Content.Load<Texture2D>("alien"); //only one texture needed
    ship = Content.Load<Texture2D>("ship");
    bullet = Content.Load<Texture2D>("bullet"); //only one texture needed

    //set initial positions as before
    for (int x = 0; x < 5; x++)
    {
        for (int y = 0; y < 4; y++)
        {
            aliensPos[alienCount].X = 100 + (x * 40);
            aliensPos[alienCount].Y = 20 + (y * 40);
            alienCount++;
        }  
    }
}
if (state.IsKeyDown(Keys.Space))
{
        bulletsPos[bulletCount].X = shipPos.X;
        bulletsPos[bulletCount].Y = shipPos.Y;
        bulletCount++;
}
protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.Black);

    spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

    spriteBatch.Draw(ship, shipPos, Color.White);

    for (int alienCount = 0; alienCount < 20; alienCount++)
    {
        //draw alien texture for every aliensPos
        spriteBatch.Draw(alien, aliensPos[alienCount], Color.White);
    }

    for (int bulletCount = 0; bulletCount < 10; bulletCount++)
    {
        //draw bullet texture for every bulletsPos
        spriteBatch.Draw(bullet, bulletsPos[bulletCount], Color.White);
    }

    spriteBatch.End();
    base.Draw(gameTime);
}
state = Keyboard.GetState();