Skip to content Skip to sidebar Skip to footer

Why Objects In Phaser 2.6.2 Do Not Collide?

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

Solution 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>

Post a Comment for "Why Objects In Phaser 2.6.2 Do Not Collide?"