6 Replies Latest reply on Feb 2, 2007 5:43 AM by mrsuperfineshag

# random movement in a non-rectangular boundary

I want a movie clip to randomly appear with a non-rectangular boundary. I've got random position changes within in a rectangular boundary working fine. Ideally i'd like to be able to draw a shape and use that as the boundary for the position changes.

Any ideas (i'm using flash 8)
• ###### 1. Re: random movement in a non-rectangular boundary
can you post your code for the random pos?
• ###### 2. Re: random movement in a non-rectangular boundary
I mean, it's possible to use hitTest...

• ###### 3. Re: random movement in a non-rectangular boundary
the code i'm using is below, the name of the clip i'm randomly distributing is called 'sparkle':
_parent.sparkle._visible = 0;
top = 570;
bottom = 800;
left = 0;
right = 260;
xRange = right-left;
yRange = bottom-top;
frames = _parent.sparkle._totalframes-1;

function duplication(obj, ink) {
_parent[obj].duplicateMovieClip(obj+ink, ink);
_parent[obj+ink]._y += random(yRange)+top;
_parent[obj+ink]._x += random(xRange)+left;
_parent[obj+ink].gotoAndPlay(random(frames));
}
for (ink=1; ink<_root.sparkles; ink += 2) {
duplication("sparkle", ink);
}
}
• ###### 4. Re: random movement in a non-rectangular boundary
OK. I've done a little experiment that can help you. If you want, I can send the FLA.
• ###### 5. Re: random movement in a non-rectangular boundary
If someone wants to see the FLA just ask to mrsuperfineshag or to me.
• ###### 6. Re: random movement in a non-rectangular boundary
heres the code to make it work - this version would go on the first frame of the timeline:

onEnterFrame = function () {
//move the object to a random place
_root.object._x = random(400);
_root.object._y = random(400);
//check to see if the object is within the target
if (target.hitTest(object._x, object._y, true) == false) {
//if it is not make it invisible and move it again
object._visible = false;
object._x = random(400);
object._y = random(400);
} else {
//if it is make it visible
object._visible = true;
}
};

to see a more complex version go here:
http://superfineshag.org/2007/02/02/moving-objects-with-a-non-square-boundary-in-flash/