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

Time:10-06

I am a beginner Flash/AS3 programmer and I have a very sophisticated problem. How can I prevent a sprite from jumping in midair? I've seen the other question related to "sprite/jump in midair", but I personally cannot figure out how to do that in AS3. Thank you for any response. Code:

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

    }
}

CodePudding user response:

You need to declare the condition "on the ground" for the player-controlled sprite (I expect from here it's called _windows), and based on that condition, either let the player change vertical speed with up key, or ignore.

You have here a block of code designed to handle ground collision (crude but it does work from what I'm seeing) in your function a(e:Event), this one is the place to set the "on the ground" flag, if there was a collision with ground, set that to true. Since your function first applies "gravity" then checks for collision, it should work properly handling cliffs/slopes together with jumping. So, you declare a Boolean variable in your game class, say "isOnGround", set it to false at first, then check collision with ground, if true, set that var to true. Then, at b() function (a "keyDown" handler) you check whether the var is true, and if yes, jumping is allowed so you happily set your _vy, otherwise you do nothing.

// 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;
  • Related