简体   繁体   English

Phaser3:检测精灵碰撞

[英]Phaser3: Detecting sprite collision

Using Phaser 3 and I'm having trouble detecting collision between two sprites. 使用Phaser 3,我无法检测两个精灵之间的碰撞。 I've been up and down the docs and several examples, mostly outdated from Phaser 2, and for some reason it just isn't working. 我一直在上下文档和几个例子,大多数是从Phaser 2过时的,并且由于某种原因它只是不起作用。

What I have going is an array of mapTiles and a player character. 我要去的是一系列mapTiles和一个玩家角色。 Right now, I have walls , floors , and doors all being passed to the collider just for testing purposes and nothing is happening. 现在,我将墙壁地板都传递到对撞机只是为了测试目的而没有任何事情发生。

As you can see in the code below I do have a collider working with the this.enemies array so I'm not sure why it isn't working with the this.mapTiles . 正如你在下面的代码中看到的,我确实有一个使用this.enemies数组的对撞机,所以我不确定为什么它不能使用this.mapTiles Also, when checking the types of my mapTiles they are each labeled as Sprite and my enemies are apparently labeled as ArcadeSprite , could this be the issue? 此外,在检查我的mapTiles的类型时,它们每个都被标记为Sprite ,我的敌人显然被标记为ArcadeSprite ,这可能是问题吗?

在此输入图像描述

Dungeon.js Dungeon.js

import 'phaser';

import {
  Enemy,
  MapGenerator,
  PlayerCharacter
} from '../game_objects/index'

class DungeonScene extends Phaser.Scene {
  constructor() {
    super({ key: 'DUNGEON' });
    this.mapTiles = []
    this.walls = []
    this.player = null
    this.enemies = []
    this.entities = []
  }

  preload() {
    this.load.spritesheet('sprites', 'src/arial10x10.png', { frameWidth: 10, frameHeight: 10 })
  }

  create() {
    this.createMap()
    this.player = new PlayerCharacter('PC', this, 9, 9, 'sprites', 32, { health: 100, atk: 10 }, [])
    this.enemies = [
      new Enemy('E1', this, 64, 64, 'sprites', 100, { health: 100, atk: 10 }, [])
    ]

    this.keyboard = this.input.keyboard.addKeys('W, A, S, D')

    // this works but not by default; aka it requires a callback to do anything
    // which is odd because examples show it working without a callback
    this.physics.add.collider(this.player, this.enemies, () => {
      this.scene.restart()
    })
    // DOES NOT WORK
    this.physics.add.overlap(this.player, this.mapTiles, () => console.log('overlap'))
    // DOES NOT WORK
    this.physics.add.collider(this.player, this.mapTiles, () => console.log('collider'))
  }

  createMap() {
    const mapGenerator = new MapGenerator(this, 39, 39, 9, 9)
    mapGenerator.create()
    this.mapTiles = mapGenerator.mapTiles
  }
}

Edit 编辑

I decided to cut down some code and also give a small 3 line example of what's not working. 我决定减少一些代码,并给出一个小的3行示例,说明什么不起作用。

This is one with just two sprites and still is not working. 这是一个只有两个精灵,仍然无法正常工作。 This small example would be placed in my scenes create method. 这个小例子将放在我的场景创建方法中。

this.foo = this.add.sprite(10, 10, 'sprites', 32)
this.bar = this.add.sprite(30, 30, 'sprites', 2)
this.physics.add.collider(this.foo, this.bar)

The collision is not working in your because the sprites do not have physics bodies. 碰撞不起作用,因为精灵没有物理体。

As you noted, the mapTiles are type Sprite where as enemies are ArcadeSprite meaning they have a physics body. 如你所知,mapTiles是Sprite类型,其中敌人是ArcadeSprite意味着他们有一个物理体。

The difference in creating them is the factory used. 创建它们的不同之处在于工厂使用。

this.add.sprite(...) uses the GameObject factory (Sprites are GameObjects) and this.physics.add.sprite(...) uses the physics Arcade factory, which creates a sprite, and gives it a physics body. this.add.sprite(...)使用GameObject工厂(Sprites是this.physics.add.sprite(...) ), this.physics.add.sprite(...)使用物理Arcade工厂,它创建一个精灵,并给它一个物理体。

So, in your code for the minimal example change your calls for creating sprites to do it with the physics factory using this.physics.add.sprite(...) and it should work. 因此,在最小示例的代码中,使用this.physics.add.sprite(...)更改您对使用物理工厂创建sprite的调用,它应该可以工作。

I don't know what your map generator code looks like, but I'm assuming it will be a similar fix. 我不知道你的地图生成器代码是什么样的,但我认为它将是一个类似的修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM