如何阻止精灵在半空中跳跃?动作脚本 3/Flash Pro CC 2015

How do I stop a sprite from jumping in midair? ActionScript 3/Flash Pro CC 2015

我是初学者 Flash/AS3 程序员,我有一个非常复杂的问题。如何防止精灵在半空中跳跃?我看过另一个与“sprite/jump in midair”相关的问题,但我个人无法弄清楚如何在 AS3 中做到这一点。感谢您的任何回复。 代码:

public class DocumentMain extends MovieClip {
    private var _vx: Number;
    import flash.utils.Timer;
    import flash.ui.Keyboard;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    private var _vy: Number;
    public function DocumentMain() {
        // constructor code
        _vx = 0;
        _vy = 0
        _startMarker.visible = false;
        this.addEventListener("enterFrame", a);
        stage.addEventListener("keyDown", b);
        stage.addEventListener("keyUp", c);
        _windows.addEventListener("enterFrame", handleCollision);
        
        function handleCollision( e:Event ):void
        {
        {
                var collisionWall:Boolean = false;
                if (wall.hitTestObject(_windows)) {
                    collisionWall = true;
                }
                if (collisionWall) {
                    while (collisionWall) {
                        _windows.x += 0.1; 
                        collisionWall = false;
                        if (wall.hitTestObject(_windows)) {
                        collisionWall = true;
                         }
                    }
                    _vx = 0;
                }
        }
    }
        function a(e:Event):void {
            _vy += 2;
            _windows.x += _vx;
            _windows.y += _vy;
            
            
            if (_vy > 0) {
                if (_windows.y > stage.stageHeight) {
                    _windows.x = _startMarker.x;
                    _windows.y = _startMarker.y;
                    _vy = 0;
                }
                
            else {
                var collision:Boolean = false;
                if (ground.hitTestObject(_windows)) {
                    collision = true;
                }
                if (collision) {
                    while (collision) {
                        _windows.y -= 0.1; 
                        collision = false;
                        if (ground.hitTestObject(_windows)) {
                        collision = true;
                         }
                    }
                    _vy = 0;
                }
                
            }
        }
    
    }
        
        function b(e:KeyboardEvent):void {
            var step:uint = 5
              switch (e.keyCode) {
                  case 37:
                      _windows.rotationY = -180;
                      _vx = -7;
                  break;
                  
                  case 39:
                      _windows.rotationY = 0;
                      _vx = 7;
                  break;
                  
                    case 38:
                     
                      _vy = -20;
                      break;
                                
                      

                }
        } 
        
        function c(e:KeyboardEvent):void {
            switch (e.keyCode) {
                case 37:
                case 39:
                    _vx = 0;
            }
            
        }

    }
}

您需要为玩家控制的精灵声明“在地面上”的条件(我希望这里称为 _windows),并根据该条件,让玩家改变垂直速度向上键,或忽略。

你的函数 a(e:Event) 中有一段设计用于处理地面碰撞的代码(很粗糙,但从我所看到的情况来看它确实有效),这个是设置“on the地面”标志,如果与地面发生碰撞,则将其设置为真。由于您的函数首先应用“重力”然后检查碰撞,因此它应该可以正确处理 cliffs/slopes 和跳跃。所以,你在你的游戏 class 中声明一个 Boolean 变量,比如“isOnGround”,首先将它设置为 false,然后检查与地面的碰撞,如果为 true,则将该变量设置为 true。然后,在 b() 函数(“keyDown”处理程序)中检查 var 是否为真,如果是,则允许跳转,因此您可以愉快地设置 _vy,否则什么都不做。

// adding only changed fragments
public class DocumentMain extends MovieClip {
    private var _isOnGround:Boolean; // the flag
    ...
        function a(e:Event):void {
            ...
                _isOnGround = false; // reset flag
                if (ground.hitTestObject(_windows)) {
                    collision = true;
                    _isOnGround = true; // we ARE on ground, rest isn't relevant here 
                }
            ...
        }       
    }
        
        function b(e:KeyboardEvent):void {
            ...
                    case 38:
                      if (_isOnGround) { // check here
                          _vy = -20;
                          _isOnGround = false; // just in case here
                      } // otherwise do nothing
                      break;