Skip to main content
November 15, 2010
Answered

Is there a way to create a math.random that doesn't overlap movieclips?

  • November 15, 2010
  • 1 reply
  • 857 views

Hi,

Actually I have two questions. I am creating a memory sequence game and I was wondering:

1. Is there a way to random position movieclips on the stage without having them overlap each other?

2. Is there  a way to have flash draw a vector line from one clip to another in sequence and then have the user retrace what flash did? I have 6 movieclips on the stage, each with their own instance (ex. obj1_mc, obj2_mc, etc.). Is there a way to tell flash to always draw a line between movieclips in sequence even though the movieclips are randomly placed on the stage?

Thanks,

OJ

This topic has been closed for replies.
Correct answer kglad

sure.

1.  the easiest way to position objects without over-lapping, IF you can assume it will always be possible, is to use something like:

var mcA:Array=[your movieclips]

var index:uint=0;

positionF();

function positionF(){

// assuming top-left reg points of your movieclips

mcA[index].x=Math.random()*(stage.stageWidth-mcA[index].width);

mcA[index].y=Math.random()*(stage.stageHeight-mcA[index].height);

var hitBool:Boolean=false;

for(var i:int=0;i<index-1;i++){

if(mcA[index].hitTestObject(mcA)){

hitBool=true;

break;

}

}

if(hitBool){

positionF();

} else {

index++;

if(index<mcA.length){

positionF();

} else {

// positioning complete.  do whatever

}

}

}

2.  you can use the graphics class to draw dynamic lines.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
November 15, 2010

sure.

1.  the easiest way to position objects without over-lapping, IF you can assume it will always be possible, is to use something like:

var mcA:Array=[your movieclips]

var index:uint=0;

positionF();

function positionF(){

// assuming top-left reg points of your movieclips

mcA[index].x=Math.random()*(stage.stageWidth-mcA[index].width);

mcA[index].y=Math.random()*(stage.stageHeight-mcA[index].height);

var hitBool:Boolean=false;

for(var i:int=0;i<index-1;i++){

if(mcA[index].hitTestObject(mcA)){

hitBool=true;

break;

}

}

if(hitBool){

positionF();

} else {

index++;

if(index<mcA.length){

positionF();

} else {

// positioning complete.  do whatever

}

}

}

2.  you can use the graphics class to draw dynamic lines.

November 16, 2010

That worked. Thanks!

kglad
Community Expert
Community Expert
November 16, 2010

you're welcome.