A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: [F8] Preventing Repeat Selections

  1. #1
    Member
    Join Date
    Mar 2006
    Location
    Torrington, CT
    Posts
    44

    [F8] Preventing Repeat Selections

    Hello all. The following is some code to have movie clips randomly loaded into the main movie. How would I go about making it so that once a certain number out of the range was selected, it would not be repeated. That is to say, if the movie clip mcExercise2 has been loaded, it will never happen again, it will select a different number because "2" has already been used.

    Code:
    var nExerciseCount:Number = 0;
    
    
    function randRange(min:Number, max:Number):Number {
    		var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
    		return randomNum;
    	}
    	for (var i = 0; i<100; i++) {
    		
    	}
    	
    	function displayExercise():Void {
    	mcExercise.removeMovieClip();
    	this.attachMovie("mcC1Exercise"+randRange(1, 3), "mcExercise", this.getNextHighestDepth());
    	mcExercise._x = 410;
    	mcExercise._y = 270;
    	
    	for(i=0; i<5; i++) { }
    	if(nExerciseCount <= 3) {
    		nExerciseCount++;
    		trace(nExerciseCount);
    			}
    			
    	else {
    	mcExercise.removeMovieClip();
    	gotoAndPlay("C1A_End");
    	}
    };
    Any input or advice is most welcome. Thanks...

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    See attached file...
    Last edited by dawsonk; 08-22-2007 at 10:51 AM.

  3. #3
    Member
    Join Date
    Mar 2006
    Location
    Torrington, CT
    Posts
    44
    Wow, that's great! Pardon my ignorance, but can you explain to me fundamentally why this works? I don't need a line by line breakdown, but I'm trying to learn actionScript, and rather than being given the fish, I would also like to learn how to fish too!

    I realize you folks don't have time to be tutors, and I'm doing a lot of research on my own, but I have a heck of a time wrapping my brain around arrays.

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    // 
    stop();
    // setup
    var maxNumOfExercises = 10;
    // initializes array
    var randArray = new Array();
    // below pushes item one at a time
    // (adds a leading zero to numbers below nine
    for (var i = 1; i <= maxNumOfExercises; i++) {
    	var num = String(i);
    	if (i < 10) {
    		num = "0" + num;
    	}
    	randArray.push(num);
    }
    // array at this point looks like (01, 02, 03, etc)
    //
    // next, this randomize the contents of the array just created
    randArray.sort(function (a, b) {
    	return Math.round(Math.random() * 2) - 1;
    });
    //
    // arrays start at zero, so initializing a counter to -1
    var CT = -1;
    //
    // function used to attach the movie clip from the library, 
    // make sure you set the linkage:export identifer
    function displayExercise() {
    	// traces the current count and the number from the randomize array at current count
    	trace("CT: "+CT+" randVal: "+randArray[CT]);
    	this.attachMovie("mcC1Exercise" + randArray[CT], "Exercise_" + randArray[CT], 100);
    }
    // buttons to increment and decrement the counter and call the displayExercise function
    btnNext.onPress = function() {
    	if (CT < maxNumOfExercises-1) {
    		CT++;
    		displayExercise();
    	}
    };
    btnBack.onPress = function() {
    	if (CT > 0) {
    		CT--;
    		displayExercise();
    	}
    };
    HTH

  5. #5
    Member
    Join Date
    Mar 2006
    Location
    Torrington, CT
    Posts
    44
    Wow, much thanks!

    I don't have time to read this at the moment, but I'm going to study this tomorrow when I have the day off, if I have any questions I guess you don't mind me posting to the forum.

    Thanks for all of your help

  6. #6
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Basically set up an array with the numbers you want randomized.
    Here, used a ‘for’ loop to push items one at a time into the array.
    Randomized the array.
    Use a counter as an index into the array – if counter is 5, it gets what’s in the array at location 5. – if counter is 0, it gets what’s in the array at location 0. (Arrays start at zero)

    So, if the randomized array looks like this (07, 02, 03, 09, etc) and the counter is at 2.
    The value of the location is ‘03’.
    And this is what gets added to the string forming the exercise movie clip identifier.

  7. #7
    Member
    Join Date
    Mar 2006
    Location
    Torrington, CT
    Posts
    44
    Thanks for the explanation.. I can use yours with no problem, when I try to apply it to mine I'm having some issues however.

    I don't want the function displayExercise to be called when a button is clicked, I want it to occur when that frame is loaded.. I tried a variety of methods but was not successful, the only way I was able to call the function was by using your button.onRelease.. thoughts?

  8. #8
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    	if (CT < maxNumOfExercises-1) {
    		CT++;
    		displayExercise();
    	}

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