-2

for my university, we have to start with learning Monogame/XNA on VisualStudio 2015. We have to make a Sprite which rotate in a circle and makes a soundeffect by clicking on it abd a soundeffect by missing it. But i can't figure it out how to make it.

Everything else just works fine (after hours of work). I would be very thankful for any help. Thank you in advance.

best regards Alex.

PS: Sorry for my english :)

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);
    }
}
}
Sancho
  • 1
  • 2
  • Please expand on your issue. What is not working? Are you receiving any compile errors? – S1r-Lanzelot Apr 29 '16 at 21:02
  • i have no issues, i just need to make a method which makes a sound by clicking on a logo. – Sancho Apr 29 '16 at 21:14
  • Give it a try then! If you run into compiling errors that you don't understand or your code is doing something weird, come back and then ask for assistance with the particular issue that you're having. – S1r-Lanzelot Apr 29 '16 at 21:21
  • I changed the update method. The problem is now, that the sound effect plays not exactly when i hit the logo. It plays when i click the mouse underneath the logo. – Sancho Apr 30 '16 at 10:31
  • SOLVED: The issue was in the origin Vector. I made him to a Vector.Zero and changed the values at the position vector. A little bit confusing but it works. – Sancho Apr 30 '16 at 11:03

1 Answers1

0

Maybe try something like this? (NOTE: this is only a fragment for collision logic, not everything you might need.)

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);
    }
}
Epic
  • 61
  • 1
  • 4