C# XNA - 创建太空入侵者数组

问题描述 投票:0回答:2

我看到了多个使用矩形作为数组的教程。但他们的入侵者却没有生气。我正在为我的入侵者使用精灵表,并且我需要将它们全部动画化...如何做到这一点?

这是我的入侵者类别:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
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;
using Microsoft.Xna.Framework.Net;

namespace SpaceInvaders
{
    class botInvaders
    {
        public botInvaders()
        {
        }

        public static Texture2D BotInvaderTex;
        Rectangle BotInvaderHitBox;
        public static Vector2 BotInvaderPos = new Vector2(0, 28), BotInvaderOrigin;

        int BotInvaderCurrentFrame = 1, BotInvaderFrameWidth = 52, BotInvaderFrameHeight = 88;

        float Timer = 0f, Interval = 100f;

        public void Initialize()
        {
        }

        public void LoadContent(ContentManager Content)
        {
            BotInvaderTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\botInvaders\\normalInvaders\\invaderShip1");
        }

        public void Update(GameTime gameTime)
        {
            BotInvaderHitBox = new Rectangle(BotInvaderCurrentFrame * BotInvaderFrameWidth, 0, BotInvaderFrameWidth, BotInvaderFrameHeight);
            BotInvaderOrigin = new Vector2(BotInvaderHitBox.X / 2, BotInvaderHitBox.Y / 2);

            Timer += (float)gameTime.ElapsedGameTime.Milliseconds;

            if (Timer > Interval)
            {
                BotInvaderCurrentFrame++;
                Timer = 0f;
            }

            if (BotInvaderCurrentFrame == 2)
            {
                BotInvaderCurrentFrame = 0;
            }

            BotInvaderHitBox = new Rectangle(BotInvaderCurrentFrame * BotInvaderFrameWidth, 0, BotInvaderFrameWidth, BotInvaderFrameHeight);
            BotInvaderOrigin = new Vector2(BotInvaderHitBox.Width / 2, BotInvaderHitBox.Height / 2);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(BotInvaderTex, BotInvaderPos, BotInvaderHitBox, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);      
        }
    }
}

这展示了我如何为入侵者制作动画。我可以修改部分代码,以便有 5 行入侵者和 10 列吗?我自己可以做动作,只需要阵法。预先感谢!

c# arrays xna
2个回答
2
投票

实现你想要的最好方法是将所有入侵者的东西封装在它自己的类中。假设你称之为入侵者。它可能看起来像这样:

public class Invader
{
    private Texture2D _texture;

    public Invader(Texture2D texture)
    {
        _texture = texture;
    }

    public void Update(GameTime gameTime)
    {
        // Update logic for this individual invader goes here
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        // Draw routine for this invidual invader goes here
    }
}

然后,你就会:

Invader[,] array = new Invader[columns,rows];

您将在游戏开始时初始化。在

Update()
方法中,您将循环遍历数组并调用入侵者的
Update()
方法:

for (int y = 0;y < rows;++y)
{
    for (int x = 0;x < columns;++x)
    {
        array[x,y].Update(gameTime);
    }
}

同样,在 Draw() 方法中,您将执行相同的循环并调用:

array[x,y].Draw(spriteBatch);

1
投票

封装它!您可以使用入侵者类来制作一系列太空入侵者。

Invader[] InvaderArray = new Invader[x,y];

然后您必须致电:

InvaderArray[x,y].Update(gameTime);

然后在draw方法中你需要绘制它。

InvaderArray[x,y].Draw(spriteBatch)
© www.soinside.com 2019 - 2024. All rights reserved.