Bullet在phaser.io中不起作用

时间:2016-07-27 11:57:38

标签: javascript phaser-framework

我想从我的物体射出子弹。一切都看起来正确,但经过很多努力没有得到正确的理由。看看我的代码。

Game.js

var Game = {
    preload : function() {

        // spaceship image screen
        this.load.image('spaceship', './assets/images/spaceship.png');
        //Load the bullet
        this.load.image('bullet',   'assets/images/bullet.png');

    },

这是创建功能

create : function() {
    //  Our bullet group
    bullets = game.add.group();
    bullets.enableBody = true;
    bullets.physicsBodyType = Phaser.Physics.ARCADE;
    bullets.createMultiple(30, 'bullet', 0, false);
    bullets.setAll('anchor.x', 0.5);
    bullets.setAll('anchor.y', 0.5);
    bullets.setAll('outOfBoundsKill', true);
    bullets.setAll('checkWorldBounds', true);

    sprite = game.add.sprite(400, 300, 'spaceship');
    sprite.anchor.set(0.5);
},

这是更新功能

update: function() {
    if (game.input.activePointer.isDown)
    {
        this.fire();
    }
},

消防功能

fire: function () {

    if (game.time.now > nextFire && bullets.countDead() > 0)
    {
        nextFire = game.time.now + fireRate;

        var bullet = bullets.getFirstDead();

        bullet.reset(sprite.x - 80, sprite.y - 80);

        game.physics.arcade.moveToPointer(bullet, 300);
    }
}

请查看我的代码并建议我错在哪里?

2 个答案:

答案 0 :(得分:0)

我无法用我在这里获得的信息发现问题......你有没有理由不使用预制武器类?在移相器示例站点上有很好的示例。

答案 1 :(得分:0)

现在它正在运作。我正在更新后台更改背景,我的子弹组正在创建中,我将我的子弹组更新并且工作正常。但是如果我改变了bullets.setAll(' anchor.x',0.5),我想改变子弹的起点; bullets.setAll(' anchor.y',0.5);从0.5增加到更多,所以我得到错误。所以,请帮助我,我想在宇宙飞船的顶部开始子弹。因为我将移动我的太空飞船子弹应朝那个方向射击。