A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: FLASH AS3 RANDOM text No repeatation.

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    2

    Cool FLASH AS3 RANDOM text No repeatation.

    hi,, a new member here, am just starting to learn adobe flash..I have heard some people saying "stop learning it, it's outdated," but I would like to try it first.. I have been making a simple code in ACTIONSCRIPT3, what it does is that it generates a random name from list of array if the Button generate is clicked, It works just fine however I am having a hard time on how can I restrict it from Generating Repeated Name.NO REPEATED NAMES MUST BE GENERATED. THank you and advance, I appreciate any help I could get.

    My code goes this way.
    [B]import flash.events.MouseEvent;


    var names:Array = ["nameA","nameB","nameC","nameD"];

    generate.addEventListener(MouseEvent.CLICK,generat eName);
    function generateName(event:MouseEvent): void{

    var randomSurname= names[int(Math.random() * names.length)];
    result.text=randomSurname.toString();

    }
    [/COLOR][/B]

  2. #2
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Hi, first of all flash air has had a lot of recent updates actually, here is my script:

    once all have been used you get a trace that warns you that the collector array is full.
    PHP Code:
    import flash.events.MouseEvent
    var 
    names:Array = ["nameA","nameB","nameC","nameD"]; 
    var 
    collected_names=[]; 
    addEventListener(MouseEvent.CLICK,mc); 
    function 
    mc(e:*){ 
    if(
    e.target.name=="generate"){ 
    generateName() 


    var 
    fails=0
    function 
    generateName(){ 
    if(
    collected_names.indexOf(names[randomRange(0,names.length-1)])==-1){ 
    collected_names.push(names[randomRange(0,names.length-1)]) 
    result.text=names[randomRange(0,names.length-1)] 
    fails=0
    }else if(
    fails++<names.length*names.length&&collected_names.length!=names.length){ 
    generateName() 
    }else if(
    fails>names.length*names.length&&collected_names.length!=names.length){ 
    setTimeout(generateName,20);
    }else if(
    collected_names.length==names.length){
    trace("all names have been used"


      
    function 
    randomRange(minNum:Number,maxNum:Number):Number
    return(
    Math.floor(Math.random()*(maxNum minNum 1))+minNum); 

    Last edited by AS3.0; 03-08-2018 at 12:16 AM.

  3. #3
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    this one is better, the one above I messed up:

    PHP Code:
    import flash.events.MouseEvent
    var 
    names:Array = ["nameA","nameB","nameC","nameD"]; 
    var 
    collected_names=[]; 
    addEventListener(MouseEvent.CLICK,mc);
    function 
    mc(e:*){ 
    if(
    e.target.name=="generate"&&e.target.enabled){ 
    e.target.enabled=false
    generateName
    ();



    var 
    fails=0
    var 
    temp
    function generateName(){ 
    temp=names[randomRange(0,names.length-1)]
    if(
    collected_names.indexOf(temp)==-1&&collected_names.length!=names.length){ 
    generate.enabled=true
    collected_names
    .push(temp
    result.text=temp
    fails
    =0
    trace(collected_names)
    }else if(
    fails++<names.length*names.length&&collected_names.length!=names.length){ 
    generateName() 
    }else if(
    fails>names.length*names.length&&collected_names.length!=names.length){ 
    setTimeout(generateName,20);
    }else if(
    collected_names.length==names.length){
    generate.enabled=true
    trace
    ("all names have been used"


      
    function 
    randomRange(minNum:Number,maxNum:Number):Number
    return(
    Math.floor(Math.random()*(maxNum minNum 1))+minNum); 

    Last edited by AS3.0; 03-08-2018 at 12:41 AM.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    You can just use a simple splice method so you dont get the same name twice.

    CS6 File
    Attached Files Attached Files

  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Here is the code, you might be better off just using the fla attached previously.

    PHP Code:

    var nameArray:Array = new Array("Anne","Bella","Bob","Clara","Joe","Rick","Rebecca","Trevor","Tony");

    var 
    copyArray:Array = nameArray.concat();//--- duplicate original array as to preserve its data.
    var storeArray:Array = new Array();

    var 
    selectName:String;
    var 
    selectNumber:Number;

    nameButton.buttonMode true;

    trace("Original array : " nameArray);
    trace("Copy array : " copyArray);

    function 
    clickHandler(e:MouseEvent):void
    {
        
    e.currentTarget.gotoAndPlay("click");

        if (
    copyArray.length 1)
        {
            
    trace("Names all used");
            
    //--- Trace order of names picked.
            
    trace("Name pick order : " storeArray);

            return;
        }
        else
        {
            
    selectNumber Math.floor(Math.random() * copyArray.length);
            
    selectName copyArray[selectNumber];

            
    storeArray.push(selectName);

            
    copyArray.splice(selectNumber,1);

            
    nameBox.text selectName;
        }
    }

    nameButton.addEventListener(MouseEvent.MOUSE_DOWNclickHandler); 

  6. #6
    Registered User
    Join Date
    Mar 2018
    Posts
    2
    Wow.. Thank you very much, Different approach but yields with the same outcome. ,how could I add music to it? It would play a sound every time a random name would be generated.. Any tips I sound do.. I tried adding sound in my button.
    var mysound:MySound = new failedplay();
    mysound.play();
    but an error keeps on appearing.. Scene 1, Layer 'Script', Frame 1, Line 41 1046: Type was not found or was not a compile-time constant: MySound.
    What does this mean, Thank you

Tags for this Thread

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