Question

I have this foreach loop to check for collision and i want platform(movieclip) to be removed in case of collision. So far i've come up with this:

if (mcContent.mcPlayer.y + mcContent.mcPlayer.height > platformCloud.y) 
                                {
                                    mcContent.mcPlayer.y = platformCloud.y - mcContent.mcPlayer.height - 1;
                                    jump();
                                    mcContent.removeChild(platformCloud);
                                    //platformsCloud.splice(platformCloud);
                                }

What this is doing is, removing the movieclip (ok so far so good) but without the splice, when the loop runs again through the array it is still there. So with the splice that is commented out there's 1 little problem, it removes all the movieclips from the array, apprently.

How can i splice only the current index that is being checked?

Was it helpful?

Solution

.splice() accepts a start index and an amount of items to remove, not the object you want to remove from the array.

Parameters

startIndex:int — An integer that specifies the index of the element in the array where the insertion or deletion begins. You can use a negative integer to specify a position relative to the end of the array (for example, -1 is the last element of the array).

deleteCount:uint — An integer that specifies the number of elements to be deleted. This number includes the element specified in the startIndex parameter. If you do not specify a value for the deleteCount parameter, the method deletes all of the values from the startIndex element to the last element in the array. If the value is 0, no elements are deleted.

You want to do this:

var index:int = platformsCloud.indexOf(platformCloud);
platformsCloud.splice(index, 1);

OTHER TIPS

Why not just create a new array of the items to keep? Use Array.push to add new items. This may actually be more efficient than modifying the existing array. It also doesn't require keeping track of indices (which are required to use Array.splice).

Example code:

var keptPlatforms = [];
// do stuff
if (mcContent.mcPlayer.y + mcContent.mcPlayer.height > platformCloud.y) 
{
    mcContent.mcPlayer.y = platformCloud.y - mcContent.mcPlayer.height - 1;
    jump();
    mcContent.removeChild(platformCloud);
} else {
    keptPlatforms.push(platformCloud);
}
// later, after this cycle, use the new Array
platformClouds = keptPlatforms;

Now, the reason platformsCloud.splice(platformCloud) removes all items is because the first argument is coerced to an integer so it is equivalent to platformsCloud.splice(0) which says "remove the 0th-indexed item to the end of the array". And, this does indeed clear the array.

To use Array.splice, you'd have to do something like:

// inside a loop this approach may lead to O(n^2) performance
var i = platformClouds.indexOf(platformCloud);
if (i >= 0) {
    platformClouds.splice(i, 1); // remove 1 item at the i'th index
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top