A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Randomly Moving Objects

  1. #1

    Randomly Moving Objects

    I need assistance with this, since I can't find it in the forums. I want to have at most 10 circular-objects moving randomly around the page. Also if it's possible to have some of them appear and disappear after time. Can this be done?

    thanks!

  2. #2
    Senior Member
    Join Date
    May 2004
    Posts
    182
    i would use Math.Random for that. what is the concrete problem?

  3. #3
    I have no idea how to approach this with code. Is there an code-example of randomly moving objects that I can follow??

  4. #4
    I have no idea how to approach this with code. Is there an code-example of randomly moving objects that I can follow??

  5. #5
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    One of the problems with your question is that 'random motion' can mean almost anything. I've answered a few similar queries in the past with a script like the following, and almost always, I get back a reply saying 'that's great, but can you make it move *this* way?'

    So you need to find the words to more accurately describe the kind of motion you want.

    In the following example, I'm moving the circles in smooth arcs. The motion paths are similar to spyrographs, and are computed using two sin/cos waves.

    I created a new movie. I drew a circle, and converted it to a movieclip symbol. I named the instance of the symbol circ0.

    Then I wrote the following script to multiply circ0 to 10 duplicate movies, and setup the movement and fading.

    code:

    CX = Stage.width/2;
    CY = Stage.height/2;

    for (var i = 1; i < 10; ++i)
    {
    circ0.duplicateMovieClip("circ"+i,i);
    }

    InitMe = function(mc)
    {
    mc.arc1 = Math.random()*2*Math.PI;
    mc.arc2 = Math.random()*2*Math.PI;
    mc.speed1 = Math.random()*1-.5;
    mc.speed2 = Math.random()*1-.5;
    mc.r1 = Math.random()*CY;
    mc.r2 = Math.random()*CY;
    mc.timeToFade = getTimer() + Math.random()*10000;
    mc.onEnterFrame = MoveMe;
    }

    MoveMe = function()
    {
    this.arc1 += this.speed1;
    this.arc2 += this.speed2;
    this._x = CX + Math.cos(this.arc1)*this.r1 + Math.cos(this.arc2)*this.r2;
    this._y = CY + Math.sin(this.arc1)*this.r1 + Math.sin(this.arc2)*this.r2;
    if (getTimer() > this.timeToFade) {
    if (this._alpha > 0) {
    this._alpha -= 5;
    if (this._alpha <= 0)
    {
    trace('resetting');
    InitMe(this);
    }
    }
    }
    else {
    if (this._alpha < 100)
    this._alpha += 5;
    }
    }

    for (var i = 0; i < 10; ++i) {
    mc = _root["circ"+i];
    InitMe(mc);
    }




    I've attached a sample project which uses it.
    Attached Files Attached Files
    Last edited by jbum; 06-16-2004 at 04:55 PM.

  6. #6
    Thanks, that should give me a good start. What I meant by moving randomnly, each object's path id different causing it to look like they are wandering around the screen from different start and ending points. Has this been done?

  7. #7
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Yes, but they are not moving in straight lines. Take a look at the .fla to see the effect.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center