开发者

ActionScript MovieClip moves to the left, but not the right

开发者 https://www.devze.com 2022-12-27 22:48 出处:网络
I have a stage with a movie clip with the instance name of \"mc\". Currently I have a code that is suppose to move the player left and right, and when the left or right key is released, the \"mc\" sli

I have a stage with a movie clip with the instance name of "mc". Currently I have a code that is suppose to move the player left and right, and when the left or right key is released, the "mc" slides a little bit. The problem I'm having is that making the "mc" move to the left works, but the exact some code used for the right doesn't.

All of this code is present on the Main Stage - Frame One

//Variables

var mcSpeed:Number = 0;//MC's Current Speed
var mcJumping:Boolean = false;//if mc is Jumping
var mcFalling:Boolean = false;//if mc is Falling
var mcMoving:Boolean = false;//if mc is Moving
var mcSliding:Boolean = false;//if mc is sliding
var mcSlide:Number = 0;//Stored for use when creating slide
var mcMaxSlide:Number = 1.6;//Max Distance the object will slide.

//Player Move Function
p1Move = new Object();
p1Move = function (dir:String, maxSpeed:Number) {
 if (dir == "left" && _root.mcSpeed<maxSpeed) {
  _root.mcSpeed += .2;
  _root.mc._x -= _root.mcSpeed;
 } else if (dir == "right"开发者_如何学运维 && _root.mcSpeed<maxSpeed) {
  _root.mcSpeed += .2;
  _root.mc._x += _root.mcSpeed;
 } else if (dir == "left" && speed>=maxSpeed) {
  _root.mc._x -= _root.mcSpeed;
 } else if (dir == "right" && _root.mcSpeed>=maxSpeed) {
  _root.mc._x += _root.mcSpeed;
 }
}

//onEnterFrame for MC
mc.onEnterFrame = function():Void  {
 if (Key.isDown(Key.LEFT)) {
  if (_root.mcMoving == false && _root.mcSliding == false) {
   _root.mcMoving = true;
  } else if (_root.mcMoving == true && _root.mcSliding == false) {
   _root.p1Move("left",5);
  }
 } else if (!Key.isDown(Key.LEFT)) {
  if (_root.mcMoving == true && _root.mcSliding == false) {
   _root.mcSliding = true;
  } else if (_root.mcMoving == true && _root.mcSliding == true && _root.mcSlide<_root.mcMaxSlide) {
   _root.mcSlide += .2;
   this._x -= .2;
  } else if (_root.mcMoving == true && _root.mcSliding == true && _root.mcSlide>=_root.mcMaxSlide) {
   _root.mcMoving = false;
   _root.mcSliding = false;
   _root.mcSlide = 0;
   _root.mcSpeed = 0;
  }
 } else if (Key.isDown(Key.RIGHT)) {
  if (_root.mcMoving == false && _root.mcSliding == false) {
   _root.mcMoving = true;
  } else if (_root.mcMoving == true && _root.mcSliding == false) {
   _root.p1Move("right",5);
  }
 } else if (!Key.isDown(Key.RIGHT)) {
  if (_root.mcMoving == true && _root.mcSliding == false) {
   _root.mcSliding = true;
  } else if (_root.mcMoving == true && _root.mcSliding == true && _root.mcSlide<_root.mcMaxSpeed) {
   _root.mcSlide += .2;
   this._x += .2;
  } else if (_root.mcMoving == true && _root.mcSliding == true && _root.mcSlide>=_root.mcMax) {
   _root.mcMoving = false;
   _root.mcSliding = false;
   _root.mcSlide = 0;
   _root.mcSpeed = 0;
  }
 }
};

I just don't get why when you press the left arrow its works completely fine, but when you press the right arrow it doesn't respond. It is literally the same code.


you never read it because of your loop code:

if(keyleft){
    //catches all keyleft
}
else if(!keyleft){
    //katches all non key left
}
else if(keyright){
    //Never called because all eventualities have been covered by the above.
}

use if(keyright) instead of else if and it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号