javascript - Phaser 3 碰撞器剪辑

标签 javascript phaser-framework clipping collider

所以我对 Phaser 有点陌生,我发现很难找到关于这个主题的任何好的信息。我的问题是我可以将碰撞器推向彼此(参见 gif)。

https://imgur.com/KI1RJQA.gif “我的意思是”

所以现在我有一个系统,我可以跟踪玩家的最后一个有效位置,如果它们发生碰撞,我会将它们设置回原来的位置。但我认为必须有更好的方法来做到这一点,所以这就是我来这里的原因。

let TopWall = this.add.tileSprite(400, 100, 1800, 100, 'wall');
this.physics.add.existing(TopWall);

this.walls = this.add.group();
this.walls.addMultiple([TopWall]);

this.walls.children.iterate(function(wall) {
            wall.body.immovable = true;
        })

this.player = this.physics.add.sprite(300, 300, 'idel', 0);

this.physics.add.collider(this.player, this.walls,(player, wall) =>  {
            // this.player.x = this.player.lastPos.x;
            // this.player.y = this.player.lastPos.y;
})

顺便说一句,我使用街机物理。

最佳答案

var config = {
    type: Phaser.AUTO,
    parent: 'phaser-example',
    width: 800,
    height: 600,    loader: {
      baseURL: 'https://raw.githubusercontent.com/nazimboudeffa/assets/master/',
      crossOrigin: 'anonymous'
    },
    scene: {
        preload: preload,
        create: create,
        update: update
    },
    physics: {
        default: 'arcade'
    }
};

var game = new Phaser.Game(config);

var topWall;
var player;
var alien1;

function preload ()
{
  this.load.image('dude', 'sprites/phaser-dude.png');
  this.load.image('alien1', 'sprites/phaser-alien.png');
  this.load.image('wall', 'sprites/block.png');
}

function create ()
{ 
  topWall = this.add.tileSprite(0, 0, 2000, 95, 'wall');
  this.physics.add.existing(topWall);
  topWall.body.immovable = true;
  
  player = this.physics.add.sprite(300, 100, 'dude');
  alien1 = this.physics.add.sprite(400, 100, 'alien1');
  
  cursors = this.input.keyboard.createCursorKeys();
 
  player.body.setCollideWorldBounds(true);
  this.physics.add.collider(player, topWall);
  this.physics.add.collider(player, alien1);
  
}

function update ()
{ 
  if (cursors.left.isDown)
  {
      player.setVelocityX(-160);
  }
  else if (cursors.right.isDown)
  {
      player.setVelocityX(160);
  }
  else if (cursors.down.isDown)
  {
      player.setVelocityY(160);

  } 
  else if (cursors.up.isDown)
  {
      player.setVelocityY(-160);
  }
}

function myCollisionHandler ()
{
 //console.log("collision") 
}
<script src="//cdn.jsdelivr.net/npm/phaser@3.18.1/dist/phaser.min.js"></script>

关于javascript - Phaser 3 碰撞器剪辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57273036/

相关文章:

javascript - PixiJS只绘制一定数量的矩形?

javascript - raphael svg 路径的一部分卡在原地

javascript - 使用Eclipse编辑javascript, 'Phaser not defined'

javascript - HTML Canvas 中可以有多个剪辑区域吗?

java - 防止 Swing 剪切完全被半透明 JComponent 覆盖的 JComponent

android - ScrollView 内的 glsurfaceview,移动但不剪切

javascript - cucumber /自动化测试 : How to approach the overall concept using javascript functions?

javascript - 将迭代器项分布到子迭代器上

javascript - Phaser 中使用 javascript 的奇怪行为

javascript - PHP 变量从一个页面转移到另一个页面