Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9095727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:40:40+00:00 2026-06-16T23:40:40+00:00

I have this code that generates circles and makes them float within the boundaries

  • 0

I have this code that generates circles and makes them float within the boundaries of the stage. Although it stays in the stage it also has some give and let’s the circles push through a small amount which I like.

Is it possible to do this but with a custom shape and have the circles confined inside this shape?

Here is the code I have:

//number of balls
var numBalls:uint = 200;
var defaultBallSize:uint = 8;
var colors:Array = [0x79B718, 0x2D91A8, 0xB019BC, 0xF98715, 0xDB1616];

//init
makeDots();

function makeDots():void {
//create desired number of balls
for (var ballNum:uint=0; ballNum<numBalls; ballNum++){
var c1:Number = randomColor();
var c2:Number = randomColor();

//create ball
var thisBall:MovieClip = new MovieClip();
thisBall.graphics.beginFill(c1);
//thisBall.graphics.lineStyle(defaultBallSize, 0);
thisBall.graphics.drawCircle(defaultBallSize, defaultBallSize, defaultBallSize);
thisBall.graphics.endFill();

addChild(thisBall);

//coordinates
thisBall.x = Math.random() * stage.stageWidth;
thisBall.y = Math.random() * stage.stageHeight;
//percieved depth
thisBall.ballNum = ballNum;
thisBall.depth = ballNum/numBalls;
thisBall.scaleY = thisBall.scaleX = 
////thisBall.alpha = 
ballNum/numBalls;
//velocity
thisBall.vx = 0;
thisBall.vy = 0;
thisBall.vz = 0;

//ball animation
thisBall.addEventListener(Event.ENTER_FRAME, animateBall);
}
}

var dampen:Number = 0.90;
var maxScale:Number = 1.3;
var minScale:Number = .3;
var maxAlpha:Number = 1.3;
var minAlpha:Number = .3;
function animateBall(e:Event):void{
var thisBall:Object = e.target;
//apply randomness to velocity
thisBall.vx += Math.random() * 0.2 - 0.1;
thisBall.vy += Math.random() * 0.2 - 0.1;
thisBall.vz += Math.random() * 0.002 - 0.001;

thisBall.x += thisBall.vx;
thisBall.y += thisBall.vy;
//thisBall.scaleX = thisBall.scaleY += thisBall.vz;
//thisBall.alpha += thisBall.vz;
thisBall.vx *= dampen;
thisBall.vy *= dampen;
thisBall.vz *= dampen;

if(thisBall.x > stage.stageWidth) {
thisBall.x = 0 - thisBall.width;
}
else if(thisBall.x < 0 - thisBall.width) {
thisBall.x = stage.stageWidth;
}
if(thisBall.y > stage.stageHeight) {
thisBall.y = 0 - thisBall.height;
}
else if(thisBall.y < 0 - thisBall.height) {
thisBall.y = stage.stageHeight;
}

if (thisBall.scaleX > maxScale){
thisBall.scaleX = thisBall.scaleY = maxScale;
}
else if (thisBall.scaleX < minScale){
thisBall.scaleX = thisBall.scaleY = minScale;
}
if (thisBall.alpha > maxAlpha){
thisBall.alpha = maxAlpha;
}
else if (thisBall.alpha < minAlpha){
thisBall.alpha = minAlpha;
}
}

function randomColor():uint
{
return colors[int(Math.random()*colors.length)];
}

Code credit:

Originally from here: Circle Cube

Additional help here: Random colour within a list of pre-defined colours

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:40:41+00:00Added an answer on June 16, 2026 at 11:40 pm

    Yes, you can. What is happening is that when each circle moves, it is checked to see if it is within the stage bounds on the x and y axis and is ‘corrected’ if it goes out. You can modify that part of the code that determines this to check if a circle is within your custom shape or not.

    The complexity of this will depend on your custom shape as well as the method to go about detecting if a circle/object is within your custom shape.

    The ‘easiest custom shape’ you could try would be a rectangle or square, since the stage is already a big rectangle. To start this, look through your given code to find the lines of code that limit the x and y position of the stage dimensions and change them to the dimensions of your custom rectangle/square. You may have to add in position offsets if your custom shape rectangle/square does not originate from 0, 0 like the stage. I suggest factoring this part out (which is actually basic collision detection) into a method if you want to experiment with other shapes.


    Edit

    I edited my answer to include the original code reworked to use a random square as the custom shape -the easiest shape to try as mentioned in my original answer. Hopefully you can compare the two and see the changes I made to try and figure out the logic behind it.

    For a circle, or any other totally random shape, it would be a bit more difficult, but same idea/concept.

    //number of balls
    var numBalls:uint = 200;
    var defaultBallSize:uint = 8;
    var colors:Array = [0x79B718, 0x2D91A8, 0xB019BC, 0xF98715, 0xDB1616];
    
    // new custom shape bounds, a square that is 200, 200 px and is at 175, 100 on the stage
    var customSquare:Rectangle = new Rectangle(175, 100, 200, 200);
    
    //init
    makeDots();
    function makeDots():void {
        //create desired number of balls
        for (var ballNum:uint=0; ballNum < numBalls; ballNum++){
            var c1:Number = randomColor();
            var c2:Number = randomColor();
    
            //create ball
            var thisBall:MovieClip = new MovieClip();
            thisBall.graphics.beginFill(c1);
            //thisBall.graphics.lineStyle(defaultBallSize, 0);
            thisBall.graphics.drawCircle(defaultBallSize, defaultBallSize, defaultBallSize);
            thisBall.graphics.endFill();
    
            addChild(thisBall);
    
            //coordinates - this part of the code is setting the initial positions of the circles based on the stage size
            //thisBall.x = Math.random() * stage.stageWidth;
            //thisBall.y = Math.random() * stage.stageHeight;
            //
            // changed so they use the "customSquare" rectangle instead, note that the custom shape has an x and y pos now that doesn't start at 0 (unlike the stage)
            thisBall.x = (Math.random() * customSquare.width) + customSquare.x;
            thisBall.y = (Math.random() * customSquare.height) + customSquare.y;
    
            //percieved depth
            thisBall.ballNum = ballNum;
            thisBall.depth = ballNum / numBalls;
            thisBall.scaleY = thisBall.scaleX = ballNum / numBalls;
    
            //velocity
            thisBall.vx = 0;
            thisBall.vy = 0;
            thisBall.vz = 0;
    
            //ball animation
            thisBall.addEventListener(Event.ENTER_FRAME, animateBall);
        }
    }
    
    var dampen:Number = 0.90;
    var maxScale:Number = 1.3;
    var minScale:Number = .3;
    var maxAlpha:Number = 1.3;
    var minAlpha:Number = 0.3;
    
    function animateBall(e:Event):void{
        var thisBall:Object = e.target;
    
        //apply randomness to velocity
        /*thisBall.vx += Math.random() * 0.2 - 0.1;
        thisBall.vy += Math.random() * 0.2 - 0.1;
        thisBall.vz += Math.random() * 0.002 - 0.001;*/
        // increased velocity ranges to add more speed to see the effects easier
        thisBall.vx += Math.random() * 1.2 - 0.6;
        thisBall.vy += Math.random() * 1.2 - 0.6;
        thisBall.vz += Math.random() * 0.012 - 0.006;
    
        thisBall.x += thisBall.vx;
        thisBall.y += thisBall.vy;
        //thisBall.scaleX = thisBall.scaleY += thisBall.vz;
        //thisBall.alpha += thisBall.vz;
        thisBall.vx *= dampen;
        thisBall.vy *= dampen;
        thisBall.vz *= dampen;
    
        // =====================================================================================================================================
        // this part of the code is determining if each ball is going outside of the bounds of the stage and repositioning them if they are
        // this part is the 'collision detection', changed to use the bounds of the "customSquare" rectangle instead
        //
        // this part is detecting the position going out of bounds along the X axis
        /*if(thisBall.x > stage.stageWidth) {
            thisBall.x = 0 - thisBall.width;
        } else if(thisBall.x < 0 - thisBall.width) {
            thisBall.x = stage.stageWidth;
        }*/
        if(thisBall.x > (customSquare.width + customSquare.x)) {
            thisBall.x = customSquare.x - thisBall.width;
        } else if(thisBall.x < customSquare.x - thisBall.width) {
            thisBall.x = customSquare.width + customSquare.x;
        }
    
        // this part is detecting the position going out of bounds along the Y axis
        /*if(thisBall.y > stage.stageHeight) {
            thisBall.y = 0 - thisBall.height;
        } else if(thisBall.y < 0 - thisBall.height) {
            thisBall.y = stage.stageHeight;
        }*/
        if(thisBall.y > (customSquare.height + customSquare.y)) {
            thisBall.y = customSquare.y - thisBall.height;
        } else if(thisBall.y < customSquare.y - thisBall.height) {
            thisBall.y = customSquare.height + customSquare.y;
        }
        // =====================================================================================================================================
    
    
        if (thisBall.scaleX > maxScale){
            thisBall.scaleX = thisBall.scaleY = maxScale;
        } else if (thisBall.scaleX < minScale){
            thisBall.scaleX = thisBall.scaleY = minScale;
        }
    
        if (thisBall.alpha > maxAlpha){
            thisBall.alpha = maxAlpha;
        } else if (thisBall.alpha < minAlpha){
            thisBall.alpha = minAlpha;
        }
    }
    
    function randomColor():uint{    return colors[int(Math.random()*colors.length)];    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that generates threads (lizards) it also makes a cat thread.
I have this code (an example that PHP generates for me)... <select id=outsideBlue3> <option
I have code that generates this error: ** Message: pygobject_register_sinkfunc is deprecated (GstObject) Traceback
I have some LINQ code that generates a list of strings, like this: var
I have some C# code that generates google maps. This codes looks at all
I have this code snippets that generates a signature for POSTs. The detail of
I have this code that does the following: -generates a linked list that contains
I have this code that generates a WP menu: <div id=menu> <?php $args =
I have this code that generates a group ID with javascript. It's shown here:
So right now I have this code that generates random letters in set increments

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.