Skip to main content
Known Participant
October 3, 2012
Question

Place a Movieclip at random x and y in a Range

  • October 3, 2012
  • 3 replies
  • 3784 views

i want to place a library item multiple times inside a movie clip over the top of another movieclip.  The graphic in the other Movieclip is around object and I need the library item to appear insdie the rounded object and not ontop of each other.  The round object is 240 wide and 228 high.  I have most of this working except to make sure it doesnt go over the edges and tthe overlapping.  Does anyone know how to place an item with in a range?  I also need to be able to address the placed items in another function to cause them to play.

CODE:

var cutAway:MovieClip = new MovieClip();

var halfTomato:MovieClip = new MovieClip();

var bubblesClip:MovieClip = new MovieClip();

var bubbleArray:Array = new Array();

function createCutAway():void {

          var tomatoCut = new tomatoCutaway();

          var bubbleNo:Number = 9;

          var nScale:Number;

          trace(bubbleNo);

          for (var a:int = 0; a<bubbleNo; a++) {

                    var bubbles = new bubble();

                    bubbles.x = (Math.random()*(tomatoCut.width-10));

                    bubbles.y = (Math.random()*(tomatoCut.height-10));

                    trace(bubbles.x,

                                bubbles.y,

                                tomatoCut.width,

                                tomatoCut.height

                                );

                    nScale = (Math.random()*3)+1;

                    bubbles.scaleX = nScale;

                    bubbles.scaleY = nScale;

                    bubbleArray.push(bubbles);

                    bubblesClip.addChild(bubbles);

          }

          halfTomato.addChild(tomatoCut);

          halfTomato.width = 240.1;

          halfTomato.height = 228.2;

          cutAway.addChild(halfTomato);

          cutAway.addChild(bubblesClip);

          addChild(cutAway);

          cutAway.x = 134;

          cutAway.y = 158;

}

This topic has been closed for replies.

3 replies

Inspiring
October 6, 2012

And this code in addition to what previous does animates bubbles within boundaries. Uses the same principal of BitmapData.hitTest().

stop();

import flash.display.BitmapData;

import flash.display.DisplayObject;

import flash.display.Graphics;

import flash.display.MovieClip;

import flash.display.Sprite;

import flash.events.Event;

import flash.geom.Point;

var containment:Sprite;

var containmentBMD:BitmapData;

var containmentPoint:Point;

// object parameters used repeatedly

// object.x

var ox:Number;

// object.y

var oy:Number;

// object.x + object.width

var sx:Number;

// object.y + object.height

var sy:Number;

// collection of bubbles

var bubbles:Vector.<MovieClip>;

// bubble speed

var speed:int = 2;

// top left corner test

var tlc:Boolean;

// top right corner test

var trc:Boolean;

// bottom left corner test

var blc:Boolean;

// bottom right corner test

var brc:Boolean;

init();

/**

* Performs initial object instantiation.

*/

function init():void

{

    makeCircle();

    makeBubbles();

    placeBubbles();

    startAnimation();

}

function startAnimation():void

{

    addEventListener(Event.ENTER_FRAME, moveBubbles);

}

/**

* Animates bubbles within boundaries of containment object.

*

* @param    e

*/

function moveBubbles(e:Event):void

{

    for each (var b:MovieClip in bubbles)

    {

        if (!objectTest(b))

        {

            // get hit tests for all corners

            tlc = pointTest(new Point(b.x, b.y));

            trc = pointTest(new Point(b.x + b.width, b.y));

            blc = pointTest(new Point(b.x, b.y + b.height));

            brc = pointTest(new Point(b.x + b.width, b.y + b.height));

            // move it back one step to remedy some jerkyness on over egde displays

            b.x -= speed * b.dx;

            b.y -= speed * b.dy;

            // if only top left corner is out of boundaries

            if (!tlc && trc && brc && blc)

            {

                b.dx = flipInt;

                b.dy = b.dx == -1 ? -1 : flipInt;

            }

            // if top left and right coreners are out boundaries

            else if (!tlc && !trc && brc && blc)

            {

                b.dx *= -1;

                b.dy = 1;

            }

            // if only top right corener is outof boundaries

            else if (tlc && !trc && brc && blc)

            {

                b.dx = flipInt;

                b.dy = b.dx == 1 ? 1 : flipInt;

            }

            // if top and bottom roght corners are out of boundaries

            else if (tlc && !trc && !brc && blc)

            {

                b.dx = -1;

                b.dy = flipInt;

            }

            // if only bottom right corener is out of boundaries

            else if (tlc && trc && !brc && blc)

            {

                b.dx = flipInt;

                b.dy = b.dx == 1 ? -1 : flipInt;

            }

            // if both bottom right and left coreners are out of boundaries

            else if (tlc && trc && !brc && !blc)

            {

                b.dx = flipInt;

                b.dy = -1;

            }

            // if only bottom left corner is out of boundaries

            else if (tlc && trc && brc && !blc)

            {

                b.dx = flipInt;

                b.dy = b.dx == 1 ? flipInt : -1;

            }

            // if both top and bottom left corners are out boundaries

            else if (!tlc && trc && brc && !blc)

            {

                b.dx = 1;

                b.dy = flipInt;

            }

        }

        b.x += b.dx * speed;

        b.y += b.dy * speed;

    }

}

/**

* Generates 1 or -1

*/

function get flipInt():int

{

    return Math.random() > .5 ? 1 : -1;

}

/**

* Places bubbles on display list and positions them within containment object boundaries.

*/

function placeBubbles():void

{

    // containment object boundaries

    for each (var b:MovieClip in bubbles)

    {

        addChild(b);

        while (!objectTest(b))

        {

            b.x = int(containment.x + Math.random() * containment.width);

            b.y = int(containment.y + Math.random() * containment.height);

        }

    }

}

/**

* Creates bubbles.

*/

function makeBubbles():void

{

    var numBubbles:int = 20;

    bubbles = new Vector.<MovieClip>();

    while (numBubbles--)

    {

        bubbles.push(bubbleSprite);

    }

}

/**

* Creates a single bubble instance.

*/

function get bubbleSprite():MovieClip

{

    var b:MovieClip = new MovieClip();

    var g:Graphics = b.graphics;

    // minimum radius 10, max radius 30

    var r:int = 10 + Math.random() * 20;

    g.lineStyle(1, 0x808080, .8);

    g.beginFill(0x004080, .4);

    g.drawCircle(r, r, r);

    g.endFill();

    // assign movement directions randomly

    b.dx = flipInt;

    b.dy = flipInt;

    return b;

}

/**

* Creates containment within wich bubbles are positioned.

*/

function makeCircle():void

{

    containment = new Sprite();

    containment.graphics.beginFill(0xCCFFCC);

    containment.graphics.drawEllipse(0, 0, 600, 400);

    containment.x = (stage.stageWidth - containment.width) * .5;

    containment.y = (stage.stageHeight - containment.height) * .5;

    addChild(containment);

    containmentPoint = new Point(containment.x, containment.y);

    containmentBMD = new BitmapData(containment.width, containment.height, true, 0);

    containmentBMD.draw(containment);

}

/**

* Tests 4 objects boundaries points against containment data

* @param    object

* @return

*/

function objectTest(object:DisplayObject):Boolean

{

    ox = object.x;

    oy = object.y;

    sx = ox + object.width;

    sy = oy + object.height;

    return pointTest(new Point(ox, oy)) && pointTest(new Point(sx, oy)) && pointTest(new Point(ox, sy)) && pointTest(new Point(sx, sy));

}

/**

* Tests point against BitmapData

* @param    p

* @return

*/

function pointTest(p:Point):Boolean

{

    return containmentBMD.hitTest(containmentPoint, 255, p)

}

Known Participant
October 23, 2012

OK I am still having trouble with this.  I have got some of it working but the placement is still a problem.  Anyone seee what is wrong with tihs code?

CODE:

//Cutaway tomato empty MovieClips Frame 12

var cutAway:MovieClip = new MovieClip();

var halfTomato:MovieClip = new MovieClip();

var bubblesClip:MovieClip = new MovieClip();

var bubbleArray:Array = new Array();

var tomatoCut = new tomatoCutaway();

var containment:Sprite;

var containmentBMD:BitmapData;

var containmentPoint:Point;

var nScale:Number;

var bubbles = new bubble();

createCutAway();

function createCutAway():void

          //Add tomatoCutAway to MovieClip (halfTomato);

          halfTomato.addChild(tomatoCut);

          //set halfTomato MovieClip width and height;

          halfTomato.width = 240.1;

          halfTomato.height = 228.2;

          //Bubble Containment Field

          containmentPoint = new Point(halfTomato.x,halfTomato.y);

          containmentBMD = new BitmapData(halfTomato.width,halfTomato.height,true,0);

          trace(containmentPoint, containmentBMD);

          //Add halfTomato MovieClip & Bubbles to CutAway MovieClip

          cutAway.addChild(halfTomato);

          cutAway.addChild(bubblesClip);

          //Set scale cutAway MovieClip;

          cutAway.scaleX = .75;

          cutAway.scaleY = .75;

          //Add cutAway MovieClip to stage

          addChild(cutAway);

          //set cutAway on the stage x and y

          cutAway.x = 400;

          cutAway.y = 158;

          makeBubbles();

  placeBubbles();

}

function makeBubbles():void

{

          var numBubbles:int = 9;

          var bubbles = new bubble();

          var i:int = numBubbles;

          while (i--)

          {

                    trace(i);

                    bubbleArray.push(bubbles);

          }

          trace(bubbleArray);

}

function placeBubbles():void

{

          for (var d = 0; d<bubbleArray.length; d++)

          {

                    nScale = (Math.random()*3)+1;

                    bubbles.scaleX = nScale;

                    bubbles.scaleY = nScale;

                    bubblesClip.addChild(bubbles);

                    while (!objectTest(bubbles))

                    {

                              bubbles.x = int(halfTomato.x + Math.random() * halfTomato.width);

                              bubbles.y = int(halfTomato.y + Math.random() * halfTomato.height);

                    }

          }

}

// object parameters used repeatedly

// object.x

var ox:Number;

// object.y

var oy:Number;

// object.x + object.width

var sx:Number;

// object.y + object.height

var sy:Number;

function objectTest(object:DisplayObject):Boolean

{

          ox = 400;

          oy = 158;

          sx = ox + 240;

          sy = oy + 228;

          return pointTest(new Point(ox, oy)) && pointTest(new Point(sx, oy)) && pointTest(new Point(ox, sy)) && pointTest(new Point(sx, sy));

}

function pointTest(p:Point):Boolean

{

          return containmentBMD.hitTest(containmentPoint, 255, p);

}

Inspiring
October 23, 2012

Did you attempt to use my code as is?

Inspiring
October 6, 2012

In order to accomplish your task the best way is to use BitmapData.hitTest() method.

Code below is creating a containment object (ellipse in this case) and places bubbles within its boundaries.

For your purposes replace containment object with tomato and bubbles with your bubbles. But first just place the code on a timeline of an empty FLA and observe what is going on.

Read comments.


stop();

import flash.display.Bitmap;

import flash.display.BitmapData;

import flash.display.DisplayObject;

import flash.display.Graphics;

import flash.display.Shape;

import flash.display.Sprite;

import flash.geom.Point;

import flash.geom.Rectangle;

var containment:Sprite;

var containmentBMD:BitmapData;

var containmentPoint:Point;

// object parameters used repeatedly

// object.x

var ox:Number;

// object.y

var oy:Number;

// object.x + object.width

var sx:Number;

// object.y + object.height

var sy:Number;

// collection of bubbles

var bubbles:Vector.<Sprite>;

init();

/**

* Performs initial object instantiation.

*/

function init():void

{

    makeContainment();

    makeBubbles();

    placeBubbles();

}

/**

* Places bubbles on display list and positions them within containment object boundaries.

*/

function placeBubbles():void

{

    for each (var b:Sprite in bubbles)

    {

        addChild(b);

        // keep changing coordinates until bubble is within containment boundaries

        while (!objectTest(b))

        {

            b.x = int(containment.x + Math.random() * containment.width);

            b.y = int(containment.y + Math.random() * containment.height);

        }

    }

}

/**

* Creates bubbles.

*/

function makeBubbles():void

{

    var numBubbles:int = 10;

    bubbles = new Vector.<Sprite>();

    var i:int = numBubbles;

    while (i--)

    {

        bubbles.push(bubbleSprite);

    }

}

/**

* Creates a single bubble instance.

*/

function get bubbleSprite():Sprite

{

    var b:Sprite = new Sprite();

    var g:Graphics = b.graphics;

    // minimum radius 10, max radius 30

    var r:int = 10 + Math.random() * 20;

    g.lineStyle(1, 0x808080, .8);

    g.beginFill(0x004080, .4);

    g.drawCircle(r, r, r);

    g.endFill();

   

    return b;

}

/**

* Creates containment within wich bubbles are positioned.

*/

function makeContainment():void

{

    containment = new Sprite();

    containment.graphics.beginFill(0xCCFFCC);

    containment.graphics.drawEllipse(0, 0, 600, 400);

    containment.x = (stage.stageWidth - containment.width) * .5;

    containment.y = (stage.stageHeight - containment.height) * .5;

    addChild(containment);

    containmentPoint = new Point(containment.x, containment.y);

    containmentBMD = new BitmapData(containment.width, containment.height, true, 0);

    containmentBMD.draw(containment);

}

/**

* Tests 4 object's boundaries points against containment data.

*

* @param    object - any DisplayObject can be used

* @return

*/

function objectTest(object:DisplayObject):Boolean

{

    ox = object.x;

    oy = object.y;

    sx = ox + object.width;

    sy = oy + object.height;

    return pointTest(new Point(ox, oy)) && pointTest(new Point(sx, oy)) && pointTest(new Point(ox, sy)) && pointTest(new Point(sx, sy));

}

/**

* Tests point against BitmapData.

*

* @param    p - Point instance that is chekced agains BitmapData

* @return

*/

function pointTest(p:Point):Boolean

{

    return containmentBMD.hitTest(containmentPoint, 255, p)

}

Ned Murphy
Legend
October 3, 2012

To determine a random value within a range you start with the lower end of that range and add a random value based on the maximum-minimum of that range.

Example: If you want  a random value between 10 and 100, then you start with 10 and and a random value between 0 to 90 (100 - 10)

Known Participant
October 3, 2012

I am getting this when I run the code.... the bubble EZ are supposed to be not outside the tomato part and not overlapping.  I am sure of how to accomplish this.

Ned Murphy
Legend
October 3, 2012

Those are probably all positioned within the limits you specify.  But you didn't take into consideration the outer corners where those limits still apply but the tomato doesn't exist.  You could try changing your limits to be some rectangle(s) within the tomato.