I am having issues with my ScreenManager
class which is designed to make it easier to add and remove sprites/screens without being too heavy on boilerplate code. Because I want to reduce the time it takes to add sprites and remove them, the ScreenManager is extremely beneficial. However, my draw
function does not seem to work as expected.
public function draw():Void
{
for (src in Screens)
if (!(this.getChildIndex(src) > -1)) addChild(src);
}
The idea is that if a Screen has not yet been drawn, it’s probably because it has not been added. The draw
function is designed with that in mind.
Other functions in the class look something like this:
public function addScreen(screen:Sprite):Void
{
Screens.push(screen);
draw();
}
public function removeScreen(screen:Sprite):Void
{
removeChild(screen);
Screens.splice(Screens.indexOf(screen), 1);
draw();
}
What I find odd is that when I remove the if (!(this.getChildIndex(src) > -1))
part from the draw
function, it works just fine and a Sprite with a filled rectangle coloured red appears as expected. But I don’t want to draw the same thing over and over again.
So my question is, what functions/methods should I be using to add sprites from an Array that do not currently exist in the children of the ScreenManager
?
Note: I am using OpenFL 2.0.1
Thanks for any help.