0

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!:)

irnik
  • 139
  • 1
  • 2
  • 13

2 Answers2

1

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.

shinobi
  • 2,511
  • 1
  • 19
  • 27
  • Thank you so much sohel. I "translated" this code :) and yes I got your point. I am a bit concern about this: "var bounds:Rectangle = parentMovieClip.getBounds(parentMovieClip.stage);" This is still rectangle?? – irnik Apr 15 '13 at 21:24
  • I mean will your loop check insight the rectangle's coordinates? Or getBounds command precisely has taken the boundaries of the container (I will call the parent movie clip a container)? Thanks – irnik Apr 15 '13 at 21:28
  • And also isn't "hitTestPoint" using rectangle boundaries ? – irnik Apr 15 '13 at 21:40
  • Yes getBounds() will give you a rectangle, but what I am doing here is, I traverse through all the points in that rectangular and check what points actually come inside the non-rectangular shape by using hitTestPoint(). And if the point is inside it we add the child. – shinobi Apr 16 '13 at 05:44
  • One more thing, hitTestPoint() is NOT using rectangle boundaries, it just takes a point(x,y) as an input and checks if the point lies within the hit area of that movieclip. Any empty space in the rectangular bounds of a non-rectangular movieclip will give hitTestPoint as false. With this in mind have a look the code once again, hope you will get my point – shinobi Apr 16 '13 at 05:48
  • Hi sohel, I didn't know how/where to add my code here so I added it in my question. I think this works but I will properly test it if I use randomized x and y to add many children. How can I do that? – irnik Apr 16 '13 at 07:36
  • I have random X and Y but the child always going on the right side of the container!:) – irnik Apr 16 '13 at 07:46
  • Hi irnik. Yes you can test it with random values. The random values that you generate will be between 'bounds.x' & 'bounds.x+bounds.width' for xPos and between 'bounds.y' & 'bounds.y+bounds.height' for yPos. Then send this xPos and yPos to hitTestPoint, if it is true then add your _symbol, else dont. – shinobi Apr 16 '13 at 08:00
  • thanks sohel. What about the quantity of the children. any ideas how to add 10 more each in different random position? – irnik Apr 16 '13 at 08:04
  • Divide your shape in four different regions and generate random values for those regions separately, that might help you. For eg first generate 10 random values in range 'bounds.x' & 'bounds.x+(bounds.width)/2' for xPos and between 'bounds.y' & 'bounds.y+(bounds.height)/2' for yPos and so on. – shinobi Apr 16 '13 at 08:39
0

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.

Volkan
  • 2,212
  • 1
  • 14
  • 14