0

インスタント ボタン アプリのように、ボタンをクリックしてサウンドを作成するプログラムを作成しています。しかし、ボタンをクリックしても音が出ません。理由もわかりません。私の「ビルド」デバッグもすべて成功しています。

これらは私のクラスです:

Game1.cs:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D Background;
    Button button;



    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        this.Window.Title = "Bengt Slår På Knappar";
        graphics.PreferredBackBufferHeight = 720;
        graphics.PreferredBackBufferWidth = 1280;
        IsMouseVisible = true;
    }

    /// <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()
    {
        Background = Content.Load<Texture2D>("bengtbakgrund");
        Texture2D Btexture = Content.Load<Texture2D>("Button");
        SoundEffect dundundun = Content.Load<SoundEffect>("DUN DUN DUN");
        button = new Button(dundundun, Btexture);

        spriteBatch = new SpriteBatch(GraphicsDevice);


    }

    /// <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)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        Rectangle mouseRect = new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 10, 10);
        MouseState mouseState = Mouse.GetState();


        if (mouseRect.Intersects(button.rect) && mouseState.LeftButton == ButtonState.Pressed)
        {
            button.MySound.Play();
        }

        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        spriteBatch.Draw(Background, new Vector2(0,0), Color.White);
        button.Draw(spriteBatch, new Vector2(300, 300));
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

ボタン.cs:

public class Button
{

    Texture2D Texture { get; set; }
    public SoundEffect MySound { get; set; }
    public Rectangle rect;


    public Button(SoundEffect mySound, Texture2D texture)
    {
        MySound = mySound;
        Texture = texture;
    }

    public void LoadContent(ContentManager Content)
    {

    }

    public void Update(GameTime gameTime)
    {

    }

    public void Draw(SpriteBatch spriteBatch, Vector2 location)
    {
        Rectangle rect = new Rectangle((int)0, (int)0, Texture.Width, Texture.Height);

        spriteBatch.Draw(Texture, location, Color.White);
    }
}

私がやろうとしているのは、1 つの基本ボタン クラスを用意することです。そのため、サウンド付きの新しいボタンを追加する場合は、次のように記述します。button = new Button(//sound effect, //texture);

しかし、私はそれを機能させることができません。どんな助けでも大歓迎です。

4

1 に答える 1

0

ボタンの位置を処理する方法が正しくないため、コンパイル方法がわかりません。draw メソッドrectで、既に存在するローカル変数を定義します。あったとしても、ボタンの実際の位置を考慮していません。

ボタンの更新ロジックをクラスに移動することもできると思いますので、あなたがする必要があると思うことを提案します。

public class Button { //プロパティ private Texture2D Texture { get; 設定; } プライベート サウンドエフェクト サウンド { get; 設定; } プライベート レクタングル レクタングル { get; 設定; }

public Button(SoundEffect sound, Texture2D texture, Rectangle position)
{
    Sound = sound;
    Texture = texture;
    Rectangle = position;
}

public void Update(GameTime gameTime, MouseState mouseState)
{
      //If mouse is down and the rectangle contains the mouse point (Better for points than Intersect())
      if (mouseState.LeftButton == ButtonState.Pressed && Rectangle.Contains(new Point(mouseState.X,mouseState.Y))
      {
           Sound.Play()
      }
}

public void Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Draw(Texture, rectangle, Color.White);
}

}

その後、次のことができますbutton.Update(Mouse.GetState())(または、既にある場合は別の MouseState を使用します)

描画位置とボタンの実際の位置 (描画メソッド) に多少の混乱があったようです。

ボタンが画面上の位置と幅と高さを認識していることを確認する必要があります。

button = new Button(dundundun, Btexture, new Rectangle(300, 300, Btexture.Width, Btexture.Height);

これにより、ボタン300,500がテクスチャのデフォルトサイズになり、サウンドボタンが完成しました:)

また、イベント ハンドラを学習して使用することをお勧めします。これにより、次のようなボタンのイベントをbutton.Click += //Do something簡単に追加できます。

于 2013-08-15T23:14:37.603 に答える