Search code examples
arraysactionscript-3flashmovieclipflash-cs6

Remove a Movieclip from an Array


I'm very new to ActionScript 3.0, so be gentle. :P I'm making a simple 'custom gun' program where you can cycle through the different parts of the weapon to give it a unique appearance. For example, you have the 'barrel' and the magazine'. Each part is a movieclip with frames for the different options.

Parent Movie Clip: Barrel Frames within are labeled, each with an MC. Each MC in there has 4 layers labeled "Paint, Details, Metal, Light".

I have an array of the current parts:

var paintList:Array = new Array (Base.Paint, Bar.BarStandard.Paint, Mag.MagStandard.Paint /*, etc.*/);

Now what I need is to remove a specific piece from that array. So for the above example, how would I just remove the Bar.BarStandard.Paint and add instead Bar.BarExtended.Paint?

Hopefully this makes sense and someone can help! :C


Solution

  • When working with arrays and removing values from within arrays you use splice

    Using splice;

    arrayName.splice(indexValue, deleteCount)
    
    • arrayName you replace with the name of your array
    • indexValue you replace with the position that the value you want to remove from the array falls
    • deleteCount is the amount of values you want to delete

    Example with your code;

    var paintList:Array = new Array (Base.Paint, Bar.BarStandard.Paint, Mag.MagStandard.Paint, etc.);
    

    The value you want to replace is the 2nd value, therefore

    paintList.splice(2,1)
    

    And to add another value to that position.

    arrayName[indexNumber] = newValue;
    

    Again with your code;

    paintList[2] = Bar.BarExtended.Paint;