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

发布于 2024-08-29 23:19:50 字数 2686 浏览 10 评论 0原文

我有一个带有实例名称为“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;
  }
 }
};

我只是不明白为什么当您按向左箭头时它工作得很好,但当您按向右箭头时它没有响应。这实际上是相同的代码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗弃M 2024-09-05 23:19:50

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

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 ,它应该可以工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文