Как создать анимацию прыжка, только когда игрок касается земли? - PullRequest
0 голосов
/ 02 апреля 2020

Резюме

Я делаю простую игру исключительно для учебных целей. Игрок будет непрерывно прыгать с одной платформы на другую. Игрок всегда будет двигаться, подпрыгивая.

У меня есть этот спрайт:

SpriteSheet

Когда игрок приближается к земле, я хотел бы, чтобы рамка в КРАСНОМ цвете появилась.

Когда игрок упадет на землю, я бы хотел, чтобы кадры в ЖЕЛТЫЙ появлялись и играли.

Моя попытка

Fail Attempt

Исходный код

/// <reference path="../defs/phaser.d.ts" />

class MainGame extends Phaser.Scene {
  constructor() {
    super("MainGame");
  }

  preload() {
    this.load.image("bg1", "assets/bg-level1.png");
    this.load.image('ground', 'assets/platform.png');
    this.load.spritesheet('dude', 'assets/final-jump.png', {
      frameWidth: 118,
      frameHeight: 118
    });
  }

  create() {

    this.setupBackground();
    this.setupPlayer();

    this.physics.add.collider(this.player, this.platforms);

  }

  update() {

  }



  //---------------------------------
  // CREATE RELATED FUNCTIONS
  //---------------------------------

  setupBackground() {
    this.background = this.add.image(0, 0, "bg1");
    this.background.setOrigin(0, 0);

    //this.background.setInteractive();
    this.background.setAlpha(.2, .2, .2, .2);

    //  The platforms group contains the ground and the 2 ledges we can jump on
    this.platforms = this.physics.add.staticGroup();

    //  Here we create the ground.
    //  Scale it to fit the width of the game (the original sprite is 400x32 in size)
    this.platforms.create(400, 568, 'ground').setScale(2).refreshBody();

  }


  setupPlayer() {

    this.player = this.physics.add.sprite(100, 283, 'dude');

    //  Player physics properties. Give the little guy a slight bounce.
    this.player.setBounce(0.9);

    // Make him collide with things
    this.player.setCollideWorldBounds(true);

    // Collision Handler
    this.physics.add.overlap(this.player, this.platforms, this.showJump, null, this);

    // ANIMATIONS
    this.anims.create({
      key: 'jump-up',
      frames: this.anims.generateFrameNumbers('dude', {
        start: 1,
        end: 2
      }),
      frameRate: 10,
      repeat: -1
    });

  }


  showJump() {
    this.player.anims.play('jump-up', true);
  }



}

Демонстрационная ссылка

Вы можете увидеть это в действии здесь .

Просто кликните по пустым сценам, чтобы попасть в игру.

Вопросы

Я совсем новичок в Phaser. Я хотел бы сменить раму для нанесения покрытия, когда он приближается к земле и когда он касается земли. Я хочу также обеспечить плавную анимацию прыжка, а в настоящее время это не так. Можем ли мы сделать подростка, может быть? Я не уверен Мои исследования и попытки пока не увенчались успехом.

Наконец, почему ноги моего игрока не касаются платформы и не вызывают столкновение? Кажется, немного выключен.

Спасибо, что посмотрели.

...