Skip to main content
Known Participant
December 5, 2009
Question

How to randomly place Dynamic Text fields created at authoring time.

  • December 5, 2009
  • 2 replies
  • 795 views

Dear Sir,

I have four dynamic text field created at the authoring time in Frame-4. I just need them to be placed randomly every time when I come from Frame3.

I have known that there is a Math Class which generates random numbers which would be an effective way.

Please suggest how do I proceed ahead i.e shall I create an array and seed it with the their current positions etc etc

Just guide me conceptually step by step and I will be indebted.

Thanks to all.

This topic has been closed for replies.

2 replies

Idaho Airships, Inc.
Known Participant
December 5, 2009

kanqana, I just completed an interface that was very similar to what you are apparently looking for.

You can distribute your TextFields randomly by calculating their x and y values randomly on the stage (e.g. Math.random() * 550 for x and Math.random() * 400 for y).

You can test to see whether two Display Objects are in a state of "collision" by using the TextField's "hitTestObject()" Method, which if "true" can be used to trigger resetting a TextField's position.

However, you'll need to run the hitTestObject() Method against each of the other TextFields. There is a logical trick to this that is covered very well in a book called Foundation ActionScript Making Things Move. In essence, you don't need to check textFieldA against textFieldB and check textFieldB against textFieldA. You only need to check each Object once against any other Object.

If you are using a flash.Motion Class to tween the objects into place it will still work fine with the collision testing.

Ned Murphy
Legend
December 5, 2009

Can you go into more detail regarding what you mean in terms of randomly placing them?  Do you mean there are specific placements you want to randomly assign to them or do you mean they would all be able to be randomly located anywhere on the stage (or within some area of the stage)?

Below is some code for the latter case, where the 4 textfields, with instance names tf1 thru tf4, are place anywhere on the stage, but relative to the stage boundaries.  You can adjust the min and max values to control the area they are restricted to.

stop();  // this is all frame 4 code

 

var minX:Number = 0;
var minY:Number = 0;
var maxX:Number = stage.stageWidth;
var maxY:Number = stage.stageHeight;

  

for(var i:int = 1; i<5; i++){
     this["tf"+i].x = minX + Math.random()*(maxX - minX);
     this["tf"+i].y = minY + Math.random()*(maxY - minY);
}

Known Participant
December 5, 2009

Dear Sir, Thanks a lot for giving me the codes.

I wish to again explain you about my requirements.

  • At the authoring time I have created the text fields i.e. they are already on the stage,
  • All I now want is that they should just exchange their places randomly i.e exchange their positions randomly or swap themselves
  • I have yet not tested your code but hope it does the same.

Thanks and I again look forward to your response.