A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Random Position

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    5

    Random Position

    I have an animated movie clip that I would like to have loop on the main stage, but each time it plays, it appears in a random location. Also, I need to constrain it to the dimensions of the stage.

    I'm really quite new to this, so I'm sure this is a simple thing to do, but I'm completely lost and in need of help. Thanks in advance!

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    AS2...code on the main timeline.
    Code:
    function randMove(myMC) {
    	var myLim = myMC._totalframes;
    	var maxX = Stage.width-myMC._width;
    	var maxY = Stage.height-myMC._height;
    	myMC.onEnterFrame = function() {
    		if (this._currentframe == myLim) {
    			myMC._x = Math.floor(Math.random()*maxX);
    			myMC._y = Math.floor(Math.random()*maxY);
    		}
    	};
    }
    randMove(animatedmovieclipinstancename);

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    5
    That's starting to do the trick!

    It's now moving randomly throughout the movie clip - but there's two small hiccups:

    1 - it is sometimes cut-off, extending beyond the stage width and/or height
    2 - it only appears to be moving up and down the left side of the stage - it never goes to the right side.

    Do I need to change some of the code to fix this?

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    1. Code needs to be changed if registration of movie clip is not in upper left.
    2. Did you re-type it? (Look for typos in your code).

  5. #5
    Junior Member
    Join Date
    Apr 2009
    Posts
    5
    I found out the source of the problem. One element of the movie clip wasn't aligned like the others, which was causing the clipping on the left side.

    Works perfectly, now. Thank you very much dawsonk!

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