与关键听众对角跳跃

时间:2016-11-11 21:39:09

标签: java animation sprite keylistener

我的角色能够很好地左右移动。我试过跳,我似乎无法搞清楚。此外,我很难让角色对角跳跃并让它看起来不错。任何帮助都会非常感激我也在考虑让计时器运行只允许用户每5秒跳一次。

 public void actionPerformed(ActionEvent e) {
repaint();
x+=velX;
height += velY;
if (height >= totalHeight){//total hieght is the max height the character can travel however
//If the user spams the W key the character will just keep going
  y+=velY;
}else{
  y+=gravity;
}
firex += velF;
if (y < 2){
  y = 3;
}
if (y >530){
  y = 531;
}   
}
public void right(){
velX = 20;
}
 public void left(){
 velX = -20; 
 }
 public void fire(){
 velF = 15;//This is a projectile//THis also should be on a timer, for every 10 seconds it can shoot once
 }
 public void jump(){
  velY = -30;
  }


public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
//NEED HELP WITH JUMP LEFT AND JUMP RIGHT//
//I WANT TO MAKE IT SO THAT HE DOESNT LOOK WIERD?GLITCHY//
//FIND OUT ABOUT COUNTER FOR ENEMIES NEARBY,LIKE IF AN ENEMY IS NEAR YOU FOR 2 SEC YOU LOSE HEALTH
if(code == KeyEvent.VK_Z){
  firex = x;
  firey = y;
  fire();
  fireIs = true;
  isMovingRight = false;
  isNotMoving = false;
  isPunch = false;
  isMovingLeft = false;
  isKick  = false;
  isJump = false;
   isSlam = false;
}
if (code == KeyEvent.VK_P){
  System.out.println("Paused");
  stopAnimation();
}
if (code == KeyEvent.VK_A){
  left();
  isMovingRight = false;
  isNotMoving = false;
  isPunch = false;
  isMovingLeft = true;
  isKick  = false;
  isJump = false;
   isSlam = false;
}

if (code == KeyEvent.VK_F){
  isPunch = true;
  isNotMoving = false;
  isMovingRight = false;
  isMovingLeft = false;
  isKick  = false;
  isJump = false;
   isSlam = false;
}

if(code == KeyEvent.VK_D){
  right();
  isMovingRight = true;
  isNotMoving = false;
  isPunch = false;
  isMovingLeft = false;
  isKick  = false;
  isJump = false;
   isSlam = false;
}
if(code == KeyEvent.VK_Q){
  isMovingRight = false;
  isNotMoving = false;
  isPunch = false;
  isMovingLeft = false;
  isKick  = true;
  isJump = false;
   isSlam = false;
}
if(code == KeyEvent.VK_W){
  jump();
  isMovingRight = false;
  isNotMoving = false;
  isPunch = false;
  isMovingLeft = false;
  isKick  = false;
  isJump = true;
   isSlam = false;
}
            if (code == KeyEvent.VK_E){
  isPunch = false;
  isNotMoving = false;
  isMovingLeft = false;
  isMovingRight = false;
  isKick  = false;
  isJump = false;
  isSlam = true;
  }
  }
  public void keyTyped(KeyEvent e){}
  public void keyReleased(KeyEvent e){
  int code = e.getKeyCode();

  if(code == KeyEvent.VK_D){
  isMovingRight = false;
  isNotMoving = true;
  isPunch = false;
  isMovingLeft = false;
  isKick  = false;
  isJump = false;
   isSlam = false;
 // System.out.println(getX());
}
if (code == KeyEvent.VK_P){
  startAnimation();
}
if (code == KeyEvent.VK_A){
  isMovingRight = false;
  isNotMoving = true;
  isPunch = false;
  isMovingLeft = false;
  isKick  = false;
  isJump = false;
   isSlam = false;
}
if(code == KeyEvent.VK_Q){
  isMovingRight = false;
  isNotMoving = true;
  isPunch = false;
  isMovingLeft = false;
  isKick  = false;
  isJump = false;
   isSlam = false;
}
if (code == KeyEvent.VK_F){
  isPunch = false;
  isNotMoving = true;
  isMovingLeft = false;
  isMovingRight = false;
  isKick  = false;
  isJump = false;
   isSlam = false;
}
        if (code == KeyEvent.VK_E){
  isPunch = false;
  isNotMoving = true;
  isMovingLeft = false;
  isMovingRight = false;
  isKick  = false;
  isJump = false;
  isSlam = false;
}
if(code == KeyEvent.VK_Z){
  isMovingRight = false;
  isNotMoving = true;
  isPunch = false;
  isKick  = false;
  isJump = false;
   isSlam = false;
}
if(code == KeyEvent.VK_W){
  if (y > 100){
    isMovingRight = false;
    isNotMoving = true;
    isPunch = false;
    isMovingLeft = false;
    isKick  = false;
    isJump = false;
     isSlam = false;
    height = 0;
  }else if (y < 540){
    isMovingRight = false;
    isNotMoving = false;
    isPunch = false;
    isMovingLeft = false;
    isKick  = false;
    isJump = true;
     isSlam = false;
    height = 0;
  }
}
velX = 0;

}

0 个答案:

没有答案