1

just for the record, i'm using AS3.

I have an issue where I would like to remove a sprite randomly in AS3, I have managed to figure out how to create the sprites so that they fill as a grid, just for the life of me I can't figure out how to remove them!

Here's the code i've used to create them:

function showpixels() : void
{   

    for (var i:int = 0; i < 40; i++)
    {
        for (var j:int = 0; j < 40; j++)
        {
            var s:Sprite = new Sprite();
            s.graphics.beginFill(0);
            s.graphics.drawRect(i*10, j*10, 10, 10);
            s.graphics.endFill();
            addChild(s);
            pixels.push(s);
        }
    }
}

Basically I need these to be removed randomly until what's underneath can be seen.

Any help would be good, I'm pretty new to this! Thanks!

Seb
  • 11
  • 2

2 Answers2

1
function removeRandom():void
{
    var rand:uint = Math.random()*pixels.length;

    var i:Sprite = Sprite(pixels[rand]);

    if(i.parent) i.parent.removeChild(i);
    pixels.splice(rand, 1);
}

UPDATE: To remove at random intervals you could try something like this:

var _timer:int = 100;

addEventListener(Event.ENTER_FRAME, _handle);
function _handle(e:Event):void
{
    if(pixels.length > 0) _timer --;
    if(_timer < 1)
    {
        _timer = 10 + Math.random()*50;
        removeRandom();
    }
}

function removeRandom():void
{
    var rand:uint = Math.random()*pixels.length;

    var i:Sprite = Sprite(pixels[rand]);

    if(i.parent) i.parent.removeChild(i);
    pixels.splice(rand, 1);
}
Marty
  • 39,033
  • 19
  • 93
  • 162
  • Hey Marty, thanks that's sort of what I tried and that is helpful! Basically I want them to all be removed at random intervals until the whole background is removed. I'll work on this thanks! – Seb May 09 '11 at 09:15
  • +1. It could be shortened to `removeChild(pixels.splice(Math.random()*pixels.length, 1)[0])` – back2dos May 09 '11 at 09:17
  • True, though I run into null object reference errors a lot when I don't wrap **removeChild** in **if(parent)** – Marty May 09 '11 at 09:23
  • How so? If the Sprite is in the `pixels`-array, then it does have a parent. It would rather make sense to check whether pixels is empty before attempting the rest of the routine, otherwise `i` will be `null` and `i.parent` will give you an error. – back2dos May 09 '11 at 09:29
  • Putting something into an array doesn't give it a parent.. Parent is the DisplayObjectContainer that a DisplayObject has been added to using addChild(). And using if(null) doesn't throw an error.. – Marty May 09 '11 at 09:47
  • Firstly, `if (null.parent)` will definitely give you an error, which is exactly what will happen if `pixels` is empty. Secondly, unless there is reason to assume, that the `DisplayObject` has been removed otherwise, its existence in the array is equivalent to being a child of the `DisplayObjectContainer` it has been added to. – back2dos May 10 '11 at 16:48
  • Sorry mate; I'm confused as to what you're trying to say. The Sprites that are added in the question are added to a container with addChild and I thought he wanted them removed from the array as well as removed visually. What are you trying to explain? – Marty May 10 '11 at 22:55
1

Marty's idea works. Another option would be to shuffle the array first and then just pop off elements.

To shuffle an Array use pixels.sort(function (...args):int { return int(2*Math.random()-1) }).

And then you can simple remove them like this:

function remove():void {
     if (pixels.length) removeChild(pixels.pop());
     else clearInterval(this.id);
}

And add this line at the end of showpixels:

this.id = setInterval(remove, 500);
BenMorel
  • 34,448
  • 50
  • 182
  • 322
back2dos
  • 15,588
  • 34
  • 50