6 Replies Latest reply on Jun 22, 2010 12:26 PM by Equx02

    Car Game - Different grounds.

    Equx02

      Hi.

       

      I'm making a "Car Game", that will be a part of a bigger web game.

       

      Now I have different grounds that my car will drive on.

       

      Now I'm using a very long code:

       

      onClipEvent (enterFrame) {
      this._y += speed*Math.sin(_rotation/57);
      this._x += speed*Math.cos(_rotation/57);
      speed *= 0.8;
      }
      onClipEvent (enterFrame) {
      if(this.hitTest(_root.wall)){
      speed = 0;
      }else if(this.hitTest(_root.wall1)){
      speed = 0;
      }else if(Key.isDown(Key.UP) && this.hitTest(_root.Grass)) {
      speed = 10;
      }else if (Key.isDown(Key.UP)) {
      speed = 15;
      } else {
      speed = 0;
      }
      }

      onClipEvent (enterFrame) {
      if(Key.isDown(Key.DOWN) && this.hitTest(_root.Grass)) {
              speed = -2.5;
      } else if (Key.isDown(Key.DOWN)) {
      speed = -5;
      }
      }
      // Turn Left (Forward)
      onClipEvent (enterFrame) {
      if (Key.isDown(Key.UP) and Key.isDown(Key.LEFT)) {
      _rotation -= 10;
      }
      }
      // Turn Right (Forward)
      onClipEvent (enterFrame) {
      if (Key.isDown(Key.UP) and Key.isDown(Key.RIGHT)) {
      _rotation += 10;
      }
      }
      // Turn Left (Backward)
      onClipEvent (enterFrame) {
      if (Key.isDown(Key.DOWN) and Key.isDown(Key.LEFT)) {
      _rotation -= 10;
      }
      }
      // Turn Right (Backward)
      onClipEvent (enterFrame) {
      if (Key.isDown(Key.DOWN) and Key.isDown(Key.RIGHT)) {
      _rotation += 10;
      }
      }
      // Turn Left (Forward, Grass)
      onClipEvent (enterFrame) {
      if (Key.isDown(Key.UP) and Key.isDown(Key.LEFT)) {
      _rotation -= 2.5;
      }
      }
      // Turn Right (Forward, Grass)
      onClipEvent (enterFrame) {
      if (Key.isDown(Key.UP) and Key.isDown(Key.RIGHT)) {
      _rotation += 2.5;
      }
      }
      // Turn Left (Backward, Grass)
      onClipEvent (enterFrame) {
      if (Key.isDown(Key.DOWN) and Key.isDown(Key.LEFT)) {
      _rotation -= 2.5;
      }
      }
      // Turn Right (Backward, Grass)
      onClipEvent (enterFrame) {
      if (Key.isDown(Key.DOWN) and Key.isDown(Key.RIGHT)) {
      _rotation += 2.5;
      }
      }

       

      I use this code so that it would go slower on "Grass" but this will be a very long code if I have multiple "grounds". Anything shorter?

      and it also needs to have to code so it does not turn when you not press UP or DOWN.

       

      // Kevin

        • 1. Re: Car Game - Different grounds.
          kglad Adobe Community Professional & MVP

          you should be using one master onEnterFrame loop.

          • 2. Re: Car Game - Different grounds.
            Equx02 Level 1

            Ok. Now I have edited the code.

            Now two questions:

            1) How about the different grounds? Do I need to set this code for every different ground?

            // Turn Left (Forward, Grass)
            if (Key.isDown(Key.UP) and Key.isDown(Key.LEFT)) {
            _rotation -= 2.5;
            }

            // Turn Right (Forward, Grass)
            if (Key.isDown(Key.UP) and Key.isDown(Key.RIGHT)) {
            _rotation += 2.5;
            }

            // Turn Left (Backward, Grass)
            if (Key.isDown(Key.DOWN) and Key.isDown(Key.LEFT)) {
            _rotation -= 2.5;
            }

            // Turn Right (Backward, Grass)
            if (Key.isDown(Key.DOWN) and Key.isDown(Key.RIGHT)) {
            _rotation += 2.5;
            }

            2) I have made obecjts, but I would wan't them to be more like in real life.

            Now when you hit them, the car goes a bit in to them, I would wan't it to stop right beside them or if the speed is high go back a bit.

            Samething if I press DOWN and go backwards, I can drive through them.

             

            // Kevin

            • 3. Re: Car Game - Different grounds.
              kglad Adobe Community Professional & MVP

              if you want different properties for the various "grounds", assign those properties and use those instead of hard-coding.  for example:

               

              var grassRot:Number = 2.5;

              var dirtRot:Number = 3;

              var roadRot:number = 4;

               

              var grassSpeed:Number=

              //etc

               

              // at the start of each loop, assign the parameters used in your if-statements:


              if(ground=="grass"){

              rot=grassRot;

              speed=grassSpeed;

              } else if(ground=="dirt"){

              rot=dirtRot;

              speed=dirtSpeed;

              } else if(ground=="road"){

              rot=roadRot;

              speed=roadSpeed;

              }




              if (Key.isDown(Key.UP) and Key.isDown(Key.LEFT)) {
              _rotation -= rot;
              }

               


              if (Key.isDown(Key.UP) and  Key.isDown(Key.RIGHT)) {
              _rotation += rot;
              }

               


              if (Key.isDown(Key.DOWN) and Key.isDown(Key.LEFT)) {
              _rotation  -= rot;
              }

               


              if  (Key.isDown(Key.DOWN) and Key.isDown(Key.RIGHT)) {
              _rotation += 2.5;

              }

               

              // and after a pos hittest with an obstacle, return the car's position to its previous postion before impact and adjust its direction

              • 4. Re: Car Game - Different grounds.
                Equx02 Level 1

                Hi again.

                 

                Now i've edited the code, but now I can't turn...

                onClipEvent (enterFrame) {

                this._y += speed*Math.sin(_rotation/57);
                this._x += speed*Math.cos(_rotation/57);
                speed *= 0.8;

                var grassRot:Number = 2.5;
                var dirtRot:Number = 1;
                var roadRot:Number = 4;

                var grassSpeed:Number = 10;
                var dirtSpeed:Number = 5;
                var roadSpeed:Number = 15;

                if(ground=="grass"){
                rot=grassRot;
                speed=grassSpeed;
                } else if(ground=="dirt"){
                rot=dirtRot;
                speed=dirtSpeed;
                } else if(ground=="road"){
                rot=roadRot;
                speed=roadSpeed;
                }

                if(this.hitTest(_root.wall)){
                speed = 0;
                }else if(this.hitTest(_root.wall1)){
                speed = 0;
                }else if (Key.isDown(Key.UP)) {
                speed = 15;
                }else if(Key.isDown(Key.DOWN)) {
                speed = -10                            
                } else {
                speed = 0;
                }

                if (Key.isDown(Key.UP) and Key.isDown(Key.LEFT)) {
                _rotation -= rot;
                }

                if (Key.isDown(Key.UP) and  Key.isDown(Key.RIGHT)) {
                _rotation += rot;
                }

                if (Key.isDown(Key.DOWN) and Key.isDown(Key.LEFT)) {
                _rotation  -= rot;
                }

                }

                 

                I can't turn to any direction..

                • 5. Re: Car Game - Different grounds.
                  kglad Adobe Community Professional & MVP

                  are you defining the ground variable?  and:

                   

                  var grassRot:Number = 2.5;
                  var dirtRot:Number = 1;
                  var  roadRot:Number = 4;

                   

                  var grassSpeed:Number = 10;
                  var  dirtSpeed:Number = 5;
                  var roadSpeed:Number = 15;

                   

                  shouldn't be in your loop.

                  • 6. Re: Car Game - Different grounds.
                    Equx02 Level 1

                    Now when I moved the code outside the loop I get the error:

                    Statement must appear within on/onClipEvent handler