4 Replies Latest reply on Oct 10, 2009 4:59 PM by kglad

    if(Key.isDown(Key.UP)) .. how do you get movment?

    bdennnn

      I get this part, if(Key.isDown(Key.UP)), If the UP key is pressed it will do ...

      But what is the line of code for the actual moving?

       

       

      And on another note I am also making it where an object will fall out of the sky at random positions, what might this code be?

      I cant seem to find a easy way to sift through the code to find out how to do these things so I've just come here to ask.

        • 1. Re: if(Key.isDown(Key.UP)) .. how do you get movment?
          Ned Murphy Adobe Community Professional & MVP

          Both of your questions involve manipulating the position properties of objects, _x and/or _y.

           

          I won't try to read further into what you offered, so if pressing down on the UP key will make something move, then you take that something and adjust its _x and/or _y properties.

           

          // the following would be preceded by some listening code...

          if(Key.isDown(Key.UP)){

               instanceName._y -= 5; // move it 5 up from where it is

          }

           

          To have a value be determined randomly, yoou use the Math.random() function.  You can look that up to see what it does, but what you usually do is multiply it by some range of a value to get a random number in that range.  So if you want to place an object at some random point across the stage and the stage is 600 pixels wide...

           

          instanceName._x = Math.random()*600;

          1 person found this helpful
          • 2. Re: if(Key.isDown(Key.UP)) .. how do you get movment?
            bdennnn Level 1

            Somewhat what I was looking for. However I am trying to get the object to fall from the top of the screen at random places.

             

            The object of the game is to avoid the falling pbjects and if it hits the instance to go to my end frame.

             

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

            And I am also looking to make a counter to see how many objects the player has actually avoided if anyone can help me there as well.

            Thanks!

            • 3. Re: if(Key.isDown(Key.UP)) .. how do you get movment?
              kglad Adobe Community Professional & MVP

              if your object(s) have left-edged reg points, you can use:

               

              obj._y=-obj.height;

              obj._x=Math.random()*(Stage.width-obj._width);

               

              // and if you want the objects to fall at a variable rate:

               

              obj.speed=minspeed+(maxspeed-minspeed)*Math.random();  // where minspeed and maxspeed are the obvious

               

              and when the object's created, add a loop to increase its _y property:

               

              obj.onEnterFrame=function(){

              this._y+=this.speed;

              if(this.hitTest(whatever)){

              // goto end frame.

              // probably, remove this

              }

              if(this._y>Stage.height+this._height){

              this.removeMovieClip();  // assuming this.getDepth()>=0

              }

              }

              1 person found this helpful
              • 4. Re: if(Key.isDown(Key.UP)) .. how do you get movment?
                Ned Murphy Adobe Community Professional & MVP

                I have given you all you need in terms of what you have to look further into, except for mentioning you will likely need to make use of enterframe event handling so that things happen continuously, like things falling/moving.  If you are looking for a solution to be custom tailored and handed over to you, that's not likely to happen, though some folks do like to hand over their own designs on occasion.

                 

                But you should be able to find tutorials if you search Google for the various things you want to accomplish.  Try using search terms like "AS2 falling objects tutorial" and others.  That particular set of terms will quickly land you some good information.  Google is one of the best tool you have at your disposal to learn Flash.