Skip navigation
Currently Being Moderated

speed and postion

Aug 18, 2012 5:36 AM

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

 
Replies
  • kglad
    72,340 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 18, 2012 7:24 AM   in reply to thunderxlight11111

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

     
    |
    Mark as:
  • kglad
    72,340 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 18, 2012 9:08 PM   in reply to thunderxlight11111

    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);

                }

            }

        }

    }

     
    |
    Mark as:
  • kglad
    72,340 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 25, 2012 7:08 AM   in reply to thunderxlight11111

    you're welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points