Skip to main content
Participating Frequently
February 1, 2007
Question

random movement in a non-rectangular boundary

  • February 1, 2007
  • 6 replies
  • 442 views
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 😎
This topic has been closed for replies.

6 replies

Participating Frequently
February 2, 2007
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/
Inspiring
February 1, 2007
If someone wants to see the FLA just ask to mrsuperfineshag or to me.
Inspiring
February 1, 2007
OK. I've done a little experiment that can help you. If you want, I can send the FLA.
Participating Frequently
February 1, 2007
the code i'm using is below, the name of the clip i'm randomly distributing is called 'sparkle':
onClipEvent (load) {
_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);
}
}
Inspiring
February 1, 2007
I mean, it's possible to use hitTest...

Inspiring
February 1, 2007
can you post your code for the random pos?