Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe your attempt to combine the if statements into one stems from your problem actually understanding the statement. As a programmer, your first and foremost duty is to write readable code even if you're the only person ever to read it. The other reason I can think of why you want only one if statement is that somehow you think this will be faster. </p> <p>So first I would start by moving all calculations out of the if clauses. Not only is this repetitive, it is also error prone and very hard to read. I've also taken the liberty to replace division by two with multiplication by 0.5f because the ARM processors do not have a division instructions, therefore the divisions are executed by software and rewritten as multiplication anyway. It's possible the compiler already optimizes this but nevertheless good practice to avoid division operations if you can.</p> <p>So here's how I would write the statement. The additional local variables cause no performance penalty. Usually the compiler will do something like that anyway. Similarly, if spriteA.tag is not 1, the condition doesn't evaluate the rest of the first if condition and jumps right to testing the else if part. This is called "early out": on the first test which will cause the entire condition to fail the program will stop evaluating the remaining tests because there would be no point in doing so. Therefore it is good practice to begin an if statement (or nested ifs) with the condition that is most often going to fail.</p> <pre><code>float spriteAHalfWidth = spriteA.boundingBox.size.height * 0.5f; float spriteBHalfWidth = spriteB.boundingBox.size.height * 0.5f; float spriteALeftSide = spriteA.position.x - spriteAHalfWidth; float spriteARightSide = spriteA.position.x + spriteAHalfWidth; float spriteBLeftSide = spriteB.position.x - spriteBHalfWidth; float spriteBRightSide = spriteB.position.x + spriteBHalfWidth; if (died == NO) { if (spriteA.tag == 1 &amp;&amp; spriteB.tag == 4 &amp;&amp; spriteALeftSide &lt;= spriteBRightSide) { float y = spriteB.position.y + spriteBHalfWidth + sprite.boundingBox.size.height * 0.5f; sprite.position = ccp(pos.x, y); } else if (spriteA.tag == 4 &amp;&amp; spriteB.tag == 1 &amp;&amp; spriteBLeftSide &lt;= spriteARightSide) { float y = spriteA.position.y + spriteAHalfWidth + sprite.boundingBox.size.height * 0.5f; sprite.position = ccp(pos.x, y); } } </code></pre> <p>Needless to say, you can't combine this into a single if statement. At the end you perform two different functions (update position based on either spriteA's or spriteB's y position). Therefore you require to have two branches.</p> <p>If you wanted to, you could add <code>died == NO</code> as the first test in both nested if statements if you really want to avoid the outermost if clause. However doing so will have your program always test for <code>died == NO</code> twice even though the died variable will not change. On the other hand it's possible that the compiler also recognizes and optimizes this situation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload