DisplayObjectUtil class added to DestroyFramework
I’ve been sifting through my old framework, finding classes that are useful, but lacking in their current state—one of them being the fSprite class. This is easily my most-used class because it’s the base of every visual object. It has so much built-in and I now realize that isn’t exactly a good thing. Whenever I don’t need its layer management or motion methods, precious memory is lost. On top of that, there are visual objects that aren’t Sprites but could benefit from fSprite’s methods, such as the Shape class. After seeing Jackson Dunstan’s comparison of the two, I realized I could be saving a solid amount memory and instantiation speed by using Shape instead of Sprite where possible.
Because of these realizations, I decided to write a DisplayObjectUtil class. It starts with my layer management methods and will grow to include many others. In the following methods, if the object isn’t the child of a parent, it returns -1 instead of throwing an error.
The bringToFront method takes a DisplayObject and brings it to the front of the display list. It also includes an optional back parameter in the case you don’t want the object in the very front, you can send it back a few.
1 2 3 4 5 | // DisplayObjectUtil.bringToFront(object:DisplayObject, back:int = 0):int // consider sprite's index is 2 in a display list of 5 objects DisplayObjectUtil.bringToFront(sprite); // returns new index of 4 DisplayObjectUtil.bringToFront(sprite, 1); // returns new index of 3 |
Along with bringToFront, I wrote bringForward, which moves the object up in the display list the set number of times. If the new index exceeds the highest index, it limits itself to that instead of throwing an error.
1 2 3 4 5 6 7 8 | // DisplayObjectUtil.bringForward(object:DisplayObject, steps:int = 1):int // consider sprite's index is 2 in a display list of 5 objects DisplayObjectUtil.bringForward(sprite); // returns new index of 3 // consider sprite's index is 1 in a display list of 5 objects DisplayObjectUtil.bringForward(sprite, 2); // returns new index of 3 DisplayObjectUtil.bringForward(sprite, 9); // returns new index of 4 |
On the reverse, I wrote methods sendToBack and sendBackward. Both work exactly the same as the methods above, but in the reverse direction.
1 2 3 4 5 | // DisplayObjectUtil.sendToBack(object:DisplayObject, forward:int = 0):int // consider sprite's index is 2 in a display list of 5 objects DisplayObjectUtil.sendToBack(sprite); // returns new index of 0 DisplayObjectUtil.sendToBack(sprite, 1); // returns new index of 1 |
1 2 3 4 5 6 7 8 | // DisplayObjectUtil.sendBackward(object:DisplayObject, steps:int = 1):int // consider sprite's index is 2 in a display list of 5 objects DisplayObjectUtil.sendBackward(sprite); // returns new index of 1 // consider sprite's index is 3 in a display list of 5 objects DisplayObjectUtil.sendBackward(sprite, 2); // returns new index of 1 DisplayObjectUtil.sendBackward(sprite, 9); // returns new index of 0 |
I hope this class lends a hand and proves useful. Feel free to suggest any other methods DisplayObjectUtil should have.












