添加多个视频
本文关键字:视频 添加 | 更新日期: 2023-09-27 18:03:37
我在游戏中添加多个视频时遇到了巨大的困难。在显示菜单之前,第一个视频完美地播放(introVid)。一旦我按下开始,而不是显示第二个视频(在level0加载之前),我得到一个错误,说vidTexture2是NULL。
有没有人知道如何添加多个视频?下面是视频管理器类的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TileEngine;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;
namespace TimerGame.VideoManagers
{
public class Video1Manager
{
#region Declarations
public Video introVid, cineVid;
public VideoPlayer vidPlayer, vidPlayer2;
public Texture2D vidTexture, vidTexture2;
public Rectangle vidRectangle, vidRectangle2;
#endregion
#region Initialization
public void Initialize(ContentManager Content)
{
try
{
vidPlayer = new VideoPlayer();
vidPlayer2 = new VideoPlayer();
introVid = Content.Load<Video>(@"Videos'TimerIntro");
vidRectangle = new Rectangle(-225, 0,
1250, 600);
cineVid = Content.Load<Video>(@"Videos'Cinematic1");
vidRectangle2 = new Rectangle(0, 0, 800, 600);
}
catch { Debug.Write("Video Manager Failed"); }
}
public void PlayIntro()
{
try { vidPlayer.Play(introVid); }
catch { Debug.Write("IntroVid Failed"); }
}
public void PlayCine1()
{
try { vidPlayer2.Play(cineVid); }
catch { Debug.Write("Cinematic1 Failed"); }
}
#endregion
}
}
和标志性图示:
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;
using TileEngine;
using TimerGame.MenuButtons;
using TimerGame.LevelManagers;
using TimerGame.VideoManagers;
namespace TimerGame
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Player player;
SpriteFont pericles10;
//MENU
...
Video1Manager videoManager;
//Levels
int currentLevel = -1;
int state = 0;
...
protected override void Initialize()
{
videoManager = new Video1Manager();
...
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
...
//Video
videoManager.Initialize(Content);
if (state == 0)
videoManager.PlayIntro();
...
}
protected override void Update(GameTime gameTime)
{
...
switch (state)
{
case 0:
if (keyState.IsKeyDown(Keys.Escape))
{
videoManager.vidPlayer.Stop();
state = 1;
}
if (videoManager.vidPlayer.State == MediaState.Stopped)
{
state = 1;
MediaPlayer.Play(menuMusic);
}
break;
case 1:
...
#region Playing
case GameState.Playing:
IsMouseVisible = false;
...
switch (currentLevel)
{
case 0:
videoManager.PlayCine1();
if (videoManager.vidPlayer2.State == MediaState.Stopped)
{
currentLevel++;
Level0Manager.LoadLevel();
}
break;
case 1:
player.Update(gameTime);
Level0Manager.Update(gameTime);
if (Level0Manager.LevelComplete)
{
currentLevel++;
}
break;
...some code
#endregion
}
break;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
videoManager.vidTexture = videoManager.vidPlayer.GetTexture(); //Video TEXTURE
spriteBatch.Begin(
SpriteSortMode.BackToFront,
BlendState.AlphaBlend);
switch (state)
{
case 0:
spriteBatch.Draw(videoManager.vidTexture, videoManager.vidRectangle, Color.White);
break;
case 1:
switch (gameState)
{
#region Menu Draw
...
break;
#endregion
#region Playing Draw
case GameState.Playing:
MediaPlayer.Stop();
//spriteBatch.DrawString(pericles10, "12:20", new Vector2(100, 20), Color.Red);
TileMap.Draw(spriteBatch);
switch (currentLevel)
{
case 0:
videoManager.vidTexture2 = videoManager.vidPlayer2.GetTexture();
spriteBatch.Draw(videoManager.vidTexture2, videoManager.vidRectangle2, Color.White);
break;
case 1:
player.Draw(spriteBatch);
Level0Manager.Draw(spriteBatch);
break;
....other code
break;
#endregion
}
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
你必须检查纹理是否为null,我建议你检查下面链接的msdn。
msdn:
中的代码片段// Draw the video, if we have a texture to draw.
if (videoTexture != null)
{
spriteBatch.Begin();
spriteBatch.Draw(videoTexture, screen, Color.White);
spriteBatch.End();
}
来源:http://msdn.microsoft.com/en-us/library/dd904198.aspx