random block placment switch

Started by
5 comments, last by raptorstrike 19 years, 7 months ago
ok i have this pice of code here

            for (int l = 0; l <30; l++)
              { 
                blocks[l]->SetPosition(rand()%500, rand()%450);
                      for(int s=0; s < 30; s++)
                         {
                             if( blocks[l]->TestCollision(blocks) == <span class="cpp-keyword">TRUE</span>)
                             {
                              <span class="cpp-keyword">if</span> (blocks != blocks[l])
                               blocks[l]-&gt;SetPosition(rand()%<span class="cpp-number">500</span>, rand()%<span class="cpp-number">450</span>);
                             };    
                          };
             };        
          <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> y = <span class="cpp-number">0</span>; y &lt;<span class="cpp-number">30</span>; y++)
          {
              blocks[y]-&gt;SetHidden(<span class="cpp-keyword">false</span>);
          }; 
             
                      
       };
       totalblocks=<span class="cpp-number">29</span>; 
   };


</pre></div><!–ENDSCRIPT–>
in my GameStart function to load the blocks randomly

but when i do this

<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
 <span class="cpp-keyword">switch</span> (totalblocks)
    {
        <span class="cpp-keyword">case</span>  <span class="cpp-number">28</span>: 
        {
        
            <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> l = <span class="cpp-number">0</span>; l &lt;<span class="cpp-number">30</span>; l++)
              { 
                blocks[l]-&gt;SetPosition(rand()%<span class="cpp-number">500</span>, rand()%<span class="cpp-number">450</span>);
                      <span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> s=<span class="cpp-number">0</span>; s &lt; <span class="cpp-number">30</span>; s++)
                         {
                             <span class="cpp-keyword">if</span>( blocks[l]-&gt;TestCollision(blocks) == <span class="cpp-keyword">TRUE</span>)
                             {
                              <span class="cpp-keyword">if</span> (blocks != blocks[l])
                               blocks[l]-&gt;SetPosition(rand()%<span class="cpp-number">500</span>, rand()%<span class="cpp-number">450</span>);
                             };    
                          };
             };        
          <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> y = <span class="cpp-number">0</span>; y &lt;<span class="cpp-number">30</span>; y++)
          {
              blocks[y]-&gt;SetHidden(<span class="cpp-keyword">false</span>);
          }; 
             
                      
       };
       totalblocks=<span class="cpp-number">29</span>; 
   };

</pre></div><!–ENDSCRIPT–>
in my GamePaint function it dosnt work sure most of them are seprate but some of them are &#111;n top of each other and that screws up my collision detection. i cant figure out what wrong here maybe you guys can 
thx in advance. [smile]

windows API
dev-cpp
4.9.9.0
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
You test for a collison. If there is you try again. But you dont then test to see if your second attempt failed. It might have collided again. You need a while loop to retest for collison everytime you try to place the block.
i tryed that i put this line
while ( blocks[l]->TestCollision(blocks) == <span class="cpp-keyword">TRUE</span>)<br>         <span class="cpp-keyword">if</span> (blocks != blocks[l])<br>                               blocks[l]-&gt;SetPosition(rand()%<span class="cpp-number">500</span>, rand()%<span class="cpp-number">450</span>);<br><br></pre></div><!–ENDSCRIPT–><br>but when it got time to exicute the statement the whole thing just froze up
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
           for (int l = 0; l <30; l++)              {                 blocks[l]->SetPosition(rand()%500, rand()%450);		bool IsNotSet=true;                		while (1)		{			bool Failed=false;	        	for(int s=0; s < 30; s++)	                {                             if( blocks[l]->TestCollision(blocks) == <span class="cpp-keyword">TRUE</span>)<br>                             {<br>                              	<span class="cpp-keyword">if</span> (blocks != blocks[l])<br>                               	blocks[l]-&gt;SetPosition(rand()%<span class="cpp-number">500</span>, rand()%<span class="cpp-number">450</span>);<br>				Failed=<span class="cpp-keyword">true</span>;<br>                             }<br>                        }<br>			<span class="cpp-keyword">if</span> (!Failed) <span class="cpp-keyword">break</span>;<br>		}<br>             }      <br>          <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> y = <span class="cpp-number">0</span>; y &lt;<span class="cpp-number">30</span>; y++)<br>          {<br>              blocks[y]-&gt;SetHidden(<span class="cpp-keyword">false</span>);<br>          }<br>             <br>                      <br>       }<br>       totalblocks=<span class="cpp-number">29</span>; <br>   }<br><br></pre></div><!–ENDSCRIPT–><br><br>Try that.
still crashes
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Sorry try putting braces round

blocks[l]->SetPosition(rand()%500, rand()%450);
Failed=true;

(otherwise it will always fail, sorry!!)
yey it works thanks alot you guys

[edit] guss i got ahead of my self well it works even better then it did before but im still getting 1 or 2 thinks overlapped i dont know why

do you think i can just ignore it. it isnt very common it must be an exception or somthing thats throwing it off
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie

This topic is closed to new replies.

Advertisement