ActionScript MovieClip向左移动,但不向右移动

时间:2010-04-22 01:39:29

标签: actionscript actionscript-2 flash-cs4

我有一个带有实例名称为“mc”的影片剪辑的舞台。目前我有一个代码,假设左右移动播放器,当释放左或右键时,“mc”滑动一点。我遇到的问题是让“mc”向左移动是有效的,但是用于右边的确切代码却没有。

所有这些代码都出现在主舞台 - 第一帧

//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;
  }
 }
};

我只是不明白为什么当你按下左箭头时它的工作完全正常,但是当你按下右箭头时它没有响应。它实际上是相同的代码。

1 个答案:

答案 0 :(得分:0)

由于您的循环代码,您从未读过它:

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.
}

使用if(keyright)代替else if,它应该有效。