所以我看到多个教程使用矩形作为数组。但他们的入侵者并没有动画。我正在为我的入侵者使用spritesheet,我需要让它们全部动画......怎么做?
这是我的入侵者类:
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行吗?我自己可以做动作,我只需要阵列。提前谢谢!
答案 0 :(得分:3)
实现所需内容的最佳方法是将所有入侵者的东西封装在自己的类中。假设你称之为Invader。它可能看起来像这样:
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 :(得分:1)
封装它!您可以使用入侵者类来创建一个空间入侵者数组。
Invader[] InvaderArray = new Invader[x,y];
然后你必须打电话:
InvaderArray[x,y].Update(gameTime);
然后在draw方法中你需要绘制它。
InvaderArray[x,y].Draw(spriteBatch)