CS6 How to get 53 different objects to randomly enter an area 5 at a time?
A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: CS6 How to get 53 different objects to randomly enter an area 5 at a time?

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    Guangxi
    Posts
    4

    CS6 How to get 53 different objects to randomly enter an area 5 at a time?

    I've used flash before but action script is completely new to me. Please excuse me for cross posting.

    I'm working in a Japanese game for my students to practice on.

    I've figured out how to get one object to randomly duplicate itself and fall downwards but I have 53 different objects that I would like to have randomly enter a designated area 5 at a time.

    Any help would be greatly appreciated.

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    106
    You will want to look into using an Array.
    1) Make an Array
    2) Randomize
    3) Use Array instead of MC code

    1) An Array is an object that can hold multiple other objects.
    var myArray:Array = [mc1,mc2,mc3,mc4,mc5];

    myArray[0] calls the mc1;
    myArray[1] calls the mc2;
    myArray[2] calls the mc3;
    myArray[3] calls the mc4;

    2) Once you get the array to hold all the MovieClips you need, you will just need to grab a random index of the array:
    var randomMc:uint = Math.floor(Math.random()*myArray.length);

    3)The code you used to get one object to duplicate itself and fall downward, you can use again.
    replace the name of your current single MovieClip with:
    myArray[randomMc]

    If you post your code I could be of more assistance.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Location
    Guangxi
    Posts
    4
    Thank you I will try this and then post the code when I run into problems

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Location
    Guangxi
    Posts
    4
    Hello

    I'm not sure how to define the images in the array or get the image clip into the array and display a specified number of images from the array. I assume its just random(10) ?

    Below is my code.

    Thank you



    var balloons:Array = new Array("red2", "red2", "re_wo", "red_we", "red_to",
    "red_su", "red_shi", "red_sa", "red_ru", "red_ri","red_ra", "red_o", "red_ni", "red_na", "red_mi", "red_ku", "red_ko", "red_i", "red_fu", "purple_yu","purple_wa", "purple_no", "purple_ne", "purple_mo","purple_ka", "purple_he", "purple_a", "purple","orange_yo", "orange_wi", "orange_u", "orange_tsu","orange_te", "orange+se", "orange_me", "orange_ma","orange_kyo", "orange_ho", "orange_ha", "orange","blue_ya", "blue_ta", "blue_so", "blue_ro", "blue_nu", "blue_mu", "blue_ki", "blue_hi","blue_e", "blue_chi");


    import mx.transitions.Tween;
    import mx.transitions.easing.*;


    balloons._alpha = 0;


    //attachMovie("red2", "red2", "re_wo", "red_we", "red_to", "red_su", "red_shi", "red_sa", "red_ru", "red_ri", "red_ra", "red_o", "red_ni", "red_na", "red_mi, depth[1]);




    hitBackground.onEnterFrame = function(){

    if ( hitBackground.hitTest(balloons) ){
    balloons._alpha = 100;

    } else {
    balloons_alpha = 0;
    }
    }

    counter = 0;


    hitBackground.onPress = function(){

    counter = counter + 1;

    if (counter == 1){
    var balloonstween1:Tween = new Tween(balloons, "_x", mx.transitions.easing.Regular.easeIn, random(6), false);
    balloonstween2.onMotionFinished = function(){
    attachMovie("balloons","balloons",1);
    }
    }
    }

  5. #5
    Senior Member
    Join Date
    Nov 2012
    Posts
    106
    I just looked over the code you have posted and It looks like you are duplicating your variable names. You are also coding with AS2. I am more familiar in AS3. I would have to play around with the file to get it to work. It seems like you have more going on than what I can see by reading the code.

    You have two different red2 names in your array. It also seems you have an array called balloons and it looks like you also have another object that you are wanting to control the alpha called balloons?

    If you want to control each item inside of the Array, you will need to learn about "loops"

    for (i=0; i<balloons.length;i++){
    balloons[i]._alpha = 0;
    }
    This will change the alpha of each of your movieclips alpha. An array is just a container of information and does not have ._alpha property.

    If you post your actual FLA I will try to take a look at it.

  6. #6
    Junior Member
    Join Date
    Dec 2012
    Location
    Guangxi
    Posts
    4

    Flash file attached

    @jkell

    thanks for your reply.
    Yes this is AS2 as I understand it AS3 deals with classes so it probably would be too complex for me at this time.

    Yes there are 2 red2 names in the array this is an error.

    I've attached the fla file perhaps it will be a bit more clearer.
    What I am trying to do is allow for 6 objects to pass through the visible screen at one time (alpha = 100) while the remainder are invisible.

    loops makes sense because I am trying to control the objects in the array.

    for (i=0; i<balloons.length;i++){
    balloons[i]._alpha = 0;
    }

    what does the "i" mean in this code?

    The fla file was too big (10mb) to attach so I am including it here
    www.sanamionline.net/images/finished_file.fla (CS6)

    Regards.

  7. #7
    Senior Member
    Join Date
    Nov 2012
    Posts
    106
    The "i" in the for loop is just a variable. You could name it whatever you want, but it is standard practice to use "i".

    In as3 you would need to define the variable inside the for loop like this:
    for(var i:uint=0; i<array.length;i++){
    //the code to execute would go here
    }

    I will take a look at your Fla over the weekend.

  8. #8
    Senior Member
    Join Date
    Nov 2012
    Posts
    106
    if you copy and paste this code, it will work.

    I am handling the balloon animations with enter_frame, so it looks a little jerky-
    Code:
    var balloons:Array = new Array("red2", "re_wo", "red_we", "red_to", "red_su", "red_shi", "red_sa", "red_ru", "red_ri", "red_ra", "red_o", "red_ni", "red_na", "red_mi", "red_ku", "red_ko", "red_i", "red_fu", "purple_yu", "purple_wa", "purple_no", "purple_ne", "purple_mo", "purple_ka", "purple_he", "purple_a", "purple", "orange_yo", "orange_wi", "orange_u", "orange_tsu", "orange_te", "orange-se", "orange_me", "orange_ma", "orange_kyo", "orange_ho", "orange_ha", "orange", "blue_ya", "blue_ta", "blue_so", "blue_ro", "blue_nu", "blue_mu", "blue_ki", "blue_hi", "blue_e", "blue_chi");
    var counter:Number = 0;
    var moveBalloons:Array = new Array();
    var distanceMoved=3;//this is distance all the balloons move 1 is slow and 5 is fast
    
    //this function will move the balloons down
    hitBackground.onEnterFrame= function(){
    	//this moves the balloons
    	for(var i=0;i<moveBalloons.length;i++){
    		_root[moveBalloons[i]]._y+=distanceMoved;
    	}
    //this removes the movieclips when they get to a certain y position
            if(_root[moveBalloons[i]]._y>400){
    			//trace('kill this one');
    			_root[moveBalloons[i]].removeMovieClip();
    		}
    }
    
    //this function makes a new balloon every 2.3 seconds
    setInterval(makeBalloon,2300);
    
    //this function creates a random balloon
    function makeBalloon(){
    	randomNum= random(balloons.length);
    	newBalloon = balloons[randomNum];
    	_root.attachMovie(newBalloon,"object"+counter,_root.getNextHighestDepth(),{_x:60+Math.random()*600,_y:50});
    	moveBalloons.push("object"+counter);
    	counter++
    }
    makeBalloon();
    
    /*hitBackground.onEnterFrame = function(){
    	
    	if ( hitBackground.hitTest(balloons) ){
    		balloons._alpha = 100; 
    		
    	} else {
    		balloons_alpha = 0; 
    	}
    }
    */
    
    
    /*hitBackground.onPress = function(){
    	
    	counter = counter + 1;
    	
    	if (counter > 0){
    		var balloonstween1:Tween = new Tween(balloons, "_x", mx.transitions.easing.Regular.easeIn, random(6),  false);
    			balloonstween2.onMotionFinished = function(){
    				attachMovie(balloons[counter],"object"+counter);
    			}
    	}
    }*/
    Last edited by jkell; 01-08-2013 at 01:50 AM.

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

Poll by Flashkit.com