Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you have good control over when the height of any given sprite is going to change then you can use a custom event. The setup would look something like this:</p> <pre><code>var UPDATED_EVENT:String = "updated"; var i:int = 0; var count:int = sprites.length; // this is the length of your array for (i = 0; i &lt; count; i++) { sprites[i].addEventListener(UPDATED_EVENT, calc); } function calc(e:Event = null):void { // here goes your positioning code for (i = 0; i &lt; count; i++) { if (i==0) sprites[i].y = 0; else sprites[i].y = sprites[i-1].y + sprites[i-1].height; } } calc(); </code></pre> <p>Then just make sure that whenever you change the height of one of your sprites you do something like this:</p> <pre><code>mySprite.dispatchEvent(new Event(UPDATED_EVENT)); </code></pre> <p>If you're spread across multiple classes (and you probably should be) then you can make the UPDATED_EVENT string a const in a separate event class and access it from both.</p> <p>That's still not a perfect solution for you - if you want, you can figure out which sprite is changing its height and only recalc the sprites that come after it, and that'll make your code more efficient - but it beats calling an enterframe event every time.</p> <p>Now, if you were using Flex you'd have even more tools at your disposal - you could probably use databinding to automate almost all of this, or at least use the changed event to automatically dispatch events for you, but that's neither here nor there!</p>
 

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