2013-11-21 2 views
1

я нагруженной звуковой файл эффекта в основной класс в XNA, но когда я пытаюсь запустить его в другом классе я дал эту ошибкуXNA Soundeffect является недействительным в том, когда загружен в основной класс и называется в другом классе

{ «ссылка на объект не указывает на экземпляр объекта.»}

Я новичок в классы XNA, так что я не очень понимаю их. Любая помощь как можно скорее приветствуется.

это часть основного класса

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; 

namespace BreakingOut 
{ 
/// <summary> 
/// This is the main type for your game 
/// </summary> 
public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 

    Ball ball; 
    Paddle paddle; 
    Rectangle screenRectangle; 
    bool is1P = true; 

    int lives = 3; 

    Texture2D startButtonTexture; 
    Vector2 buttonVec; 

    Texture2D exitButtonTexture; 
    Vector2 button2Vec; 

    int bricksWide = 10; 
    int bricksHigh = 7; 
    Texture2D brickImage; 
    Brick[,] bricks; 

    //rec for menu 
    Texture2D menuTexture; 
    Rectangle menuRec = new Rectangle(0, 0, 960, 640); 
    //rec for pause 
    Texture2D pauseTexture; 
    Rectangle pauseRec = new Rectangle(0, 0, 960, 640); 
    //lose 
    Texture2D loseTexture; 
    Rectangle loseRec = new Rectangle(0, 0, 960, 640); 

    Texture2D winTexture; 
    Rectangle winRec = new Rectangle(0, 0, 960, 640); 

    //sound effects 
    public SoundEffect BallHitSE; 
    SoundEffect WallHitSE; 
    SoundEffect StartSE; 
    //BGM 
    Song menuMusic; 
    Song gameMusic; 

    bool isGameMusicPlaying = false; 
    bool isMenuMusicPlaying = false; 

    float pauseTimer; 
    float exitTimer; 
    float muteTimer; 
    float SEmuteTimer; 

    //public int brickCount = 70; 

    SpriteFont font; 

    Vector2 mousePos; 

    enum gameStates 
    { 
     MENU, 
     GAME1P, 
     LOSE, 
     PAUSE, 
     WIN, 
    } 

    gameStates currentState = gameStates.MENU; 
    gameStates prevState = gameStates.MENU; 

    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 

     IsMouseVisible = true; 

     graphics.PreferredBackBufferWidth = 960; 
     graphics.PreferredBackBufferHeight = 640; 

     screenRectangle = new Rectangle(
      0, 
      0, 
      graphics.PreferredBackBufferWidth, 
      graphics.PreferredBackBufferHeight); 
    } 

    /// <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() 
    { 
     // 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); 

     Texture2D tempTexture = Content.Load<Texture2D>("paddle"); 
     paddle = new Paddle(tempTexture, screenRectangle); 

     BallHitSE = Content.Load<SoundEffect>("BallHit"); 
     WallHitSE = Content.Load<SoundEffect>("WallHit2"); 
     StartSE = Content.Load<SoundEffect>("Start"); 
     gameMusic = Content.Load<Song>("gameMusic"); 
     menuMusic = Content.Load<Song>("menuMusic"); 

Это класс Бал

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 

namespace BreakingOut 
{ 
class Ball 
{ 
    Vector2 motion; 
    Vector2 position; 
    Rectangle bounds; 
    bool collided; 

    public int brickCount = 70; 

    const float ballStartSpeed = 6.4f; 
    public float ballSpeed; 

    Texture2D texture; 
    Rectangle screenBounds; 


    SoundEffect BallHitSE; 
    //BallHitSE = new SoundEffect 

    public Rectangle Bounds 
    { 
     get 
     { 
      bounds.X = (int)position.X; 
      bounds.Y = (int)position.Y; 
      return bounds; 
     } 
    } 

    public Ball(Texture2D texture, Rectangle screenBounds) 
    { 
     bounds = new Rectangle(0, 0, texture.Width, texture.Height); 
     this.texture = texture; 
     this.screenBounds = screenBounds; 
     //Different things we tried 
     //BallHitSE = Content.Load<SoundEffect>("BallHit"); 
     //BallHitSE = new soundeffect(what goes in here?); 
     //BallHitSE = Content.Manager.Load("path") 

    } 

    public void Update() 
    { 
     collided = false; 
     position += motion * ballSpeed; 
     ballSpeed += 0.001f; 

     CheckWallCollision(); 
    } 

    public void CheckWallCollision() 
    { 
     if (position.X < 0) 
     { 
      position.X = 0; 
      motion.X *= -1; 
      //if (BallHitSE != null) 
      //{ 
       BallHitSE.Play(); 
      //} 
     } 
     if (position.X + texture.Width > screenBounds.Width) 
     { 
      position.X = screenBounds.Width - texture.Width; 
      motion.X *= -1; 
      //if (BallHitSE != null) 
      //{ 
       BallHitSE.Play(); 
      //} 
     } 
     if (position.Y < 0) 
     { 
      position.Y = 0; 
      motion.Y *= -1; 
      //if (BallHitSE != null) 
      //{ 
       BallHitSE.Play(); 
      //} 
     } 
    } 

    public void SetInStartPosition(Rectangle paddleLocation) 
    { 
     Random rand = new Random(); 

     motion = new Vector2(rand.Next(2, 6), -rand.Next(2, 6)); 
     motion.Normalize(); 

     ballSpeed = ballStartSpeed; 

     position.Y = paddleLocation.Y - texture.Height; 
     position.X = paddleLocation.X + (paddleLocation.Width - texture.Width)/2; 
    } 

    public bool OffBottom() 
    { 
     if (position.Y > screenBounds.Height) 
      return true; 
     return false; 
    } 

    public void PaddleCollision(Rectangle paddleLocation) 
    { 
     Rectangle ballLocation = new Rectangle(
      (int)position.X, 
      (int)position.Y, 
      texture.Width, 
      texture.Height); 

     if (paddleLocation.Intersects(ballLocation)) 
     { 
      position.Y = paddleLocation.Y - texture.Height; 
      motion.Y *= -1; 
     } 
    } 

    public void Deflection(Brick brick) 
    { 
     if (!collided) 
     { 
      motion.Y *= -1; 
      collided = true; 
      brickCount -= 1; 
     } 
    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(texture, position, Color.White); 
     for (int i = 1; i < 10; i++) 
     { 
      var drawPosition = position - (i * motion * 3); 
      var colour = Color.Lerp(Color.White, Color.Transparent, i * (20/100f)); 
      spriteBatch.Draw(texture, drawPosition, colour); 
     } 
    } 
} 
} 
+0

Колтон имеет ответ для вас, но я думаю, вы должны сначала попытаться получить общее представление о том, как классы работают в C#. –

ответ

0

пользователь Kai Hartmann решить этот вопрос с легким и простым исправлением

Убедитесь, что Болл инстанциируется перед тем LoadContent().

Затем поменять эту строку

BallHitSE = Content.Load ("BallHit"); с этим

Ball.BallHitSE = Содержимое.Нагрузка ("BallHit"); и измените переменную в вашем классе Ball на этот

public static SoundEffect BallHitSE;

https://gamedev.stackexchange.com/a/66184/36332

1

классы XNA не отличаются, чем обычный класс, не позволяйте им обмануть вас!

Кажется, вам просто нужно установить две части, равные друг другу.

В классе шара:

public Ball(Texture2D texture, Rectangle screenBounds, SoundEffect ballHit/*NEW*/) 
{ 
    this.BallHitSE = ballHit;// NEW 

    bounds = new Rectangle(0, 0, texture.Width, texture.Height); 
    this.texture = texture; 
    this.screenBounds = screenBounds; 
    //Different things we tried 
    //BallHitSE = Content.Load<SoundEffect>("BallHit"); 
    //BallHitSE = new soundeffect(what goes in here?); 
    //BallHitSE = Content.Manager.Load("path") 

} 

Тогда просто добавить звуковой эффект в конструктор:

Ball ball = new Ball(texture, bounds, BallHitSE); 
+0

к сожалению это не сработало для меня – Evan

+0

Является ли ошибка все-таки такой же? Кроме того, я не думаю, что это будет иметь значение, но убедитесь, что вы загружаете эффект, прежде чем отправлять его в Ball. – Colton

+0

Да, я уверен, я пробежал код, когда сначала это проверило. Я также попытался положить в него нуль, чтобы попытаться обходить его, но он только остановил его от сбоя. – Evan

1

Если вы используете те SoundEffect в вашем Ball классе вам следует загрузить их в LoadContent из ваш класс Ball, а не в основном классе.
Вот почему вы получаете эту ошибку, когда пытаетесь запустить ее, они инициализируются в другом классе и по-прежнему являются нулевыми.

Конечно, если вы должны загрузить что-то в Ball классе вам нужно пройти ContentManager, или сделать что Ball класс наследует от GameComponent или DrawableGameComponent для того, чтобы загрузить свои звуковые эффекты таким образом:

BallHitSE = Game.Content.Load<SoundEffect>("BallHit"); 
WallHitSE = Game.Content.Load<SoundEffect>("WallHit2"); 

EDIT

Обычно мне удается аудио таким образом:

public class AudioComponent : GameComponent 
{ 
    public enum LevelEffect { BadCollision, GoodCollision, PopUp } 
    public enum GameMusic { GameWin, GameLose, GameStart } 

    private Song winSound, loseSound, startSound; 
    private SoundEffect badCollSound, goodCollSound, popUp; 

    public AudioComponent() 
    { 
    } 


    public override void Initialize() 
    { 
     base.Initialize(); 
    } 


    public void loadAudio(
     Song winSound, 
     Song loseSound, 
     Song startSound, 
     SoundEffect badCollSound, 
     SoundEffect goodCollSound, 
     SoundEffect popUp 
     ) 
    { 
     this.winSound = winSound; 
     this.loseSound = loseSound; 
     this.startSound = startSound; 
     this.badCollSound = badCollSound; 
     this.goodCollSound = goodCollSound; 
     this.popUp = popUp; 
    } 


    public void PlayGameMusic(GameMusic effType) 
    { 
     switch (effType) 
     { 
      case GameMusic.GameWin: 
       PlayMusic(winSound); 
       break; 
      case GameMusic.GameLose: 
       PlayMusic(loseSound); 
       break; 
      case GameMusic.GameStart: 
       PlayMusicRepeat(startSound); 
       break; 
     } 
    } 


    public void PlayLevelEffect(LevelEffect effType) 
    { 
     switch (effType) 
     { 
      case GameMusic.BadCollision: 
       PlayEffect(badCollSound); 
       break; 
      case GameMusic.GoodCollision: 
       PlayEffect(goodCollSound); 
       break; 
      case GameMusic.PopUp: 
       PlayEffect(popUp); 
       break; 
     } 
    } 


    public void PlayEffect(SoundEffect sound) 
    { 
     sound.Play(); 
    } 


    public bool CheckAlreadyPlayingMusic() 
    { 
     return MediaPlayer.State == MediaState.Playing; 
    } 


    public void PlayMusic(Song song) 
    { 
     MediaPlayer.Play(song); 
     MediaPlayer.IsRepeating = false; 
    } 


    public void PlayMusicRepeat(Song song) 
    { 
     MediaPlayer.Play(song); 
     MediaPlayer.IsRepeating = true; 
    } 


    public void StopMusic() 
    { 
     MediaPlayer.Stop(); 
    } 


    public void PauseMusic() 
    { 
     MediaPlayer.Pause(); 
    } 


    public void ResumeMusic() 
    { 
     MediaPlayer.Resume(); 
    } 


    public void ChangeVolume(float volume) 
    { 
     MediaPlayer.Volume = volume; 
    } 
} 
+0

У меня есть класс SoundEffect с процедурой «Загрузить», где загружаются все звуки в общедоступные переменные из 'LoadContent'. поэтому везде мне нужен какой-то звук, я просто называю 'SoundEffect.Kaboom.Play()' ... что-то вроде этого. –

+0

@DavorMlinaric Я использую класс 'AudioManager', и когда мне нужен звук, я просто называю' audioManager.PlayEffect (effectName) ', где' effectName' является перечислением. – pinckerman

+0

Я получил эту ошибку при ее попытке Ошибка Ссылка на объект обязательна для нестатического поля, метода или свойства «Microsoft.Xna.Framework.Game.Content.get» – Evan

Смежные вопросы