Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单游戏:点击精灵的声音效果_C#_Xna_Click_Monogame_Soundeffect - Fatal编程技术网

C# 单游戏:点击精灵的声音效果

C# 单游戏:点击精灵的声音效果,c#,xna,click,monogame,soundeffect,C#,Xna,Click,Monogame,Soundeffect,对于我所在的大学,我们必须从在VisualStudio 2015上学习Monogame/XNA开始。我们必须制作一个旋转成一个圆圈的精灵,点击它会产生声音效果,错过它会产生声音效果。但我不知道怎么做 其他一切正常(工作数小时后)。 我将非常感谢任何帮助。 先谢谢你 致意 亚历克斯 PS:对不起,我的英语:) 使用系统; 使用Microsoft.Xna.Framework; 使用Microsoft.Xna.Framework.Audio; 使用Microsoft.Xna.Framework.Gra

对于我所在的大学,我们必须从在VisualStudio 2015上学习Monogame/XNA开始。我们必须制作一个旋转成一个圆圈的精灵,点击它会产生声音效果,错过它会产生声音效果。但我不知道怎么做

其他一切正常(工作数小时后)。 我将非常感谢任何帮助。 先谢谢你

致意 亚历克斯

PS:对不起,我的英语:)

使用系统;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
名称空间Hausaufgabe_Uni_徽标
{
/// 
///这是游戏的主要类型。
/// 
公共类游戏1:游戏
{
图形副经理;
SpriteBatch mSpriteBatch;
私人纹理2D背景;
私人纹理2D标志;
私有向量2 mPos=向量2.0;
私人音响效果mHit;
私人音响效果MMIS;
私人住宅物业;
私人穆斯泰斯特庄园;
私人双槽;
私有向量2位置=新向量2(640512);
私有浮动mX=0;
私人浮动mY=0;
公共游戏1()
{
mGraphics=new GraphicsDeviceManager(此)
{
//将窗口大小更改为1280x1024
PreferredBackBufferWidth=1280,
PreferredBackBufferHeight=1024
};
Content.RootDirectory=“Content”;
}
/// 
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
/// 
受保护的覆盖无效初始化()
{
//设置鼠标可见
IsMouseVisible=true;
base.Initialize();
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
mSpriteBatch=新的SpriteBatch(图形设备);
//加载精灵
mBackground=Content.Load(“背景”);
mUniLogo=Content.Load(“Unilogo”);
mHit=Content.Load(“Logo_hit”);
mMiss=Content.Load(“Logo_miss”);
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///游戏特定内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed | | | Keyboard.GetState().IsKeyDown(Keys.Escape))
退出();
mPreviousMouseState=mCurrentMouseState;
mCurrentMouseState=Mouse.GetState();
如果(mCurrentMouseState.LeftButton==ButtonState.Pressed&&MPPreviousMousEstate.LeftButton!=ButtonState.Pressed)
{

如果(Mouse.GetState()

矩形spritect;
MouseState ms、oldms;
受保护的覆盖无效初始化()
{
//设置鼠标可见
IsMouseVisible=true;
spriteRect=新矩形(/*foo*/);
base.Initialize();
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
mSpriteBatch=新的SpriteBatch(图形设备);
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///游戏特定内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed | | | Keyboard.GetState().IsKeyDown(Keys.Escape))
退出();
ms=Mouse.GetState();
矩形=新矩形((int)ms.X,(int)ms.Y,1,1);
如果(ms.LeftButton==ButtonState.Pressed&&oldms.LeftButton!=ButtonState.Pressed){
if(垂直相交(斜交))
{
//播放声音
}
否则{
//播放不同的声音
}
}
更新(游戏时间);
}
}

请详细说明您的问题。什么不起作用?您是否收到任何编译错误?我没有问题,我只需要创建一个通过单击徽标发出声音的方法。然后再试一次!如果您遇到不理解的编译错误或您的代码正在执行某些奇怪的操作,请返回,然后向p寻求帮助您遇到的问题。我更改了更新方法。现在的问题是,当我点击徽标时,声音效果不完全播放。当我单击徽标下方的鼠标时,声音效果会播放。解决:问题在原始向量中。我将其设置为向量。零并更改了位置向量的值。有点混乱,但它仍然有效ks。
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Hausaufgabe_Uni_Logo
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
    GraphicsDeviceManager mGraphics;
    SpriteBatch mSpriteBatch;
    private Texture2D mBackground;
    private Texture2D mUniLogo;
    private Vector2 mPos = Vector2.Zero;
    private SoundEffect mHit;
    private SoundEffect mMiss;
    private MouseState mPreviousMouseState;
    private MouseState mCurrentMouseState;
    private double mAngle;
    private Vector2 mPosition = new Vector2(640, 512);



    private float mX = 0;
    private float mY = 0;

    public Game1()
    {
        mGraphics = new GraphicsDeviceManager(this)
        {
            // Change the windows size into 1280x1024
            PreferredBackBufferWidth = 1280,
            PreferredBackBufferHeight = 1024
        };

        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()
    {
        // Set mouse visible
        IsMouseVisible = true;
        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.
        mSpriteBatch = new SpriteBatch(GraphicsDevice);

        // Load sprites
        mBackground = Content.Load<Texture2D>("Background");
        mUniLogo = Content.Load<Texture2D>("Unilogo");

        mHit = Content.Load<SoundEffect>("Logo_hit");
        mMiss = Content.Load<SoundEffect>("Logo_miss");
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// game-specific 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)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        mPreviousMouseState = mCurrentMouseState;
        mCurrentMouseState = Mouse.GetState();
       if (mCurrentMouseState.LeftButton == ButtonState.Pressed && mPreviousMouseState.LeftButton != ButtonState.Pressed)
        {
            if (Mouse.GetState().Position.X >= mX && Mouse.GetState().Position.X <= mX + 300 &&
                Mouse.GetState().Position.Y >= mY && Mouse.GetState().Position.Y <= mY + 300)
            {
                mHit.Play();
            }
            else if (new Rectangle(0,0,1280,1024).Contains(Mouse.GetState().X, Mouse.GetState().Y) == true)
            {
                mMiss.Play();
            }
        }


        mAngle -= 0.01;
        mX = (float)Math.Sin(mAngle) * 350;
        mY = (float)Math.Cos(mAngle) * 250;


        base.Update(gameTime);

    }

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

        var viewport = mGraphics.GraphicsDevice.Viewport;
        // Draw our sprites
        mSpriteBatch.Begin();
        mSpriteBatch.Draw(mBackground, new Rectangle(0,0,1280,1024), Color.White);
        mSpriteBatch.Draw(mUniLogo, new Vector2(mX,mY), null, Color.White, 0, new Vector2(512, 512), 0.33f, SpriteEffects.None, 0);
        mSpriteBatch.End();

        base.Draw(gameTime);
    }
}
}
Rectangle spriteRect;
MouseState ms, oldms;

protected override void Initialize()
{
    // Set mouse visible
    IsMouseVisible = true;
    spriteRect= new Rectangle(/*foo*/);
    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.
    mSpriteBatch = new SpriteBatch(GraphicsDevice);

}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific 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)
{
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
        Exit();
    ms=Mouse.GetState();
    Rectangle mouseRect= new Rectangle((int)ms.X,(int)ms.Y,1,1);

   if(ms.LeftButton==ButtonState.Pressed && oldms.LeftButton!=ButtonState.Pressed){
if(mouseRect.Intersects(spriteRect))
        {
           //play a sound
        }
else{
//play a different sound
}

    }





    base.Update(gameTime);
    }
}