I have the following code:
if (i==0) sprites[i].y = 0; else sprites[i].y = sprites[i-1].y + sprites[i-1].height;
Every Sprite can have a different height. Height of a Sprite can change on user action.
When the height of some Sprite changes, I would like to update the position of other sprites accordingly.
I am looking for an object oriented (maybe event driven) solution. Any ideas?
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:
Then just make sure that whenever you change the height of one of your sprites you do something like this:
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.
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.
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!