Skip to main content
Participating Frequently
June 29, 2022
Answered

Adobe Animate HTML5 Canvas Random Position Change

  • June 29, 2022
  • 1 reply
  • 244 views

How can I make movieclips randomly switch to each other's position?

movieclips name: harf_1, harf_2, harf_3...

    This topic has been closed for replies.
    Correct answer kglad

    // begin initialization

    var harfA = [];

    var positionA = [];

     

    for(var i=1;i<=14;i++){  // assuming there are 14 harf movieclips

    harfA.push(this["harf_"+i]);

    positionA.push({x:harfA[i-1].x, y:harfA[i-1].y});

    }

     

    function shuffleF(a) {
    for (var i = a.length - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    }
    }

    // end initialization

     

    // whenever you want to shuffle the harf positions, call

    function shuffle_harfF(){

    shuffleF(harfA);

    for(var i=0;i<harfA.length;i++){

    harfA[i].x = positionA[i].x;

    harfA[i].y = positionA[i].y

    }

    }

     

    1 reply

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    June 29, 2022

    // begin initialization

    var harfA = [];

    var positionA = [];

     

    for(var i=1;i<=14;i++){  // assuming there are 14 harf movieclips

    harfA.push(this["harf_"+i]);

    positionA.push({x:harfA[i-1].x, y:harfA[i-1].y});

    }

     

    function shuffleF(a) {
    for (var i = a.length - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    }
    }

    // end initialization

     

    // whenever you want to shuffle the harf positions, call

    function shuffle_harfF(){

    shuffleF(harfA);

    for(var i=0;i<harfA.length;i++){

    harfA[i].x = positionA[i].x;

    harfA[i].y = positionA[i].y

    }

    }