Why objects in phaser 2.6.2 do not collide?

情到浓时终转凉″ 提交于 2020-01-25 10:09:15

问题


There are two tanks. One of them is controlled by arrows. I did everything that was necessary so that the tanks could collide, but this does not happen. Why? If this large code is not easy to read, please tell me where I could run the example. Below I commented on the place where the code does not work as it should.

UPDATE

I downloaded a working example here

enter link description 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/phaser-ce@2.13.2"></script>


来源:https://stackoverflow.com/questions/57622217/why-objects-in-phaser-2-6-2-do-not-collide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!