【问题标题】:Collision detection via overlap() not working in phaser.io通过重叠()的碰撞检测在 phaser.io 中不起作用
【发布时间】:2017-05-08 05:34:37
【问题描述】:

请注意,我完全使用 Phaser.io 创建了以下代码。

所以我创建了一个敌人组和一个武器组(使用内置的game.add.weapon())。

我检查这两组之间是否有重叠,但由于某种原因,hitEnemy() 函数从未被触发。

我认为这与使用内置武器组有关,但我无法弄清楚它到底是什么。

var playState = {
    preload: function () {
        game.load.image('player', 'assets/player.png');
        game.load.image('enemy', 'assets/enemy.png');
        game.load.image('floor', 'assets/floor.png');
        game.load.image('bullet', 'assets/bullet.png');
    },

    create: function () {
        game.stage.backgroundColor = '#000000';
        game.physics.startSystem(Phaser.Physics.ARCADE);
        game.renderer.renderSession.roundPixels = true;

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

        this.createBullets();

        this.createEnemies(game.width/2, 120, 'enemy');
     },
     update: function () {
        game.physics.arcade.collide(this.player, this.floor);
        game.physics.arcade.overlap(this.weapon, this.enemies, this.hitEnemy, null, this);
     },
     createBullets: function () {    
        this.weapon = game.add.weapon(30, 'bullet');
        this.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
        this.weapon.bulletSpeed = 850;
        this.weapon.fireRate = 100;
        this.weapon.trackSprite(this.player);
    },
    createEnemies: function (x,y ,size) {
        this.enemies = game.add.group();
        this.enemies.enableBody = true;
        this.enemies.physicsBodyType = Phaser.Physics.ARCADE;

        var asteroid = this.enemies.create(x, y, size);
        asteroid.anchor.setTo(0.5,0.5);
        asteroid.name = "enemy1";
        asteroid.body.immovable;
    },
    hitEnemy: function (player, enemy) {
        this.enemy.kill();
        console.log("Hit");
    },
};

【问题讨论】:

    标签: javascript game-physics phaser-framework


    【解决方案1】:

    找到了解决方案。

    而不是检查与 this.enemies 和 this.weapon 的重叠。我应该检查与 this.enemies 和 this.weapon.bullets 的碰撞

    【讨论】:

      猜你喜欢
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多