AS3: Is it possible to add child in a specific area on the screen which is not a rectangle?

StackOverflow https://stackoverflow.com/questions/16022737

  •  04-04-2022
  •  | 
  •  

I need to add many children (addChild()) in a specific area. This area is not in a regular shape (figure). I noticed that if I want to add many children into my figure Flash creates rectangle that represent my figure and some of my children are going out of the figure but into this rectangle. I came to the idea to make many small rectangles that will cover my non regular figure and using arrays to distribute all the children into those small rectangles. Is this the right way? I will appreciate some ideas. Thanks

//----------------------------------------------------------------------------

                function randomRange(max:Number, min:Number = 0):Number
    {
        return Math.random() * (max - min) + min;
    }

    public function Main()
    {
        var bounds:Rectangle = Area_mc.getBounds(Area_mc.stage);
        var xIncr:int = randomRange(15,320);
        var yIncr:int = randomRange(15,220);
        for (var xPos=bounds.x; xPos <= bounds.x + bounds.width; xPos += xIncr)
        {
            for (var yPos=bounds.y; yPos <= bounds.y + bounds.height; yPos += yIncr)
            {
                var isInsideShape:Boolean = Area_mc.hitTestPoint(xPos,yPos,true);
                if (isInsideShape)
                {
                    //trace(isInsideShape);
                    stage.addChild(_symbol);
                    _symbol.x = xPos;
                    _symbol.y = yPos;
                }

            }
        }
    }    

Ok, I have random X and Y but the child always going on the right side of the container!:)

有帮助吗?

解决方案

I couldnt understand your exact requirement. From what i understood, if you are adding children on a movieclip with non-rectangular shape, you can use hitTestPoint() function of MovieClip.

For example, if you have 'child' movieclip that you intend to add on a non-rectangular 'parent' movieclip, you can use hitTestPoint to check if a point is within the shape of parent movieclip and then add it on that point.

Below code will add instances of 'Child' class which extends movieclip on 'parentMovieClip'. 'Child' is the linkage name of a movieclip in library whose instances you need to add on non-rectangular parent. 'parentMovieClip' is the instance name of the a movieclip which is on stage.

//storing bounds of parent that is added on stage
var bounds:Rectangle = parentMovieClip.getBounds(parentMovieClip.stage);

//these are the x and y gap you need between each child
var xIncr:int = 5;
var yIncr:int = 5;

//Traverse through the rectangular bound, and check what points actually comes within the shape
for(var xPos=bounds.x; xPos <= bounds.x+bounds.width; xPos += xIncr)
{
    for(var yPos=bounds.y; yPos <= bounds.y+bounds.height; yPos += yIncr)
    {
            //check if the point is inside the parent's shape
        var isInsideShape:Boolean = parentMovieClip.hitTestPoint(xPos,yPos,true);
        if(isInsideShape)
        {
            //if point is indise the shape add an instance of 'Child'
            var oChild:Child = new Child();
            //we are adding oChild on stage 
            //since adding on parentMovieClip will increase its bound if oChild goes outside parentMovieClip 
            stage.addChild(oChild);
            oChild.x = xPos;
            oChild.y = yPos;
        }
    }
}

I might be able to give you exact solution if you elaborate your requirement a bit more, by giving some code sample.

其他提示

No, it is the hard way.

Better

1) Define the area as a polygon.

2) Check the boundaries of new objects you want to add there via an algorithm which can detect if a point is inside a polygon or not.

Here is a good algorithm in C, which is short, brief, easy to convert to as3 and educative.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top