Javascript 相位器3,子弹和tilemap之间没有碰撞
根据Phaser 3示例,我已经从JSON文件创建了一个tilemap,并通过鼠标左键单击光标方向添加了发射子弹。还增加了一个平台。我的问题是,子弹和波浪贴图的对象之间未检测到碰撞。但是子弹和平台之间的碰撞效果很好 这是我的演示代码:Javascript 相位器3,子弹和tilemap之间没有碰撞,javascript,collision-detection,phaser-framework,Javascript,Collision Detection,Phaser Framework,根据Phaser 3示例,我已经从JSON文件创建了一个tilemap,并通过鼠标左键单击光标方向添加了发射子弹。还增加了一个平台。我的问题是,子弹和波浪贴图的对象之间未检测到碰撞。但是子弹和平台之间的碰撞效果很好 这是我的演示代码: var config = { type: Phaser.CANVAS, width: 800, height: 600, backgroundColor: '#2d2d2d', parent: 'phaser-example
var config = {
type: Phaser.CANVAS,
width: 800,
height: 600,
backgroundColor: '#2d2d2d',
parent: 'phaser-example',
physics: {
default: 'arcade',
arcade: {
gravity: { y: 300 },
debug: false
}
},
scene: {
preload: preload,
create: create
}
};
var game = new Phaser.Game(config);
var bullets;
var platforms;
function preload ()
{
this.load.tilemapTiledJSON('map', 'assets/tilemaps/maps/impact-tilemap.json');
this.load.image('kenney', 'assets/tilemaps/tiles/kenney.png');
this.load.image('ground', 'src/games/firstgame/assets/platform.png');
this.load.image('bullet', 'src/games/firstgame/assets/star.png');
}
function create ()
{
var map = this.make.tilemap({ key: 'map' });
var tileset = map.addTilesetImage('kenney');
var layer = map.createStaticLayer(0, tileset, 0, 0);
layer.setCollisionByExclusion([-1]);
this.physics.world.bounds.width = layer.width;
this.physics.world.bounds.height = layer.height;
platforms = this.physics.add.staticGroup();
platforms.create(400, 268, 'ground');
// Fires bullet on left click of mouse
this.input.on('pointerdown', function() {
fire(this);
}, this);
var Bullet = new Phaser.Class({
Extends: Phaser.GameObjects.Image,
initialize: function Bullet(scene)
{
Phaser.GameObjects.Image.call(this, scene, 0, 0, 'bullet');
this.speed = Phaser.Math.GetSpeed(300, 1);
this.velocity = new Phaser.Geom.Point(0, 0);
},
fire: function (x, y, direction)
{
this.setPosition(x, y);
this.setActive(true);
this.setVisible(true);
this.velocity.setTo(0, -this.speed);
Phaser.Math.Rotate(this.velocity, direction);
},
update: function (time, delta)
{
// Update position based on velocity
this.x += this.velocity.x * delta;
this.y += this.velocity.y * delta;
}
});
bullets = this.physics.add.group({
classType: Bullet,
maxSize: 30,
runChildUpdate: true
});
this.physics.add.collider(bullets, platforms, callbackFunc, null, this);
this.physics.add.collider(bullets, layer, callbackFunc, null, this);
}
function callbackFunc(bullet, target)
{
if ( bullet.active === true ) {
console.log("Hit!");
bullet.setActive(false);
bullet.setVisible(false);
}
}
function fire(that)
{
var bullet = bullets.get();
if (bullet) {
bullet.body.allowGravity = false;
var angle = Math.atan2(that.input.activePointer.y - 400, that.input.activePointer.x - 300);
bullet.fire(300, 400, angle + (3.14/2));
}
}
您可以将其复制粘贴到任何位置的示例,然后开始单击以在任何方向开火
我也尝试过从示例中添加一个标准播放器,碰撞可以很好地处理tilemap的对象。所以问题只在于子弹
请帮忙解决这个问题。谢谢 如果将console.log()语句放在bullet碰撞回调中,它会打印出来吗?我想知道你的子弹是否不符合
bullet.active
条件?@scunliffe,子弹穿过瓷砖时不会发生任何事情。就像没有碰撞一样。但有了平台,一切都还不错。有什么想法吗?