5 Replies Latest reply: Aug 25, 2012 7:08 AM by kglad RSS

    speed and postion

    thunderxlight11111 Community Member

      hi , how are you all guys , i hope you all fine

      first : sorry because iam asking alot in this forum

      here is the problem

       

      i made a game and i know how to move the hero left up etc..

      also i know how to change his speed

      but ..

      i dont know how to change hi speed when i use electrical shield speel

      i tried with variables vs and vx without anything

      what i mean that i want to change hero speed without change his postion

      like wen you use this spell  electrical shield his speed will improve

      for example when you move  left or right , your speed 3 or 4 px per frame , but when you use this spell and moves the hero to the left or right  his speed should be 30

       

      here is the hero class

       

      package  {

      import flash.display.MovieClip;
      import flash.events.KeyboardEvent;
      import flash.ui.Keyboard;
      import flash.events.Event;
      import flash.sensors.Accelerometer;

      public class HERO extends MovieClip {
         var ELECTRICALSHIELD:electricalshield  = new electricalshield
        var vx:int
        var vs:int
       
        public function HERO() {
         addEventListener(Event.ADDED_TO_STAGE,onAddedToStage)
        
        }
        public function onAddedToStage(event:Event):void
        {
      addEventListener(Event.ENTER_FRAME,onEnterFrame )
      stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown)
         stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp)
         addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage)
         gotoAndStop(4)
        
        
        

      }
      public function onRemovedFromStage(event:Event):void{
      removeEventListener(Event.ADDED_TO_STAGE,onAddedToStage)
      removeEventListener (Event.ENTER_FRAME,onEnterFrame )
         removeEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage)
         trace("player removed")
      }
      public function onEnterFrame(event:Event):void{

      x += vx
      }
      public function onKeyDown(event:KeyboardEvent):void{

      if (event.keyCode == Keyboard.RIGHT){
      gotoAndStop(2)
      vx += 1
      vs = 10
      vx *= vs

      }

      if (event.keyCode == Keyboard.LEFT){
        gotoAndStop(1)
        vx -= 1
        vs = 10
        vx *= vs
       
      }
      if (event.keyCode == Keyboard.UP){
        gotoAndStop(1)
        y = 10
      }
      if(event.keyCode == Keyboard.Q  ){
        vs = 30
      addChild(ELECTRICALSHIELD)

      }
      }


       

      public function onKeyUp (event:KeyboardEvent):void{
      if(event.keyCode == Keyboard.Q ){
      if(ELECTRICALSHIELD.stage){
      removeChild(ELECTRICALSHIELD)
      vx = 0
      }
      }
      if(event.keyCode == Keyboard.LEFT){
          vx =0
      gotoAndStop(3)
      }
      else if (event.keyCode == Keyboard.RIGHT){
         vx = 0
         gotoAndStop(4)
      }
      }
      }
      }

       

       

      thank you again for watching my discuss

        • 1. Re: speed and postion
          kglad MVP

          if the change in x per unit time determines speed, changing vx will change speed.

          • 2. Re: speed and postion
            thunderxlight11111 Community Member

            sorry , i realy cant get you very well , i just want to add more speed to the hero when he clicks that spell

            i mean what should i put here

             

            if(event.keyCode == Keyboard.Q ){

            vs = 30

            addChild(ELECTRICALSHIELD)

             

            }

            if i put vx = 30 he will gonna to the left also if i put -30 he will go to the right  and i dont want that i want it only improve the hero speed so when i click right  or left button with q button the speed improve to 30 but that not gonna because when i do that this command

            if (event.keyCode == Keyboard.RIGHT){ vx = 3 }  that will return the speed to 3 

            , yeah also this var (vs) is useless i just added  it because i was trying to find a solution but i couldn,t 

             

            thank you mr.kglad for your replay

            • 3. Re: speed and postion
              kglad MVP

              start with this and tweak it to suit your needs:

               

              package {

               

                  import flash.display.MovieClip;

                  import flash.events.KeyboardEvent;

                  import flash.ui.Keyboard;

                  import flash.events.Event;

                  import flash.sensors.Accelerometer;

               

                  public class HERO extends MovieClip {

                      var ELECTRICALSHIELD:electricalshield=new electricalshield  ;

                      var speed:int=3;

                      var dir:int;

                      public function HERO() {

                          addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);

                      }

                      public function onAddedToStage(event:Event):void {

                          addEventListener(Event.ENTER_FRAME,onEnterFrame );

                          stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);

                          stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);

                          addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage);

                          gotoAndStop(4);

                      }

                      public function onRemovedFromStage(event:Event):void {

                          removeEventListener(Event.ADDED_TO_STAGE,onAddedToStage);

                          removeEventListener(Event.ENTER_FRAME,onEnterFrame );

                          removeEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage);

                          trace("player removed");

                      }

                      public function onEnterFrame(event:Event):void {

                          x+=dir*speed;

                      }

                      public function onKeyDown(event:KeyboardEvent):void {

                          if (event.keyCode==Keyboard.RIGHT) {

                              gotoAndStop(2);

                              dir=1;

                          }

               

                          if (event.keyCode==Keyboard.LEFT) {

                              gotoAndStop(1);

                              dir=-1;

                          }

                          if (event.keyCode==Keyboard.UP) {

                              gotoAndStop(1);

                              y=10;

                          }

                          if (event.keyCode==Keyboard.Q) {

                              speed=10;

                              addChild(ELECTRICALSHIELD);

                          }

                      }

                      public function onKeyUp(event:KeyboardEvent):void {

                          if (event.keyCode==Keyboard.Q) {

                              if (ELECTRICALSHIELD.stage) {

                                  removeChild(ELECTRICALSHIELD);

                                  speed=3;

                              }

                          }

                          if (event.keyCode==Keyboard.LEFT) {

                              gotoAndStop(3);

                          } else if (event.keyCode == Keyboard.RIGHT) {

                              gotoAndStop(4);

                          }

                      }

                  }

              }

              • 4. Re: speed and postion
                thunderxlight11111 Community Member

                sorry because i late , and thank you for your help

                • 5. Re: speed and postion
                  kglad MVP

                  you're welcome.