1 Reply Latest reply on May 29, 2007 5:39 AM by Greg Dove

# Random placement of objects on a specified / unique shaped area

Hi , Wondering if anyone can help. Basically I saw a humerous game where randomly placed pimples grew on a face, and the player had to squeeze them against a timer.

I'm trying to simulate a similar idea, but I can't work out how to make the objects appear only in the area of the face. sure, I can do it on a rectangle or square. In my mind I am thinking that I must use some kind of hit test function to check if it lands on the face shaped area.. But yeah my syntax is from another planet. Any ideas. Thanks. Probably stupendously easy.
• ###### 1. Re: Random placement of objects on a specified / unique shaped area
Hmmm, I'm not too sure. Probably just looping through random numbers with a hitTest with shapeFlag set to true would do it....but...

If I was doing this, my requirements might be that the "pimple" is also a certain minimum number of pixels from the edge. Because the face is a filled in shape with no gaps, then I would try to do a 'scan' of the two edges along one axis. Perhaps this can be done easier with bitMapData, but I'm not so familiar with that yet... here's how I would approach it:
loop through the pixel rows from 0 to _height of the face clip (or the part of the face clip that matters)
for each row, scan from left and right by pixel until the hitTest returns true.
push an object {xMin: leftXvalue, xMax:rightXvalue} onto a scan array that records the left and right pixel _x values for that row.

I now have an array (I would call it scan for example) that I can use to check the upper and lower x values that I can use as a range (to select a random x value from) after I have selected a random y value (which would correspond to the index in the scan array). With this x range you could also ensure that the 'pimple' was not too close to the edge of the face.

Doing this pixel level scan could be a little processor intensive at the start, especially if its a large image. There may be better/easier ways to do it with bitmapData.