开发者问题收集

为什么 Phaser 2.6.2 中的物体不会发生碰撞?

2019-08-23
319

有两辆坦克。其中一辆由箭头控制。我做了一切必要的事情,以便坦克可以相撞,但这并没有发生。为什么?如果这个大代码不易阅读,请告诉我在哪里可以运行该示例。下面我评论了代码无法正常工作的地方。

更新

I downloaded a working example here

在此处输入链接描述

export default function game() {
    const game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser', { preload: preload, create: create, update: update, render: render });

    let player;
    let cursors;
    let fireButton;
    let weapon;
    let  weapon2;
    let controls;

    let enemy;


    function preload() {
        game.load.image('player', '../img/game/tank.png');
        game.load.image('bullet', '../img/game/bullet.png');
    }

    function create() {
        game.physics.startSystem(Phaser.Physics.ARCADE);

        game.world.setBounds(0, 0, 800, 600);

        player = game.add.sprite(500, 300, 'player');
        player.scale.setTo(.1, .1);
        player.anchor.setTo(0.5, 0.5);
        player.angle = 270;
        player.enableBody = true;


        cursors = game.input.keyboard.createCursorKeys();

        fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

        weapon = game.add.weapon(1, 'bullet');
        weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
        weapon.bulletAngleOffset = 90;
        weapon.bulletSpeed = 400;
        weapon.trackSprite(player, 0, 0, false);
        weapon.fireAngle = player.angle + 90;


        enemy = game.add.sprite(100, 100, 'player');
        enemy.scale.setTo(.1, .1);
        enemy.anchor.setTo(0.5, 0.5);
        enemy.angle = 270;
        enemy.enableBody = true;


        weapon2 = game.add.weapon(1, 'bullet');
        weapon2.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
        weapon2.bulletAngleOffset = 90;
        weapon2.bulletSpeed = 1400;
        weapon2.trackSprite(enemy, 0, 0, false);
        weapon2.fireAngle = enemy.angle + 90;



        controls = {
            right : this.input.keyboard.addKey(Phaser.Keyboard.D),
            left : this.input.keyboard.addKey(Phaser.Keyboard.A),
            up : this.input.keyboard.addKey(Phaser.Keyboard.W),
            down : this.input.keyboard.addKey(Phaser.Keyboard.S),
            fire : this.input.keyboard.addKey(Phaser.Keyboard.Q),
        };


        game.physics.arcade.enable(player);
        game.physics.arcade.enable(enemy);
        player.body.immovable = true;
        enemy.body.immovable = true;
    }

    function killThem(enemy,  bullet) {
        console.dir(weapon);
        enemy.kill();
        bullet.kill();

    }

    function update() {
        game.physics.arcade.collide(weapon.bullets, enemy, killThem);
        game.physics.arcade.collide(weapon2.bullets, player, killThem);
        game.physics.arcade.collide(player, enemy); // THIS CODE DOESNT WORK

        if (cursors.up.isDown) {
            player.y -= 4;
            player.angle = 180;
            weapon.fireAngle = 270;
        } else if (cursors.down.isDown) {
            player.y += 4;
            player.angle = 0;
            weapon.fireAngle = 90;
        } else if (cursors.left.isDown) {
            player.x -= 4;
            player.angle = 90;
            weapon.fireAngle = 180;
        } else if (cursors.right.isDown) {
            player.x += 4;
            player.angle = 270;
            weapon.fireAngle = 360;
        }

        if (fireButton.isDown) {
            weapon.fire()
        }


        if (controls.up.isDown) {
            enemy.y -= 4;
            enemy.angle = 180;
            weapon2.fireAngle = 270;
        } else if (controls.down.isDown) {
            enemy.y += 4;
            enemy.angle = 0;
            weapon2.fireAngle = 90;
        } else if (controls.left.isDown) {
            enemy.x -= 4;
            enemy.angle = 90;
            weapon2.fireAngle = 180;
        } else if (controls.right.isDown) {
            enemy.x += 4;
            enemy.angle = 270;
            weapon2.fireAngle = 360;
        }


        if (controls.fire.isDown) {
            weapon2.fire()
        }
    }


    function render() {
        weapon.debug()
    }

}
1个回答
const game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser', { preload: preload, create: create, update: update, render: render });

    let player;
    let enemy;
    let cursors;

    function preload() {
      this.load.baseURL = 'https://raw.githubusercontent.com/nazimboudeffa/assets/master/';
      this.load.crossOrigin = 'anonymous';
      this.load.image('dude', 'sprites/phaser-dude.png');
      this.load.image('alien', 'sprites/phaser-alien.png');
    }

    function create() {
        game.physics.startSystem(Phaser.Physics.ARCADE);

        game.world.setBounds(0, 0, 800, 600);

        player = game.add.sprite(100, 100, 'dude');
        game.physics.arcade.enable(player);
        player.body.enbable = true;

        enemy = game.add.sprite(300, 100, 'alien');
        game.physics.arcade.enable(enemy);
        enemy.body.enbable = true;
        enemy.body.immovable = true;
        
        cursors = game.input.keyboard.createCursorKeys();
        
    }

    function update() {

        game.physics.arcade.collide(player, enemy, ()=>{console.log("collision")});

        if (cursors.up.isDown) {
            player.y -= 4;
        } else if (cursors.down.isDown) {
            player.y += 4;
        } else if (cursors.left.isDown) {
            player.x -= 4;
        } else if (cursors.right.isDown) {
            player.x += 4;
        }
    }


    function render() {
      var debug = this.game.debug;
      debug.phaser(10, 20);
    }
<script src="//cdn.jsdelivr.net/npm/[email protected]"></script>
nazimboudeffa
2019-08-23