6 Replies Latest reply on Oct 17, 2012 10:30 AM by Ned Murphy

    Random Position outside of the stage

    alaStickman

      Hello,

      any idea to "inverse" this ??

      tempEnemy._x = random(Stage.width);

                          tempEnemy._y = random(Stage.height);

      i want them to appear outside of the stage randomly (in limit), i guess what i need is an interval of 2 intervals, anyone can help plz

      thank you

        • 1. Re: Random Position outside of the stage
          Ned Murphy Adobe Community Professional & MVP

          You need to choose between two sides of the stage for each axis which you can accomplish by the code equivalent of a coin toss - the random function can provide that. 

           

          From there it is just a matter of determining how far from the stage you need to be, which could depend on which side you are heading to and where the registration mark is for whatever object you are placing.  If the goal is to plant the object just enough off stage so that it is not seen, then  at some point you will probably have to take the width of the object into account when placing it. 

          1 person found this helpful
          • 2. Re: Random Position outside of the stage
            alaStickman Level 1

            what i want to do is create enemies outside of the stage (max 100 pixel far away all around the stage) so it gives the impression like they are coming from everywhere,

            if you may, i want to ask you to help me writing a code that do the following,

            i want to create a movie clip randomly between [900..1000] or [-150..-50]  so the enemy X or Y can be a number between 900 and 1000 or -150 and -50

            thank you

            • 3. Re: Random Position outside of the stage
              prodigalmaster Level 2

              900+Math.random()*100;

               

              The above returns a random number between 900 and 1000. This is because the number begins with 900 and adds a random number between 0 and 100.

               

              random(x); has been deprecated. I do not use it because it produced strange behaviour once. Instead, Math.random()*x; should be used.

              1 person found this helpful
              • 4. Re: Random Position outside of the stage
                Ned Murphy Adobe Community Professional & MVP

                I know what you want to do.  I am trying to help you write a code, but it's up to you to try to write it. I am only going to try to explain what you need to write... someone else might come along and decide to write it for you.

                 

                See about getting the coin toss to work first... use the Math.random() function to decide whether to use the right or left of the stage for your _x and the top or bottom fir your _y.  Can you think of how that might be used?

                 

                Once you have that decision made, you can then work out the specific value based on which side you're on and how wide the object is.

                1 person found this helpful
                • 5. Re: Random Position outside of the stage
                  alaStickman Level 1

                  thank you ^_^  i did it, here is the code (maybe someone could use it one day)

                  --------------

                  //the stage is : 1024 x 768, enemies will appear reandomle 100 pixel far away from the stage

                  choiceX = random(2);

                            choiceY = random(2);

                            if (choiceX == 0)

                            {

                                      //("left");

                                      xpos = random(150);

                                      xpos = -xpos;

                            }

                            else

                            {

                                      //("right");

                                      xpos = random(1021 - 100) + 100;

                   

                            }

                            if (choiceY == 0)

                            {

                                      //("up");

                                      ypos = random(150);

                                      ypos=-ypos

                   

                            }

                            else

                            {

                                      //("down");

                                      ypos = random(900- 800) + 800;

                   

                            }

                   

                  -------------

                  • 6. Re: Random Position outside of the stage
                    Ned Murphy Adobe Community Professional & MVP

                    You're welcome