Search code examples
classactionscript-3flashgraphics

remove graphics from inside a class as3


When I click a button in my game it draws shapes using the graphics in as3. simple shapes such as circles and rectangles.

I want to remove the graphics that have been drawn when something happens in one of my classes.

Basically when there is a hitTestObject (which works fine) I want all graphics on stage to be cleared.

if (gb2.hitTestObject(h1s2))
            {
                trace ("holed")
                ySpeed2=0;
                xSpeed2=0;
                this.visible=false;


                var mcSplash:MovieClip =parent.getChildByName("mcSplash") as MovieClip;
                mcSplash.visible=true;
                //parent.drawings.graphics.clear();
            }

My attempt using parent.drawings.graphics.clear(); was unsuccessful, it gives me this error:

Line 481 1119: Access of possibly undefined property drawings through a reference with static type flash.display:DisplayObjectContainer.

Anyone have any suggestions

UPDATE:

this is how, on the min time line, the drawings occur.

var drawings:Shape = new Shape;

for (i=0; i<numRecs; i++) 


{

    recStartX = Number(xmlContent.rec[i].startpoint.@ptx);

    recStartY = Number(xmlContent.rec[i].startpoint.@pty);

    recWidth = Number(xmlContent.rec[i].dimensions.@w);

    recHeight = Number(xmlContent.rec[i].dimensions.@h);

    fillColor=int(xmlContent.rec[i].look.fillhex);

    lineThick = Number(xmlContent.rec[i].look.strokethick);

    lineColor = int(xmlContent.rec[i].look.strokehex);

    drawings.graphics.lineStyle(lineThick, lineColor);

    drawings.graphics.beginFill(fillColor);

    drawings.graphics.drawRect(recStartX,recStartY,recWidth,recHeight);

    drawings.graphics.endFill();

}

Solution

  • Create an array and push in each shape/rect. Then iterate through this and remove..

    for(var iteration:int = 0; iteration < rectArray.length; iteration++)
        this.removeChild(rectArray[iteration]);
    

    or if you are calling this from a class, use

    MovieClip(this.root).removeChild(rectArray[iteration]);
    

    Hopefully this is helpful :) Z