简体   繁体   English

C#XNA阵列移除

[英]C# XNA Array Removal

I'm trying to make a simple brick breaker game. 我正在尝试制作一个简单的破砖游戏。 I have everything created with collision and simple physics. 我拥有碰撞和简单物理学创造的一切。 The ball bounces off the paddle and walls, but I'm having trouble with the bricks. 球从球拍和墙壁上弹起,但是我遇到了麻烦。 I can get the ball to bounce off the bricks, I'm just not experienced enough to know how to make the brick disappear. 我可以使球从砖块上弹起,我只是没有足够的经验来知道如何使砖块消失。 The bricks are in an array. 砖块成阵列排列。 Is there some way to remove a brick/specific index of an array? 有什么方法可以删除数组的砖块/特定索引吗?

This is how I create the blocks 这就是我创建积木的方式

        int blockCount=0;

        for (int x = 0; x < 8; x++)
        {
            for (int y = 0; y < 5; y++)
            {
                blocks[blockCount] = Content.Load<Texture2D>("Block");
                blocksPosition[blockCount].X = 95 + (x * 75);
                blocksPosition[blockCount].Y = 20 + (y * 50);
                blockCount++;
            }
        }

This is how I do collision and how I would implement the brick removal 这就是我发生碰撞的方式以及实现砖块清除的方式

    public void DetectBlockBallCollision()
    {
        for (int i = 0; i<40; i++)
        {
            if ((ballPosition.Y + ball.Height) >= blocksPosition[i].Y &&
                (ballPosition.Y + ball.Height) < (blocksPosition[i].Y+75) &&
                (ballPosition.X + ball.Width) > blocksPosition[i].X &&
                ballPosition.X < (blocksPosition[i].X + blocks[i].Width))
            {
                movingUp = false;
                //whatever would get rid of block;
            }

        }

    }
  1. Don't load texture in your loop, load it only once in LoadContent. 不要在循环中加载纹理,只能在LoadContent中加载一次。
  2. Put properly Active (as TyCobb suggested) in your brick element and in collsion set it to true and then remove it somewhere outside loop. 在砖元素中正确放置“活动”(如TyCobb建议),并在碰撞中将其设置为true,然后在循环外的某个位置将其删除。

    bricks.removeAll(b => !(b.active)) bricks.removeAll(b =>!(b.active))

By different properties your bricks could have health, destroyable property, brick that drop some item... 通过不同的属性,您的砖块可能具有健康,可破坏的属性,掉落某些物品的砖块...

Two-dimensional array is useful to align block by grid. 二维数组可用于逐格对齐。 Blocks will be arranged by grid cell coords with fixed pixel step in the X and Y, the position is defined as (col * step_x, row * step_y). 块将通过在X和Y中具有固定像素step的网格单元坐标进行排列,位置定义为(col * step_x,row * step_y)。 Then, for an array of int value of 0 means there is no block, and non-zero value mean block ID from the block register. 然后,对于int值为0的数组,意味着没有块,而非零值意味着来自块寄存器的块ID。 This register should keep data common to all units of this block (with the same ID). 该寄存器应保持该块所有单元(具有相同ID)的公共数据。 This method is best suited for blocks that can be destroyed with one hit. 此方法最适合一击即可摧毁的方块。 For a more durable can be used several variants of the same total block, such if brick block (1 HP) have ID = 1, stone block (2 HP) have ID = 2 for non-damaged and ID = 3 for damaged, iron block with 3/2/1 HP have ID = 4/5/6 and so on. 为了更耐用,可以使用同一总块的多个变体,例如,砖块(1 HP)的ID = 1,石头块(2 HP)的ID = 2(未损坏)和ID = 3(损坏的铁) 3/2/1 HP的方块的ID = 4/5/6,依此类推。 Then you need to remove the unit set in the array to 0. 然后,您需要删除数组中设置为0的单位。

The second way is use list. 第二种方法是使用列表。 Blocks should be presented in the form of objects, each of which knows its coordinates, max and current HP, special states and so on. 块应该以对象的形式呈现,每个对象都知道其坐标,最大和当前HP,特殊状态等。 Benefits that you can actually remove the blocks from the list when you hit it ant it's HP become 0. 好处是,当您将其击中并使其HP变为0时,您实际上可以从列表中将其删除。

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

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