XNA Creators Club Online
Page 1 of 1 (10 items)
Sort Posts: Previous Next

XNA Newbie - Random Number Generator?

Last post 5/11/2008 3:08 AM by Greenskull. 9 replies.
  • 5/9/2008 10:12 PM

    XNA Newbie - Random Number Generator?

    Hey everyone. I'm kinda new to coding but I'm figuring it out slowly. I just have one question at the moment. How would I go about making a random number and then using it? So for example, I want a random song to play. So I want it to play one song if the number is one and another if it's 2. How do I do this? Thanks in advance.

  • 5/9/2008 10:17 PM In reply to

    Re: XNA Newbie - Random Number Generator?

    Well you create a new Random object. Then tell it to give you a new random number within a certain range. Then use that to get the song.

    Random rand = new Random();

    int songIndex = rand.Next(2);

    if (songIndex == 0)
    //play song 1
    else if (songIndex == 1)
    //play song 2

    The number you give to Next is exclusive meaning that you will get a non-negative number less than that value.
  • 5/9/2008 10:25 PM In reply to

    Re: XNA Newbie - Random Number Generator?

    this is strange. For some reason the game crashes when I start it up. The game never even starts! Here's the code, tell me if there's anything wrong.

     

    public void PlayActionMusic()
    {

    Random rand = new Random();
    int songIndex = rand.Next(2);
    if (songIndex == 0) //play song 1
    MediaPlayer.Play(actionMusic);
    else if (songIndex == 1) //play song 2
    MediaPlayer.Play(actionMusic2);





    }
  • 5/9/2008 10:44 PM In reply to

    Re: XNA Newbie - Random Number Generator?

    Where are you loading in actionMusic and actionMusic2? What error are you getting?
  • 5/9/2008 11:36 PM In reply to

    Re: XNA Newbie - Random Number Generator?

    public void LoadContent(ContentManager Content)

    {

    newMeteor = Content.Load<SoundEffect>("newmeteor");

    menuSelect = Content.Load<SoundEffect>("menu_select3");

    menuScroll = Content.Load<SoundEffect>("menu_scroll");

    menuBack = Content.Load<SoundEffect>("menu_back");

    explosion = Content.Load<SoundEffect>("explosion");

    powerget = Content.Load<SoundEffect>("powerget");

    powershow = Content.Load<SoundEffect>("powershow");

    actionMusic = Content.Load<Song>("backmusic");

    actionMusic2 = Content.Load<Song>("fpbeat");

    openMusic = Content.Load<Song>("startmusic");

    // All music must be in loop

    MediaPlayer.IsRepeating = true;

    }

     

    And it also plays in a loop, might have to fix that...

     

    hmm it's saying that fpbeat doesnt exist...it's named correctly though....

  • 5/10/2008 1:11 PM In reply to

    Re: XNA Newbie - Random Number Generator?

    I still can't figure out why it's saying fpbeat doesn't exist...can anyone help?
  • 5/10/2008 1:43 PM In reply to

    Re: XNA Newbie - Random Number Generator?

    I'm assuming that you have added the fpbeat to the project and that it's a .wav file because the content pipeline just support that file format. (unless you have another sound engine like Windows MCI or ActiveX sound playback codecs)

    There's a few things you can do to make sure the file is used properly:

    -Make sure the build action of the fpbeat is set to 'Compile'. Otherwise your game will not recognize the file.
    -Check the asset name. You may have changed it in some way.

  • 5/10/2008 3:23 PM In reply to

    Re: XNA Newbie - Random Number Generator?

    Hetraq:

    I'm assuming that you have added the fpbeat to the project and that it's a .wav file because the content pipeline just support that file format. (unless you have another sound engine like Windows MCI or ActiveX sound playback codecs)

    There's a few things you can do to make sure the file is used properly:

    -Make sure the build action of the fpbeat is set to 'Compile'. Otherwise your game will not recognize the file.
    -Check the asset name. You may have changed it in some way.

    has to be wav? I'll try it
  • 5/10/2008 3:29 PM In reply to

    Re: XNA Newbie - Random Number Generator?

    No. Here's how it works (by default):

    wav files = SoundEffect
    mp3 or wma files = Song

    As long as you have an mp3 or wma in your content project, you should be able to load it as a Song no problem. Is your song in a folder? If it is you need to add that to the name when loading.
  • 5/11/2008 3:08 AM In reply to

    Re: XNA Newbie - Random Number Generator?

    haha! I figured it out! I needed to add the file to the solution explorer! Makes you happy when you figure it out yourself. Now can someone tell me how to make it so that it randomize the song upon finishing the old song? Also, is there any way to set options for what happens when an angle is pressed on the dpad? Like top left? Using gamepadstatus.DPad.somthing? Thanks for everyone's help, by the way, I'm just learning how to code and everyone is very kind about helping me.
Page 1 of 1 (10 items) Previous Next