9 Replies Latest reply on Dec 11, 2009 5:47 PM by Ned Murphy

    Trying to write a line that has 3 options, then randomly choose between the 3

    TF22Raptor2 Level 1

      Hi all,
      I am trying to randomly rotate an object in flash, I have done this previously via:

       

      this._rotation = random ( 360 );

       

      But now I would like to specific 3 rotation angles, that it is able to randomly choose between instead of 0 to 360.

       

      Eg. I might have 0, 120, 240 as the 3 options I'd like it to randomly choose between.

       

      Any help or advice or examples would be really appreciated.

        • 1. Re: Trying to write a line that has 3 options, then randomly choose between the 3
          Ned Murphy Adobe Community Professional & MVP

          For those particular values you could use the following...

           

          this._rotation = Math.floor(Math.random()*3) * 120;

           

           

          If you want to be able to change the three values, or change the number of values, then a more adaptive approach would be to use an array to define the different values and change the code to use the array...

           

          var rotValues:Array = new Array(0,120,240);

           

          this._rotation = rotValues[Math.floor(Math.random()*rotValues.length)];

          • 2. Re: Trying to write a line that has 3 options, then randomly choose between the 3
            TF22Raptor2 Level 1

            HI Ned thanks for the help having some difficulty with

             


            var rotValues:Array = new Array(0,50,190);
            this._rotation = rotValues[Math.floor(Math.random()*rotValues.length)];

             

            Am I supposed to do anything else? As at the moment it doesnt do anything.

             

            I've just replaced

             

            this._rotation = random ( 360 );

             

            with what you have above.

             

            Any help would be great

            • 3. Re: Trying to write a line that has 3 options, then randomly choose between the 3
              Ned Murphy Adobe Community Professional & MVP

              If your code worked as you had it, my code can be copied in place of it and it should set the _rotation value, at least it does when I test it using an "mc" named instance rather than "this"  My only question if your code doesn't work would be what "this" is in your case, and where that code is being placed.

               

              If it seems to not be working, you may just be getting the 0 value as a result, so you may have to run it a few times.

              • 4. Re: Trying to write a line that has 3 options, then randomly choose between the 3
                TF22Raptor2 Level 1

                Yeah thats what I have done Ned.

                 

                Just replaced

                 

                this._rotation = random ( 360 ); //This works

                 

                with:

                 

                var rotValues:Array = new Array(25,50,190);
                this._rotation = rotValues[Math.floor(Math.random()*rotValues.length)];

                 

                I changed the 0 to a 25 but still there is not rotation produced.

                 

                The other line you supplied works, but its the above way I really want to use:

                this._rotation = Math.floor(Math.random()*3) * 180;

                 

                 

                I also tried,

                 

                var rotValues:Array = new Array(25,50,190);
                this._rotation = rotValues[Math.floor(Math.random()*rotValues.length)];

                 

                in a seperate flash file and yes it also works there.

                 

                 

                What do you think could be the problem with that bit of code, I thought it would be just self contained and just exeute by itself.

                I am doing this rotation stuff at about 3 movies clips deep. Movieclip1/moviesclip2/movieclip3

                 

                I have a mc called mcrotateme_mc on one layer of the timeline and on the other I have actions with the above code.

                • 5. Re: Trying to write a line that has 3 options, then randomly choose between the 3
                  Ned Murphy Adobe Community Professional & MVP

                  So that code would be changing the _rotation of whatever movieclip is holding mcrotateme_mc.  If the goal is to rotate mcrotateme_mc, then you could try using its name in place of "this" and see if that works.

                   

                  I cannot see any reason why the code would not work if your code worked and you just replaced it with what I offered.  You should try placing trace(this._rotation); just after the second line to see what it indicates.

                  • 6. Re: Trying to write a line that has 3 options, then randomly choose between the 3
                    TF22Raptor2 Level 1

                    Yeah ned I did trace using the named instacne for the movie clip and then updating the code to reflect but it just did the same thing (no rotation). This is what I get.

                     

                    var rotValues:Array = new Array(25,50,190);
                    this._rotation = rotValues[Math.floor(Math.random()*rotValues.length)];
                    trace(this._rotation);

                     

                    outputted trace shows

                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0
                    0

                     

                     

                     

                    The other thing I am doing inside this movieclip that holds the rotation movie clip is that on the first line I am randomly going to the frames that hold these movieclips and actions.

                     

                     

                    eg First frame of movieclip1/movieclip2/movieclip3

                     

                    stop();
                    gotoAndStop(random(2)+2);

                     

                     

                    second frame

                    rotateme_mc

                    actions

                    var rotValues:Array = new Array(25,50,190);
                    this._rotation = rotValues[Math.floor(Math.random()*rotValues.length)];
                    trace(this._rotation);

                     

                     

                    third frame

                    rotateme2_mc

                    actions to rotate this in a different manner

                     

                    four frame etc etc etc

                    • 7. Re: Trying to write a line that has 3 options, then randomly choose between the 3
                      Ned Murphy Adobe Community Professional & MVP

                      This is where AS3 would be a friendlier player for you if what I suspect is the case is what it is, though it may not be (AS3 will tell you anytime code is messed up in some way).

                       

                      We've both seen the code worked as expected, so it's not the code itself, just something else in the file interfering.

                       

                      Are you declaring the same array variable multiple times?  If so, don't do that, just declare:  var rotValues:Array;  once, all by itself, possibly on it's own layer that extends the length of the timeline, and in each case where you assign it new values just use...

                       

                      rotValues = new Array(...your values...);

                       

                      If that doesn't pan out, then I may need to look at your file to try to see what might be causing the problem.  If that's the case, I cannot open CS4 files, so you'd have to save it as a CS3 file for that to happen.  I don't know where in the world you are, but where I am it's about bedtime, so don't expect much else today from me.

                      1 person found this helpful
                      • 8. Re: Trying to write a line that has 3 options, then randomly choose between the 3
                        TF22Raptor2 Level 1

                        HI Ned I'm in AUstralia mate, thanks for the help on this turns out the file I was working on i brought it back from 6-7 years ago and it was only set to actionscript 1 output! Switched it over to AS2 worked straight away!


                        Whats your advice for someone to become a better programmer in Flash? I'm fairly confident with the the structure of flash, the timeline calling objects in the timeline, named instances, functions, stacking order etc but just have never really done much work with math, and creating randomness via math and the different ways you can go about it. I'm coming from more of a designer background and don't really have the computer science skills etc

                         

                        Thanks for the help on this really apprecaited.

                        • 9. Re: Trying to write a line that has 3 options, then randomly choose between the 3
                          Ned Murphy Adobe Community Professional & MVP

                          That's funny, at one point I was going to see about setting it to AS1 to see if that might be related to the problem, but I got sidetracked I guess.

                           

                          As far as becoming better at anything, including programming, here's a link to a posting I added my voice to that pretty much sums up my approach to learning... I'm no expert myself, but helping in the forums helps me learn new things and keep my feet wet when opportunities are otherwise dry...

                           

                          http://forums.adobe.com/thread/537468?tstart=0

                          1 person found this helpful