Добавить более одного видео

1

У меня тяжелая сложность в добавлении нескольких видео в мою игру. Первое видео воспроизводится отлично (introVid) перед отображением меню. Как только я нажимаю старт, вместо того, чтобы показывать второе видео (до загрузки уровня0), я получаю сообщение об ошибке, что vidTexture2 имеет значение NULL.

Кто-нибудь знает, как добавить несколько видео? вот код для класса Video Manager:

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

    }
}

и Game1:

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);

        }
    }
}
Теги:
video
xna

1 ответ

0

Вы должны проверить, не является ли текстура нулевой, я предлагаю вам проверить 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

Ещё вопросы

Сообщество Overcoder
Наверх
Меню