C# 设置阵列动画
目前正试图在我用XNA4.0制作的C#游戏中为一系列敌人精灵制作动画 使用此动画代码C# 设置阵列动画,c#,xna,xna-4.0,C#,Xna,Xna 4.0,目前正试图在我用XNA4.0制作的C#游戏中为一系列敌人精灵制作动画 使用此动画代码 namespace Rotationgame { class Animation { Texture2D texture; Rectangle rectangle; Vector2 position; Vector2 origin; Vector2 velocity; int currentFrame; int frameHeight; int
namespace Rotationgame
{
class Animation
{
Texture2D texture;
Rectangle rectangle;
Vector2 position;
Vector2 origin;
Vector2 velocity;
int currentFrame;
int frameHeight;
int frameWidth;
float timer;
float interval = 150;
public Animation(Texture2D newTexture, Vector2 newPosition, int newFrameHeight, int newFrameWidth)
{
texture = newTexture;
position = newPosition;
frameHeight = newFrameHeight;
frameWidth = newFrameWidth;
}
public void Update(GameTime gameTime)
{
rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
origin = new Vector2(rectangle.Width / 2, rectangle.Height / 2);
position = position + velocity;
if (Keyboard.GetState().IsKeyUp(Keys.F1))
{
AnimateRight(gameTime);
velocity.X = 0;
}
else if (Keyboard.GetState().IsKeyUp(Keys.F2))
{
AnimateLeft(gameTime);
velocity.X = -0;
}
else velocity = Vector2.Zero;
}
public void AnimateRight(GameTime gameTime)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
if (timer > interval)
{
currentFrame++;
timer = 0;
if (currentFrame > 1)
currentFrame = 0;
}
}
public void AnimateLeft(GameTime gameTime)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
if (timer > interval)
{
currentFrame++;
timer = 0;
if (currentFrame > 1)
currentFrame = 0;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, rectangle, Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);
}
}
}
这是数组的代码
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Animation[] invaders;
protected override void Initialize()
{
invaders = new Animation[13];
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// Array of Space Invaders
invaders[0] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(400, 400), 115, 96);
invaders[1] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(427, 310), 115, 96);
invaders[2] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(427, 490), 115, 96);
invaders[3] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(490, 250), 115, 96);
invaders[4] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(490, 550), 115, 96);
invaders[5] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(580, 240), 115, 96);
invaders[7] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(580, 560), 115, 96);
invaders[8] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(670, 550), 115, 96);
invaders[9] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(670, 250), 115, 96);
invaders[10] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(730, 490), 115, 96);
invaders[11] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(730, 310), 115, 96);
invaders[12] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(757, 400), 115, 96);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
// Drawing Invaders
foreach (Animation invader in invaders)
invaders.Draw(spriteBatch);
}
spriteBatch.End();
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
入侵者;
受保护的覆盖无效初始化()
{
入侵者=新动画[13];
//TODO:在此处添加初始化逻辑
base.Initialize();
}
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
//太空入侵者阵列
入侵者[0]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(400400)、115、96;
入侵者[1]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(427310)、115、96;
入侵者[2]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(427490)、115、96;
入侵者[3]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(490250)、115、96);
入侵者[4]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(490550)、115、96);
入侵者[5]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(580240)、115、96);
入侵者[7]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(580560)、115、96;
入侵者[8]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(670550)、115、96;
入侵者[9]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(670250)、115、96);
入侵者[10]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(730490)、115、96;
入侵者[11]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(730310)、115、96;
入侵者[12]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(757400)、115、96;
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
//吸引入侵者
foreach(入侵者中的动画入侵者)
入侵者。抽签(斯皮特巴特);
}
spriteBatch.End();
visual Studio中的所有代码都可以正常工作,但是我在Draw方法上遇到一个错误:“错误1”“System.Array”“不包含”“Draw”“的定义,并且找不到接受”“System.Array”“类型的第一个参数的扩展方法”“Draw”“(是否缺少using指令或程序集引用?)
知道出了什么问题吗
编辑:这是game1文件的完整代码
namespace Rotationgame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Animation[] invaders;
// Different Windows
enum GameState
{
MainMenu,
Playing,
}
GameState CurrentGameState = GameState.MainMenu;
// Screeb Adjustments
int screenWidth = 1250, screenHeight = 930;
// Main Menu Buttons
button btnPlay;
button btnQuit;
// Pause Menu & buttons
bool paused = false;
button btnResume;
button btnMainMenu;
// Player's Movement
Vector2 spriteVelocity;
const float tangentialVelocity = 0f;
float friction = 1f;
Texture2D spriteTexture;
Rectangle spriteRectangle;
Vector2 spritePosition;
float rotation;
// The centre of the image
Vector2 spriteOrigin;
// Background
Texture2D backgroundTexture;
Rectangle backgroundRectangle;
// Shield
Texture2D shieldTexture;
Rectangle shieldRectangle;
// Bullets
List<Bullets> bullets = new List<Bullets>();
KeyboardState pastKey;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
invaders = new Animation[12];
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// Load Player's Shield (Cosmetic at moment as not set up fully
shieldTexture = Content.Load<Texture2D>("Shield");
shieldRectangle = new Rectangle(517, 345, 250, 220);
// Load Player's Ship
spriteTexture = Content.Load<Texture2D>("PlayerShipright");
spritePosition = new Vector2(640, 450);
// Load Game background
backgroundTexture = Content.Load<Texture2D>("Background");
backgroundRectangle = new Rectangle(0, 0, 1250, 930);
// Screen Adjustments
graphics.PreferredBackBufferWidth = screenWidth;
graphics.PreferredBackBufferHeight = screenHeight;
graphics.ApplyChanges();
IsMouseVisible = true;
// Main menu Buttons & locations
btnPlay = new button(Content.Load<Texture2D>("Playbutton"), graphics.GraphicsDevice);
btnPlay.setPosition(new Vector2(550, 310));
btnQuit = new button(Content.Load<Texture2D>("Quitbutton"), graphics.GraphicsDevice);
btnQuit.setPosition(new Vector2(550, 580));
// Array of Space Invaders
invaders[0] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(400, 400), 115, 96);
invaders[1] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(427, 310), 115, 96);
invaders[2] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(427, 490), 115, 96);
invaders[3] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(490, 250), 115, 96);
invaders[4] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(490, 550), 115, 96);
invaders[5] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(580, 240), 115, 96);
invaders[6] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(580, 560), 115, 96);
invaders[7] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(670, 550), 115, 96);
invaders[8] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(670, 250), 115, 96);
invaders[9] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(730, 490), 115, 96);
invaders[10] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(730, 310), 115, 96);
invaders[11] = new Animation(Content.Load<Texture2D>("SpaceInvaderbefore"), new Vector2(757, 400), 115, 96);
// Pause menu buttons & locations
btnResume = new button(Content.Load<Texture2D>("Playbutton"), graphics.GraphicsDevice);
btnResume.setPosition(new Vector2(550, 310));
btnMainMenu = new button(Content.Load<Texture2D>("Quitbutton"), graphics.GraphicsDevice);
btnMainMenu.setPosition(new Vector2(550, 580));
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
MouseState mouse = Mouse.GetState();
// Allows the game to exit
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
switch (CurrentGameState)
{
case GameState.MainMenu:
if(btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
btnPlay.Update(mouse);
if (btnQuit.isClicked == true)
this.Exit();
btnQuit.Update(mouse);
break;
case GameState.Playing:
if (!paused)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
{
paused = true;
btnResume.isClicked = false;
}
}
else if (paused)
{
if (Keyboard.GetState().IsKeyDown(Keys.Enter))
if (btnResume.isClicked)
paused = false;
if (btnMainMenu.isClicked) CurrentGameState = GameState.MainMenu;
}
break;
}
// TODO: Add your update logic here
if (Keyboard.GetState().IsKeyDown(Keys.Space) && pastKey.IsKeyUp(Keys.Space))
Shoot();
pastKey = Keyboard.GetState();
spritePosition = spriteVelocity + spritePosition;
spriteRectangle = new Rectangle((int)spritePosition.X, (int)spritePosition.Y,
spriteTexture.Width, spriteTexture.Height);
spriteOrigin = new Vector2(spriteRectangle.Width / 2, spriteRectangle.Height / 2);
if (Keyboard.GetState().IsKeyDown(Keys.Right)) rotation += 0.025f;
if (Keyboard.GetState().IsKeyDown(Keys.Left)) rotation -= 0.025f;
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
spriteVelocity.X = (float)Math.Cos(rotation) * tangentialVelocity;
spriteVelocity.Y = (float)Math.Sin(rotation) * tangentialVelocity;
}
else if (Vector2.Zero != spriteVelocity)
{
float i = spriteVelocity.X;
float j = spriteVelocity.Y;
spriteVelocity.X = i -= friction * i;
spriteVelocity.Y = j -= friction * j;
base.Update(gameTime);
}
UpdateBullets();
}
public void UpdateBullets()
{
foreach (Bullets bullet in bullets)
{
bullet.position += bullet.velocity;
if (Vector2.Distance(bullet.position, spritePosition) > 760)
bullet.isVisible = false;
}
for (int i = 0; i < bullets.Count; i++)
{
if(!bullets[i].isVisible)
{
bullets.RemoveAt(i);
i--;
}
}
}
public void Shoot()
{
Bullets newBullet = new Bullets(Content.Load<Texture2D>("bullet"));
newBullet.velocity = new Vector2((float)Math.Cos(rotation),(float)Math.Sin(rotation)) * 3f + spriteVelocity;
newBullet.position = spritePosition + newBullet.velocity * 5;
newBullet.isVisible = true;
if(bullets.Count() < 25)
bullets.Add(newBullet);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
switch (CurrentGameState)
{
case GameState.MainMenu:
spriteBatch.Draw(Content.Load<Texture2D>("MainMenu"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
btnPlay.Draw(spriteBatch);
btnQuit.Draw(spriteBatch);
break;
case GameState.Playing:
// Drawing Background
spriteBatch.Draw(backgroundTexture, backgroundRectangle, Color.White);
// Drawing Shield
spriteBatch.Draw(shieldTexture, shieldRectangle, Color.White);
// Drawing Invaders
foreach (Animation invader in invaders)
invader.Draw(spriteBatch);
// Drawing Bullets
foreach (Bullets bullet in bullets)
bullet.Draw(spriteBatch);
// Drawing Player's Character
spriteBatch.Draw(spriteTexture, spritePosition, null, Color.White, rotation, spriteOrigin, 1f, SpriteEffects.None, 0);
if (paused)
{
spriteBatch.Draw(Content.Load<Texture2D>("PauseMenu"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
btnResume.Draw(spriteBatch);
btnMainMenu.Draw(spriteBatch);
}
break;
}
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
名称空间旋转游戏
{
///
///这是游戏的主要类型
///
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
入侵者;
//不同的窗口
枚举配子状态
{
主菜单,
玩,
}
GameState CurrentGameState=GameState.main菜单;
//尖叫声调整
int屏幕宽度=1250,屏幕高度=930;
//主菜单按钮
按钮btnPlay;
按钮b取消;
//暂停菜单和按钮
布尔=假;
按钮btnResume;
按钮BTN主菜单;
//运动员动作
矢量2速度;
恒浮切向速度=0f;
浮子摩擦力=1f;
质感2维织物;
长方形斜六角形;
矢量2精神定位;
浮动旋转;
//图像的中心
矢量2精神起源;
//背景
纹理2D背景纹理;
矩形背景矩形;
//护盾
纹理2D屏蔽纹理;
矩形屏蔽;
//子弹
列表项目符号=新列表();
键盘状态pastKey;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
///
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
///
受保护的覆盖无效初始化()
{
入侵者=新动画[12];
//TODO:在此处添加初始化逻辑
base.Initialize();
}
///
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
///
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
//装载玩家的盾牌(暂时未完全设置)
shieldTexture=Content.Load(“屏蔽”);
shieldRectangle=新矩形(517345250220);
//装载玩家的船
spriteTexture=Content.Load(“PlayerShipright”);
spritePosition=newvector2(640450);
//加载游戏背景
backgroundTexture=Content.Load(“背景”);
背景矩形=新矩形(0,01250930);
//屏幕调整
graphics.PreferredBackBufferWidth=屏幕宽度;
graphics.PreferredBackBufferHeight=屏幕高度;
graphics.ApplyChanges();
IsMouseVisible=true;
//主菜单按钮和位置
btnPlay=新按钮(Content.Load(“Playbutton”)、graphics.GraphicsDevice);
BTN显示设置位置(新矢量2(550310));
btnQuit=新按钮(Content.Load(“Quitbutton”)、graphics.GraphicsDevice);
b取消设置位置(新矢量2(550580));
//太空入侵者阵列
入侵者[0]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(400400)、115、96;
入侵者[1]=新动画(Content.Load(“spaceinvesterbefore”)、新矢量2(427310)、115、96;
入侵者[2]=新动画(内容
namespace Rotationgame
{
class button
{
Texture2D texture;
Vector2 position;
Rectangle rectangle;
Color colour = new Color(255, 255, 255, 255);
public Vector2 size;
public button(Texture2D newTexture, GraphicsDevice graphics)
{
texture = newTexture;
// ScreenW = 1250 (currently atm 800), ScreenH = 930 (currently atm 600)
//ImgW = 100 , ImgH = 20
size = new Vector2(graphics.Viewport.Width / 8, graphics.Viewport.Height / 30);
}
bool down;
public bool isClicked;
public void Update(MouseState mouse)
{
rectangle = new Rectangle((int)position.X,(int)position.Y,
(int)size.X, (int)size.Y);
Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
if (mouseRectangle.Intersects(rectangle))
{
if (colour.A == 255) down = false;
if (colour.A == 0) down = true;
if (down) colour.A += 3; else colour.A -= 3;
if (mouse.LeftButton == ButtonState.Pressed) isClicked = true;
}
else if (colour.A < 255)
{
colour.A += 3;
isClicked = false;
}
}
public void setPosition(Vector2 newPosition)
{
position = newPosition;
}
public void Draw(SpriteBatch spritebatch)
{
spritebatch.Draw(texture, rectangle, colour);
}
}
}
Content.Load<Texture2D>("SpaceInvaderbefore")