Fork me on GitHub
Not signed in (Sign In)

Welcome, Guest

Want to take part in these discussions? Sign in if you have an account, or apply for one below

    • CommentAuthorrrh
    • CommentTimeJun 23rd 2010 edited
     
    Why isn't this removing all the initializers?

    var tempList:Array = emit.initializers;
    for (var i:int=0;i<tempList.length;i++) {
    emit.removeInitializer(Initializer(tempList[i]));
    }

    trace(emit.initializers.length);


    That trace at the end keeps tracing 3 or 4, but if it's removing the initializers, I'd expect it to be 0. Does removeInitializer not remove the intializer from the linitializers array?
    • CommentAuthorrrh
    • CommentTimeJun 24th 2010 edited
     
    For context, the purpose of this is I'm doing something where I need to change the initializer properties a lot, and if I keep going:

    emit.addInitializer( new ScaleImageInit( scale*0.25, scale ) );
    emit.addInitializer( new ColorInit( colour ) );


    What happens to the old initializers? I figured, hey, the easy solution is to just remove the old initializers before I add the new ones, but whatever I do, I want to be sure I'm not creating a memory leak.
    • CommentAuthorRichard
    • CommentTimeJul 1st 2010
     
    Because tempList is not a copy of the initializers array, it is the initializers array. As a result, tempList.length reduces by one every time around the loop and you miss some of the initializers.

    There are a number of other ways you could do this that would work. Here's three of them...

    var tempList : Array = emit.initializers.slice();
    for ( var i : int = 0; i < tempList.length; i++ )
    {
    emit.removeInitializer( Initializer( tempList[i] ) );
    }


    for ( var i : int = emit.initializers.length - 1; i >= 0; i-- )
    {
    emit.removeInitializer( Initializer( emit.initializers[i] ) );
    }


    while ( emit.initializers.length > 0 )
    {
    emit.removeInitializer( Initializer( emit.initializers[0] ) );
    }
    • CommentAuthorXor
    • CommentTimeJul 5th 2010
     
    If you don't need to actually remove or add new initializers, then you can also save reference to the initializer and then directly change the values on it as you go. This way you don't need to recreate it each time the values change.

    var colorInit :ColorInit = new ColorInit( colour );
    emit.addInitializer( colorInit );

    // later
    colorInit.minColor = 0xFFFFFF;