Forum Thread
by member
  Posts  
Need help/assistance with drawing multiple sprites in random positions.... (Forums : Coding & Scripting : Need help/assistance with drawing multiple sprites in random positions....) Locked
Thread Options
Aug 29 2012, 7:15pm Anchor

Hi everyone :).

Basically the problem that i am faced with is i am trying to draw multiple sprites to screen, giving them random positions each time they are drawn, so when the game first starts up, these 3 (i am using 3 sprites atm) should be given random positions on screen and then be drawn there.... and stay there for now, however the problem for me atm is they are given random positions but it seems that this is occuring 60 times a second, i.e. they are given random locations each frame or each time the draw method is called.
 The code for the procedure where i draw the sprites is included below along with the draw and update methods i have in place atm. 

csharp code:
//creating this variable at the top of my code, [im calling it object as its a game object and just for a suitable name, however i am using a structure atm to keep things simple]

theObject currentObject;



        //Update method (atm)
        protected override void Update(GameTime gameTime)
        {
            HandleInput();
            base.Update(gameTime);
        }


        //Draw method (atm)
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
           
            spriteBatch.Begin();
            SetupGame(gameTime);
            spriteBatch.End();
           
            base.Draw(gameTime);
        }


        //METHOD WHERE I DRAW EACH SPRITE
        private void SetupWayBlocks()
        {
            int windowX = graphics.PreferredBackBufferWidth;
            int windowY = graphics.PreferredBackBufferHeight;
            for (int i = 0; i < 3; i++)
            {
                currentObject.RanX = currentObject.theRandX.Next(20, windowX);
                currentObject.RanY = currentObject.theRandY.Next(20, windowY);
                spriteBatch.Draw(currentObject.Texture, new Vector2(currentObject.RanX, currentObject.RanY), Color.Green);
            }
        }

 I have purposely forgot to include the SetupGame method, as this just calls the SetupWayBlocks method from it, (just in case anyone says you havent called it :S)Thanks for taking the time to read this and for the help :) 

Aug 29 2012, 7:22pm Anchor

your probably invoking the random  function somewhere in your Update() thread, since you are saying it happens every 60 seconds which is how many times it loops. i dont understand why you are assigning random values and then drawing it in the setupwayblocks() function. put the starting random positions in the constructor and nowhere else.

Aug 29 2012, 8:25pm Anchor

Hmm im not using it at all in the HandleInput method, so if thats what you mean. Soz what do u mean exactly by invoking the random function?

I am only guessing that it happens every 60 seconds... i mean the sprites are being drawn alot (or they look like they are flashing, as they are being drawn in random positions so many times). Oh soz, i meant they are being drawn 60 times per second if that wasnt clear :).

Yea i did try and give a random position only once and i did place it elsewhere... well in the initialise method... because i didnt know any other method to put it in that isnt called 60 times a second :D.

Thanks for the help btw

--

I wrote: "The solution to any problem is to break it up into smaller parts and attack it with logic"

I wrote: "I hate the term 'coding', as it's being thrown around alot by what seems to be the code monkeys, as oppose to the others that stand back and try to solve the problem logically first. FYI I prefer the term 'programming'.

This seems to be an interesting read: Thomasinterestingblog.wordpress.com. This also shows the a programmer requires more skill than is required for a coder, so a programmer would therefore be a more valuable asset to a team compared to a coder."

User Posted Image

Aug 29 2012, 8:36pm Anchor

try putting it in Game1()

Aug 29 2012, 11:14pm Anchor

Ok but i think i solved it like 5 minutes after my last post. I have put it into the load content method and it seems to be working atm. Do you think its ok to still keep it in there, as that method is only called once whilst the game is running. :)

Atm, i am trying to, (when random positions are generated) to make it so that no two blocks are overlapping each other, otherwise a new random position is generated.

It may seem quite strange but for purposes to simply the code at this stage, i am using structures and for each texture drawn, i am just using the same an instance of that structure, so i dont think they are like seperate objects, or even different instances of an object being created.

Thanks for the help :)

Ok... the problem i face now is with using/creating rectangles that are the same size as each of the blocks. I am going to use rectangles, so its easier for collision detection. I do not know what the height and width of each rectangle should be for each block, as each of the blocks are the same size and this block is simply a white square created in paint, with dimensions of 25 by 25 px (im assuming thats pixels).

How would i find the height and width of each of the blocks and/or how would i know what dimensions for the rectangles should be, if i want the rectangles to be exactly the same size as the blocks that are drawn?

I am also finding the problem that even though the squares/blocks are drawn in a random position each time, they are only drawn ... hmm well to explain it they only randomly are positioned atm in a pattern like a negative correlated graph, so on a graph, or more relavently, in the game window, the blocks are randomly positioned in the general shape of:  \

Do you know how this could even happen, i mean i randomly generate the x and y vals for each block from a min val of 5 to a max val of the screenheight (for the y value) - 50, as i have found from messing around that - 50 is best to keep the whole block on the screen :).

Thanks for the help :)

Edited by: Raxsta

--

I wrote: "The solution to any problem is to break it up into smaller parts and attack it with logic"

I wrote: "I hate the term 'coding', as it's being thrown around alot by what seems to be the code monkeys, as oppose to the others that stand back and try to solve the problem logically first. FYI I prefer the term 'programming'.

This seems to be an interesting read: Thomasinterestingblog.wordpress.com. This also shows the a programmer requires more skill than is required for a coder, so a programmer would therefore be a more valuable asset to a team compared to a coder."

User Posted Image

Reply to Thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.