A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: Random MovieClip Creator

  1. #1
    Senior Member SpikeyOmega's Avatar
    Join Date
    Dec 2004
    Posts
    400

    Random MovieClip Creator

    Hello, I am making a doger game. And I can do everything from making the speed vars to doing the hitTests but of course, the most important thing I donno how to do. How do I make the flash movie create a movieclip in random (random X, same Y (top of screen)), and then have them fall at the random speed?

  2. #2
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    For that task all you need is creating random numbers.
    code:

    var rand = Math.floor(Math.random() * n);
    // creates a random number between 0 and n-1

    var rand = Math.floor(Math.random() * n + 1);
    // creates a random number between 1 and n
    // and so on...



    So, to place a movie clip on random coordinates, you could attach it from the library:
    code:

    var i = 1;
    //
    var randX = Math.floor(Math.random() * 200);
    var randY = Math.floor(Math.random() * 100);
    //
    var new_mc = this.attachMovie("my_mc", "my_mc" + i, i);
    new_mc._x = randX;
    new_mc._y = randY;
    //
    i++;



    For the speed would be the same...

  3. #3
    Senior Member SpikeyOmega's Avatar
    Join Date
    Dec 2004
    Posts
    400
    As I said, I can do all that, but what I need is to know how to make it fall at "speed". And, they need to be made at different times, possible with timeouts?

  4. #4
    Re Member websam's Avatar
    Join Date
    Jul 2000
    Location
    Australia (VIC)
    Posts
    660
    Try this:
    Code:
    // =====================================
    //    customizable variables
    // =====================================
    var objNum = 20;	// number of objects
    var initY = 0;	// initial y
    var destY = 400;	// destination y
    var aWidth = 550;	// area width
    // =====================================
    //   END customizable variables
    // =====================================
    
    // use negative value if you want it going up but
    // don't forget to reverse the initY and destY accordingly
    var maxSpeed = 5; // number of pixel per move
    var minSpeed = 1;
    
    for(i=1;i<=objNum;i++){
       // create holder object
       var obj = _root.createEmptyMovieClip("obj"+i,i+10);
       // visualize it by drawing a square
       obj.beginFill(Math.random()*0xFF6600, 80);
       obj.lineStyle(1, 0x333333, 100);
       obj.moveTo(0, 0);
       obj.lineTo(10, 0);
       obj.lineTo(10,10);
       obj.lineTo(0, 10);
       obj.lineTo(0, 0);
       obj.endFill();
    	
       //get randomized x
       obj._x = Math.random()*aWidth;
       obj._y = initY;
       // randomize moving speed
       obj.speed = Math.random()*(maxSpeed - minSpeed) + minSpeed;
       // assign onEnterFrame event handler
       obj.onEnterFrame=function(){
           if(this._y < destY){
              // if not reach pre-defined destY
              this._y += this.speed;
           }else{
              // if reach pre-defined destY
              this._y = destY;
              // delete onEnterFrame event handler
              delete this.onEnterFrame;
           }
       }
    }

  5. #5
    Senior Member SpikeyOmega's Avatar
    Join Date
    Dec 2004
    Posts
    400

    HOLY CRAP!

    Holy f***** crap! I wish I understood that script but that is way beyond me. I think it works so thanks but. Can you do me one other favor? Can you explain what
    Code:
    i
    does? I never get that.
    Edit: Nvm what I said b4. I am not sure where to add the correct MC in that script, and then where do I put the MC's instance name? For example I will need to do hitTests so yeah. Thank you so much for your help btw. (And dont try that script with 20000 objects, it kills ur comp.
    Last edited by SpikeyOmega; 04-17-2005 at 05:36 PM.

  6. #6
    Re Member websam's Avatar
    Join Date
    Jul 2000
    Location
    Australia (VIC)
    Posts
    660
    You need to change some code within the FOR loop. Instead of creating empty object to draw color boxes, put your attachMovie() code there with sequentially indexed objects:

    REPLACE this:
    Code:
       // create holder object
       var obj = _root.createEmptyMovieClip("obj"+i,i+10);
       // visualize it by drawing a square
       obj.beginFill(Math.random()*0xFF6600, 80);
       obj.lineStyle(1, 0x333333, 100);
       obj.moveTo(0, 0);
       obj.lineTo(10, 0);
       obj.lineTo(10,10);
       obj.lineTo(0, 10);
       obj.lineTo(0, 0);
       obj.endFill();
    with something like:
    Code:
       var obj = _root.attachMovie("my_mc", "my_mc" + i, i+10);
    and make your "my_mc" with proper linkgage in your library.

    Here is all the steps:
    1. create a new FLA
    2. create my_mc movie clip of your own, specify linkgage "my_mc"
    3. copy and paste the code with above mentioned update to frame 1
    4. test run it and you should see your "my_mc" falling down in random speed

  7. #7
    Senior Member SpikeyOmega's Avatar
    Join Date
    Dec 2004
    Posts
    400
    Hello, I have two more questions. I have turned that script into a function and now, when I call it, it makes the cube but doesn't allow it to fall. Here is the script:
    Code:
    stop();
    function createRBox(){
    	var objNum = random(5);
    	var initY = 0;
    	var destY = 500;
    	var aWidth = 600;
    	var minSpeed = 4;
    	for(i=1;i<=objNum;i++){
    		var obj = _root.attachMovie("squareR", "squareR_mc" + i, i+10);
    	  //get randomized x
    	   obj._x = Math.random()*aWidth;
    	   obj._y = initY;
    	   // randomize moving speed
    	   obj.speed = Math.random()*(maxSpeed - minSpeed) + minSpeed;
    	   // assign onEnterFrame event handler
    	   obj.onEnterFrame=function(){
    	       if(this._y <= destY){
    	          // if not reach pre-defined destY
    	          this._y += this.speed;
    			  this._rotation += this.speed;
        	   }else{
    	          // if reach pre-defined destY
    	          // delete onEnterFrame event handler
    	          delete this.onEnterFrame;
    	       }
    	   }
    	}
    }
    Second, before I call the fuction I will need to have a timeout, before the script is activated again, like a second or something so will this work?
    Code:
    _root.onEnterFrame = function (){
    	createReady();
    	if (redBoxReady){
    		createRBox();
    		redBoxReady = false;
    		redBoxReadyNumber = 0;
    	}if (blueBoxReady){
    		createBBox();
    		blueBoxReady = false;
    		blueBoxReadyNumber = 0;
    	}if (yellowBoxReady){
    		createYBox();
    		yellowBoxReady = false;
    		yellowBoxReadyNumber = 0;
    	}if (greenBoxReady){
    		createGBox();
    		greenBoxReady = false;
    		greenBoxReadyNumber = 0;
    	}if (blackBoxReady){
    		createBlBox();
    		blackBoxReady = false;
    		blackBoxReadyNumber = 0;
    	}if (pinkBoxReady){
    		createPBox();
    		pinkBoxReady = false;
    		pinkBoxReadyNumber = 0;
    	}if (brownBoxReady){
    		createBrBox();
    		brownBoxReady = false;
    		brownBoxReadyNumber = 0;
    	}if (orangeBoxReady){
    		createOBox();
    		orangeBoxReady = false;
    		orangeBoxReadyNumber = 0;
    	}if (coneReady){
    		createCone();
    		coneReady = false;
    		coneReadyNumber = 0;
    	}
    }
    Then here is the create ready function:
    Code:
    function createReady(){
    	if (started === undefined){
    		//Booleans
    		var started:Boolean = true;
    		var redBoxReady:Boolean = false;
    		var blueBoxReady:Boolean = false;
    		var yellowBoxReady:Boolean = false;
    		var greenBoxReady:Boolean = false;
    		var blackBoxReady:Boolean = false;
    		var pinkBoxReady:Boolean = false;
    		var brownBoxReady:Boolean = false;
    		var orangeBoxReady:Boolean = false;
    		var coneReady:Boolean = false;
    		//Numbers
    		var redBoxReadyNumber:Number = 0;
    		var blueBoxReadyNumber:Number = 0;
    		var yellowBoxReadyNumber:Number = 0;
    		var greenBoxReadyNumber:Number = 0;
    		var blackBoxReadyNumber:Number = 0;
    		var pinkBoxReadyNumber:Number = 0;
    		var brownBoxReadyNumber:Number = 0;
    		var orangeBoxReadyNumber:Number = 0;
    		var coneReadyNumber:Number = 0;
    		//Numbers 2
    		var rDest:Number = random (40 + 1);
    		var bDest:Number = random (120 + 1);
    		var yDest:Number = random (100 + 1);
    		var gDest:Number = random (140 + 1);
    		var blDest:Number = random (110 + 1);
    		var pDest:Number = random (120 + 1);
    		var brDest:Number = random (120 + 1);
    		var oDest:Number = random (160 + 1);
    		var cDest:Number = random (7 + 1);
    	}else if (started){
    		//Increase the Timeout's value
    		if (redBoxReadyNumber < rDest){
    			redBoxReadyNumber ++;
    		}if (blueBoxReadyNumber < rbDest){
    			blueBoxReadyNumber ++;
    		}if (yellowBoxReadyNumber < yDest){
    			yellowBoxReadyNumber ++;
    		}if (greenBoxReadyNumber < gDest){
    			greenBoxReadyNumber ++;
    		}if (blackBoxReadyNumber < blDest){
    			blackBoxReadyNumber ++;
    		}if (pinkBoxReadyNumber < pDest){
    			pinkBoxReadyNumber ++;
    		}if (brownBoxReadyNumber < brDest){
    			brownBoxReadyNumber ++;
    		}if (orangeBoxReadyNumber < oDest){
    			orangeBoxReadyNumber ++;
    		}if (coneReadyNumber < cDest){
    			coneReadyNumber ++;
    		}
    		//Check For Completion
    		if (redBoxReadyNumber == rDest){
    			redBoxReady = true;
    			var rDest:Number = random (40 + 1);
    		}if (blueBoxReadyNumber == rbDest){
    			blueBoxReady = true;
    			var bDest:Number = random (120 + 1);
    		}if (yellowBoxReadyNumber == yDest){
    			yellowBoxReady = true;
    			var yDest:Number = random (100 + 1);
    		}if (greenBoxReadyNumber == gDest){
    			greenBoxReady = true;
    			var gDest:Number = random (140 + 1);
    		}if (blackBoxReadyNumber == blDest){
    			blackBoxReady = true;
    			var blDest:Number = random (110 + 1);
    		}if (pinkBoxReadyNumber == pDest){
    			pinkBoxReady = true;
    			var pDest:Number = random (120 + 1);
    		}if (brownBoxReadyNumber == brDest){
    			brownBoxReady = true;
    			var brDest:Number = random (120 + 1);
    		}if (orangeBoxReadyNumber == oDest){
    			orangeBoxReady = true;
    			var oDest:Number = random (160 + 1);
    		}if (coneReadyNumber == cDest){
    			coneReadyNumber ++;
    			var cDest:Number = random (7 + 1);
    		}
    	}
    }
    Let me explain my variables. (color Inital)Dest is the number that is the timeout, my flash movie is running at a 20FPS frame rate. So 120 is 6 seconds. Then (color)BoxReadyNumber is the number that continues to go up until it reaches the timeout's (dest) value. Then (color)BoxReady is true. When that is true, it runs the script and resets the numbers to start it again. Will this work?
    Edit: (2 mins later) HOLD on. Forget it if (started) thing. I declared the values in the first frame, so forget that.
    Last edited by SpikeyOmega; 04-18-2005 at 08:50 AM.

  8. #8
    Senior Member SpikeyOmega's Avatar
    Join Date
    Dec 2004
    Posts
    400
    Yeah, sorry for dbl posting but...:
    The functions and everything are perfect, however like I said the squares dont fall, and now whenever the rDest == redBoxNum...etc it deletes the old squares and replaces them with new ones. How can it be where it just adds more instead of replacing?

  9. #9
    Re Member websam's Avatar
    Join Date
    Jul 2000
    Location
    Australia (VIC)
    Posts
    660
    Let me solve your problems one at a time.

    Problem 1: Box not falling
    Reason:
    the scope of variable destY has changed when you declare it inside the createBox function. When you decalre destY inside the function, it is no longer a global accessible variable. It becomes a variable of createBox().
    Code:
    ...
    var destY = 500;
    ...
         if(this._y <= destY){
    To solve this, you may declare all constants in _root level.
    Code:
    var initY = 0;
    var destY = 500;
    var aWidth = 600;
    var minSpeed = 4;
    
    function createRBox(){
       var objNum = random(5);
       ...
       if(this._y <= _root.destY){
    }

    For your problem 2, I am not quite understand. Are you trying to create random number of red boxes, wait for 1 second, create another lot of blue boxes, then green box...and so on? I want to wait for your reply before getting into it.

    Problem 2 has 2 questions in it. Another question of you is that the newly created boxed replaced the existing box. The reason is that when you call createBox() after the first time, your i counter has been reset.
    Code:
    objNum = random(5);
    for(i=1;i<=objNum;i++){
       var obj = _root.attachMovie("squareR", "squareR_mc" + i, i+10);
    Then, all you newly attached mc will have the same name and level as the first lot. The one with same name/level will be replaced by new mcs. You could have a variable to keep aggregating the number of boxes, see below
    Code:
    var totalBox=0;
    
    function createBox(){
       objNum = random(5)+1;
       ...
       for(i=1+totalBox;i<=objNum+totalBox;i++){
          var obj = _root.attachMovie("squareR", "squareR_mc" + i, i+10);
       } // end for loop
       ...
      totalBox += objNum;
    }
    Wait for yoru reply before I solve the 1 second wait problem.
    Last edited by websam; 04-18-2005 at 09:54 AM.

  10. #10
    Senior Member SpikeyOmega's Avatar
    Join Date
    Dec 2004
    Posts
    400
    Ahh, ok thats not what I am trying to do. I do not think I scripted createReady() correctly. What I am trying to do is have random timeouts generated. Then when the timeout expires the colored square will be created and fall, then the timeout will be re-randomized and started. Then when it expires again etc... All the colored blocks are doing this at the same time, they are not in any sequence, however the reason there are different random numbers is because some are more rare, like invincability is more rare then point up (red) because point up just gives you points.

  11. #11
    Re Member websam's Avatar
    Join Date
    Jul 2000
    Location
    Australia (VIC)
    Posts
    660
    Sorry... I am confused now...

    Do you want to generate random amount of same colored box in random interval? or generate single box in each random interval?

  12. #12
    Senior Member SpikeyOmega's Avatar
    Join Date
    Dec 2004
    Posts
    400
    My god... is it really that confusing? Well, everytime it will randomize the number of boxes that will be created, and then they will fall (at diff speeds), then the timeout will go again, and when thats done it will randomize again and that amount of THAT COLOR will fall.

  13. #13
    Senior Member SpikeyOmega's Avatar
    Join Date
    Dec 2004
    Posts
    400
    Ok, I posted the fla. However, the movie is messed up, it makes the comp run slow and unresponsive. Ever since i changed the vars in the beginning and did the _root.destY thing it messes up. Maybe you guys can see why.

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