7 Replies Latest reply on Jul 12, 2016 10:46 AM by ClayUUID

    Movieclips random not work in HTML canvas

    noppanatp30620432 Level 1

      This code works for as3 but why does it not work for HTML canvas?

      Can anyone help me?

       

       

      var MovieArray:Array = new Array(btr,btg,bty);

      for(var i:int = 0;  i < MovieArray.length;  i++){

      var RandomNumber:Number = Math.floor(Math.random()* MovieArray.length)

      var MoviePosX:Number = MovieArray[RandomNumber].x;

      var MoviePosY:Number = MovieArray[RandomNumber].y;

      MovieArray[RandomNumber].x = MovieArray[i].x;

      MovieArray[RandomNumber].y = MovieArray[i].y;

      MovieArray[i].x = MoviePosX

      MovieArray[i].y = MoviePosY

      }

        • 1. Re: Movieclips random not work in HTML canvas
          Colin Holgate MVP & Adobe Community Professional

          Variables don't have types in Javascript. Also, you have to show the current level when referring to display objects, such as movieclips.

           

          I haven't tested this, but your code would need to be more like this:

           

          var MovieArray = new Array(this.btr,this.btg,this.bty);

          for(var i:int = 0;  i < MovieArray.length;  i++){

            var RandomNumber = Math.floor(Math.random()* MovieArray.length);

            var MoviePosX = MovieArray[RandomNumber].x;

            var MoviePosY = MovieArray[RandomNumber].y;

            MovieArray[RandomNumber].x = MovieArray[i].x;

            MovieArray[RandomNumber].y = MovieArray[i].y;

            MovieArray[i].x = MoviePosX;

            MovieArray[i].y = MoviePosY;

          }

          • 2. Re: Movieclips random not work in HTML canvas
            ClayUUID Adobe Community Professional

            Also you really shouldn't use new in JavaScript unless there's some very specific reason for it (JavaScript, We Hardly new Ya). So that first line would be better written like this:

             

            var MovieArray = [this.btr, this.btg, this.bty];

             

            And re-declaring variables every time through a loop is really bad coding practice.

            • 3. Re: Movieclips random not work in HTML canvas
              noppanatp30620432 Level 1

              Thank you very much. But still do not work for this code.

               

              var MovieArray = [this.btr, this.btg, this.bty];

              for (var i: int = 0; i < MovieArray.length; i++) {

                var RandomNumber = Math.floor(Math.random() * MovieArray.length);

                var MoviePosX = MovieArray[RandomNumber].x;

                var MoviePosY = MovieArray[RandomNumber].y;

                MovieArray[RandomNumber].x = MovieArray[i].x;

                MovieArray[RandomNumber].y = MovieArray[i].y;

                MovieArray[i].x = MoviePosX;

                MovieArray[i].y = MoviePosY;

              }

              • 5. Re: Movieclips random not work in HTML canvas
                Nipun Asthana Adobe Employee

                There are still a couple of errors in your code. Use this instead:

                 

                var MovieArray = [this.btr, this.btg, this.bty];

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

                  var RandomNumber = Math.floor(Math.random() * MovieArray.length)

                  var MoviePosX = MovieArray[RandomNumber].x;

                  var MoviePosY = MovieArray[RandomNumber].y;

                  MovieArray[RandomNumber].x = MovieArray[i].x;

                  MovieArray[RandomNumber].y = MovieArray[i].y;

                  MovieArray[i].x = MoviePosX;

                  MovieArray[i].y = MoviePosY;

                }

                 

                In general, when moving from AS3 to JS, you have to make the following changes primarily:

                1. all the instance names should be referenced as 'this.instance-name'

                2. all the ':type' specifiers should be removed.

                • 6. Re: Movieclips random not work in HTML canvas
                  Colin Holgate MVP & Adobe Community Professional

                  I was curious what the difference was, and it's because in the one I posted I still left in the var I:int part.

                  • 7. Re: Movieclips random not work in HTML canvas
                    ClayUUID Adobe Community Professional

                    The only refinements I'd suggest on the previous code would be:

                    • Don't declare variables inside a loop. This slows down execution. The difference is negligible with a loop this small, but very often it's not, so there's no reason not to break yourself of the habit.
                    • Don't use verbose variable names for simple tasks like this. It just makes the code harder to read.
                    • Group logically related lines of code together.
                    • Comment what you're doing if it's even remotely non-obvious. Future you will thank you.

                     

                    So this is how I'd tweak it:

                     

                    var i, r, x, y;
                    var movies = [this.btr, this.btg, this.bty];
                    for (i = 0; i < movies.length; i++) {
                        r = Math.floor(Math.random() * movies.length);
                        // swap X
                        x = movies[r].x;
                        movies[r].x = movies[i].x;
                        movies[i].x = x;
                        // swap Y
                        y = movies[r].y;
                        movies[r].y = movies[i].y;
                        movies[i].y = y;
                    }