手把手一步一步教你使用Java开发一个大型街机动作闯关类游戏18玩家攻击动画实现

Player攻击动画实现

Player.java部分代码

public class Player extends AttackAbleObject{
    private int _leftLocked = 0;
    private int _rightLocked = 0;

    private boolean _attkKeyUp = true;

    private boolean _attack = false;
    private boolean _rdyToAttack = true;

    private final int _hitDelay = 320;
    private int _hitDelayTimer = _hitDelay;


    public void update(Graphics2D g){
        _animator.show(g);
        _handleMovement();
        handleAttack();
    }

    public Transform getTransform(){
        return _animator;
    }

    public void attack(){
        if(_rdyToAttack){
            _animator.setAnimation("attk");
            _attack = true;
            _rdyToAttack = false;
            _hitDelayTimer = _hitDelay;
        }
    }

    void handleAttack(){
        if(_attack){
            _hitDelayTimer -= Config.TIMER_DIF;
            if(_hitDelayTimer<=0){
                _rdyToAttack = true;
                _attack = false;
                _hitDelayTimer = _hitDelay;
                _animator.setAnimation("idle");
            }
        }
    }

    private void _handleMovement(){

        if(_input.isKeyDown(Config.PLAYER1_MELEE) && _attkKeyUp) {
            attack();
            _attkKeyUp = false;
            _keyPressed = true;
        }

        if(_input.isKeyUp(Config.PLAYER1_MELEE) && !_attkKeyUp){
            _attkKeyUp = true;
        }

        if(!_keyPressed && !_animator.isPlaying("idle") && !_animator.isPlaying("attk")){
            _animator.setAnimation("idle");
            _animator.translate(0,0);
        }

_handleMovement方法判断当F键被按下的时候,调用attack()方法;
attack()方法主要是设置攻击动画_animator.setAnimation("attk");
另外,update方法中调用handleAttack方法;
_hitDelayTimer = _hitDelay = 320毫秒
TIMER_DIF = 15.0毫秒
每一帧

_hitDelayTimer -= Config.TIMER_DIF;

320/15大约等于21;1秒60帧的话,攻击动画大约播放21/60=0.35秒。

如果您迷路了,请参考完整源码:

项目源码

项目源码

你可能感兴趣的:(手把手一步一步教你使用Java开发一个大型街机动作闯关类游戏18玩家攻击动画实现)