如何在保持跳跃状态时不让我的角色飞起来

问题描述 投票:3回答:1

我正在使用JavaScript开发游戏。现在,在保持跳跃的同时,我的角色也在不断跳跃。我希望他不要继续跳来跳去。我如何让他停止跳跃并在大约2秒钟后摔倒,即使保持了跳跃?我有重力,我放开后他就倒下了。。但是我希望他自己落下。

玩家

import { detectCollisionLeft } from './collision';
import Map from './map';
import Entity from './entity';

class Player extends Entity {
  constructor(x, y) {
    super(x, y)
    this.width = 24,
      this.height = 24,
      this.color = "blue",
      // this.pos = {
      //   x: 20, // change to level start later
      //   y: 20
      // }
    this.velX = 0;
    this.maxVelX = 3;
    this.velY = 0;
    this.maxVelY = 8;
    this.gravity = 3;
    this.jumping = false
    // this.topLeft = {
    //   x: this.pos.x,
    //   y: this.pos.y - this.height
    // }
    // this.botRight = {
    //   x: this.pos.x + this.width,
    //   y: this.pos.y
    // }

    // this.top = this.pos.y
    // this.left = this.pos.x
    // this.bot = this.pos.y + this.height;
    // this.right = this.pos.x + this.width
  }

  draw(ctx) {
    // console.log('j')
    ctx.fillStyle = this.color;
    ctx.fillRect(this.pos.x, this.pos.y, this.width, this.height)
  }

  moveLeft() {
    // debugger
    // if (this.map.collidingWithMap(this.topLeft)) {
    //   console.log('hi')  
    //   return
    // }
    this.velX = -this.maxVelX
  }

  moveRight() {
    this.velX = this.maxVelX
  }

  jump() {
    // if (this.pos.y === 600 - this.height)
      if (!this.jumping) {
        this.velY = -this.maxVelY
        this.jumping = true
    }
  }

  comeDown() {
    this.jumping = false
    this.velY = this.gravity
  }

  stop() {
    this.velX = 0;
  }

  update(dt) {
    // console.log(this.pos)
    if (!dt) return

    this.pos.x += this.velX
    this.pos.x += this.velX
    this.pos.y += this.velY + this.gravity
    // if (this.pos.y + this.height > 600) {
    //   this.pos.y = 600 - this.height
    // }

  }





}

export default Player;

Controller

import Player from './player1';
import Map from './map';

class Controller {
  constructor(player, map, ctx) {
    this.player = player;
    this.map = map;
    this.ctx = ctx
    // this.keyboardHandlers = this.keyboardHandlers.bind(this);

    // this.keyboardHandlers();


  // keyboardHandlers() {

    // document.addEventListener("keypress", (e) => {
    //   if (e.keyCode === 38 || 87) {
    //     this.player.jumping = false;
    //     this.player.jump();
    //     // break;
    //   }
    // })

    document.addEventListener("keydown", (e) => {
      switch (e.keyCode) {
        case 38 && 87: // up arrow
          // console.log("w")

          if (this.player.jumping === false) {
            this.player.jump();
          }
          break;

        // case 40 && 83: // down arrow

        //   this.player.move("down");
        //   // console.log(this.player.pos)
        //   this.player.draw(this.ctx)
        //   break;

        case 37 && 65: // left arrow

          this.player.moveLeft();
          break;

        case 39 && 68: // right arrow

          this.player.moveRight();
          break;

        case 32: // space
          // debugger
          this.map.flipMap();

        // case 38 && 39: // doesnt work
        //   console.log('upright')
        //   break;

        // case 13: // enter
        //   if (this.game.menu === true) {

        //   } else {

        //   }

        default:
          break;
      }
    })

    document.addEventListener("keyup", (e) => {
      // debugger
      switch (e.keyCode) {
        case 38 && 87: // up arrow
          // console.log("w")
          if (this.player.jumping)
            this.player.comeDown()
          break;

        // case 40 && 83: // down arrow

        //   if (this.player.velY > 0)
        //     this.player.stop();
        //   break;

        case 37 && 65: // left arrow
          if (this.player.velX < 0)
            this.player.stop();
          break;

        case 39 && 68: // right arrow
          if (this.player.velX > 0)
            this.player.stop();
          break;

        // case 32: // space
        //   this.map.flipMap(this.ctx);

        // case 38 && 39: // doeswnt work
        //   console.log('upright')
        //   break;

        // case 13: // enter
        //   if (this.game.menu === true) {

        //   } else {

        //   }

        default:
          break;
      }
    })
  }
  // }
}

export default Controller;

游戏

import Player from './player';
import Map from './map';
import Controller from './controller';
import Tile from './tile'
import { buildLevel, levels } from './level'
import Collision from './collision';

class Game {
  constructor(ctx) {
    this.ctx = ctx;
    // this.map = new Map(ctx);
    // // debugger
    // this.tile = new Tile()
    // this.map = new Map(this.ctx)
    this.map = new Map(levels[1].tiles, this, this.color);
    this.player = new Player(20, 20);
    this.controller = new Controller(this.player, this.map, this.ctx, this);
    this.collision = new Collision(this.map)
    this.tiles = [];
    this.color = "black"
    // // this.controller = new Controller(this.player, this.map, ctx)
    // // this.keyboardHandlers(ctx)
    // this.render(ctx);
  };

  draw(ctx) {
    // debugger
    ctx.clearRect(0, 0, 1000, 600)
    // debugger
    // this.tiles.forEach(tile => tile.draw(ctx))
    this.player.draw(ctx)
    this.map.tiles.forEach(tile => tile.draw(ctx))

    // this.map.render(ctx)
  }

  start() {
    // debugger
    // this.tiles = buildLevel(levels[1].tiles, this)
    this.map.create(levels[1].tiles, this);
    this.lastTime = 0;
    // this.controller.keyboardHandlers();
    requestAnimationFrame(this.animate.bind(this))
  }

  animate(time) {
    const dt = time - this.lastTime;
    this.draw(this.ctx)
    // this.tiles.forEach(tile => tile.update())
    this.player.update(dt)
    this.collision.isColliding(this.player)
    // this.lastTime = time;

    requestAnimationFrame(this.animate.bind(this))
  }

  // draw(ctx) {
  //   console.log('cear')
  //   ctx.clearRect(0, 0, 1000, 600)
  // }

  // render(ctx) {
  //   // debugger
  //   ctx.clearRect(0, 0, 1000, 600)

  //   this.map.render(ctx)
  //   this.player.draw(ctx)
  // }
}

const CONSTANTS = {
  GRAVITY: 0.4
}

export default Game;
javascript
1个回答
1
投票
//On key up case 38 && 87: // up arrow // console.log("w") if (this.player.jumping){ clearTimeout(this._timeout) this.player.comeDown() } break;

此外,(与此问题无关),case 38 && 87:不能按您预期的方式工作:

由于表达式38 && 87等于87(由于[C​​0]),因此它不适用于38。
要写38或87,必须写两个折叠的情况:

&&


0
投票
//On key up case 38 && 87: // up arrow // console.log("w") if (this.player.jumping){ clearTimeout(this._timeout) this.player.comeDown() } break;

此外,(与此问题无关),case 38 && 87:不能按您预期的方式工作:

由于表达式38 && 87等于87(由于[C​​0]),因此它不适用于38。
要写38或87,必须写两个折叠的情况:

&&

© www.soinside.com 2019 - 2024. All rights reserved.