代码之家  ›  专栏  ›  技术社区  ›  Koffee

Phaser 3中的跳槽(上下)平台

  •  2
  • Koffee  · 技术社区  · 1 年前

    我跟着 this tutorial 这对于在平台上跳跃和着陆非常有效。

    我已经设置了碰撞禁用技巧(这样玩家也可以跳过),效果也很好。

    我的问题是: 这适用于所有平铺层。

    如何检查角色所站的平铺是否实际上来自平铺贴图中的特定层?

    我当前的设置:

    // Import map from Tiled
    this.map = this.make.tilemap({ key: 'myBeautifulMap' });
    this.platform = this.map.createLayer('platform', this.tileset);
    
    // Apply collision to ALL tiles from this Layer
    this.platform.setCollisionByExclusion(-1, true); 
    
    // disable one side collision so character can jump up trough it
    this.platform.forEachTile(tile => {
      if (tile.index > 0){
        tile.setCollision(false, false, true, false);
      }
    });
    
    // Allow jump down
    // This part shoudl check if the tiles the character is standing on are part of the 'platform' Layer
    if (this.keyboard.S.isDown) {
      this.body.checkCollision.down = false;
      this.scene.time.addEvent({ 
        delay: 400,
        callback:  () => {
          this.body.checkCollision.down = true;
        },
        callbackScope: this 
      }); 
    }
    
    1 回复  |  直到 1 年前
        1
  •  2
  •   winner_joiner    1 年前

    一个简单的解决方案是添加一个 collider 事件在 create 每一层的功能。

    每层一行代码如下:

    this.physics.add.collider(player, layer, (p, t) => this.playerOnTileLayer = t.layer);
    

    现在,当玩家落在一个图块上时,变量将被设置。现在,您可以检查玩家是否在该特定层上。

    大致如下:

    if (this.keyboard.S.isDown && this.playerOnTileLayer == this.platform) {
       ...
    }
    

    如果玩家可以降落在其他不是磁贴的游戏对象上,你可以重置变量( this.playerOnTileLayer = null; )例如在按键上或在每次与非拼贴块碰撞时。 只是为了保存,变量总是设置正确的。

    推荐文章