5 Replies - 347 Views - Last Post: 02 December 2011 - 11:27 AM Rate Topic: -----

#1 zem107  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 84
  • Joined: 10-April 11

Making a random number stop

Posted 01 December 2011 - 09:33 PM

I started XNA programming and ran into a problem pretty quickly.
I wanted to make my 2D sprite appear somewhere random on the screen, so I wrote this code:
Vector2 position = Vector2.Zero;
        public void rand(int a, int B)/>
        {
            Random rand = new Random();
            xTop = rand.Next(a);
            yTop = rand.Next(B)/>;
            position.X = xTop;
            position.Y = yTop;
        }


For whenever I needed to have a randomly placed sprite.
But whenever I run this code the sprite just won't stop appearing randomly. It goes nonstop!
I want it to be placed randomly and stop, until told to do so again.
I haven't got any errors and I can't see what I am doing wrong.
I even tried putting them in for and while loops, but it still won't work.

Is This A Good Question/Topic? 0
  • +

Replies To: Making a random number stop

#2 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Making a random number stop

Posted 01 December 2011 - 10:13 PM

Are you calling this method from the Update or where do you call it?
Was This Post Helpful? 0
  • +
  • -

#3 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,299
  • Joined: 22-October 08

Re: Making a random number stop

Posted 01 December 2011 - 11:21 PM

This is because you are calling the above code inside a loop, most presumably your game code where you render your sprites / cameras ect. However, you can randomly place a sprite using say a for loop out side the current loop. Where variable is lower than max sprites + 1.

Or, you can use the above code with slight modification. You can introduce variable sIndex of type int and increment it at the methods end. Then insert something like the following:

if( sIndex >= maxSprites + 1 )
{
    return;
}



Hope it helps.
Was This Post Helpful? 1
  • +
  • -

#4 zem107  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 84
  • Joined: 10-April 11

Re: Making a random number stop

Posted 02 December 2011 - 08:45 AM

Thanks #3 aaron1178, I had inside a loop which made it continuously appear. I placed it inside LoadContent() and it fixed the problem.
Was This Post Helpful? 0
  • +
  • -

#5 zem107  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 84
  • Joined: 10-April 11

Re: Making a random number stop

Posted 02 December 2011 - 08:51 AM

I have another problem. How can I register a mousse click. Say I have a grass background and stick that randomly appears, whenever the person clicks on the stick, how do I make it randomly appear again. I have no idea how to do this so I have no code for this part.
Was This Post Helpful? 0
  • +
  • -

#6 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Making a random number stop

Posted 02 December 2011 - 11:27 AM

Here is some tutorial i found on how to use the Mouse Input:
http://bluwiki.com/g...als/Mouse_Input

And here is someone asking for how to use keyboard and mouse in XNA and good answear on the question:
http://stackoverflow...licks-using-xna


Maybe you can make a rectangle or something like that around the stick so if the mouse click was in that box then call random again?
Try experiment alittle with it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1