未捕获的TypeError:无法读取未定义的移相器的属性“apply”

时间:2015-12-25 19:39:29

标签: javascript undefined phaser-framework

未捕获的TypeError:无法读取属性'apply'的undefined ??那是什么意思?

我的意思是我试过调试它,但除了弄清楚问题之外什么都没有。帮助将非常感激。你们需要帮助解决这个问题,请随时问我。谢谢!

JSBIN

这是我的代码:

var game = new Phaser.Game(500, 550, Phaser.CANVAS, 'gameDiv');

var CountDown = {

    preload: function() {
    },
    update: function() {
    },
    render: function() {
    }
}
var player;
var bullets;
var enemies;
var greenEnemies
var bulletTimer = 0;
var mainState = {

    preload: function() {
        game.load.image('background', 'http://s1.postimg.org/nqynk9tkv/starfield.png')
        game.load.image('player', 'http://s28.postimg.org/9qdf4xrfx/145103252914234.gif')
        game.load.image('bullet', 'http://s9.postimg.org/z2bptetxn/bullet.png');
        game.load.image('green', 'http://s28.postimg.org/kpmq4byt5/enemy_green.png')
    },

    create: function() {
        this.backgroundImg = this.game.add.tileSprite(0, 0, 500, 550, 'background')
        player = game.add.sprite(game.world.centerX, 500, 'player')
        player.anchor.setTo(0.5)
        player.scale.setTo(0.25)
        game.physics.arcade.enable(player);
        game.physics.enable(player, Phaser.Physics.ARCADE);
        player.body.collideWorldBounds = true;
        this.game.inputEnabled = true;
        this.game.input.useHandCursor = true;
        player.body.maxVelocity.setTo(400, 400)
        player.body.drag.setTo(400, 400)

        //  The baddies!
    greenEnemies = game.add.group();
    greenEnemies.enableBody = true;
    greenEnemies.physicsBodyType = Phaser.Physics.ARCADE;
    greenEnemies.createMultiple(5, 'green');
    greenEnemies.setAll('anchor.x', 0.5);
    greenEnemies.setAll('anchor.y', 0.5);
    greenEnemies.setAll('scale.x', 0.5);
    greenEnemies.setAll('scale.y', 0.5);
    greenEnemies.setAll('angle', 180);
    greenEnemies.setAll('outOfBoundsKill', true);
    greenEnemies.setAll('checkWorldBounds', true);

    this.launchGreenEnemy();

        bullets = game.add.group();
        bullets.enableBody = true;
        bullets.physicsBodyType = Phaser.Physics.ARCADE;
        bullets.createMultiple(30, 'bullet');
        bullets.setAll('anchor.x', 0.5);
        bullets.setAll('anchor.y', 1);
        bullets.setAll('outOfBoundsKill', true);
        bullets.setAll('checkWorldBounds', true);

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

    update: function() {
        this.backgroundImg.tilePosition.y += 2;
        player.body.acceleration.x = 0;
        if (this.cursors.left.isDown) {
            player.body.acceleration.x -= 600;


        } else if (this.cursors.right.isDown) {
            player.body.acceleration.x += 600;
        }

        if (this.fireButton.isDown) {
            //Grab first bullet from the pool

            if (game.time.now > bulletTimer) {
                var bullet = bullets.getFirstExists(false);
                if (bullet) {
                    bullet.reset(player.x, player.y + 8);
                    //Getting it up
                    bullet.body.velocity.y = -400;
                    bulletTimer = game.time.now + 250;
                }
            }
        }
    },

    launchGreenEnemy: function(){

    var MIN_ENEMY_SPACING = 300;
    var MAX_ENEMY_SPACING = 3000;
    var ENEMY_SPEED = 300;

    var enemy = greenEnemies.getFirstExists(false);
    if (enemy) {
        enemy.reset(game.rnd.integerInRange(0, game.width), -20);
        enemy.body.velocity.x = game.rnd.integerInRange(-300, 300);
        enemy.body.velocity.y = ENEMY_SPEED;
        enemy.body.drag.x = 100;
    }

    //  Send another enemy soon
    game.time.events.add(game.rnd.integerInRange(300, 3000), this.launchGreenEnemy);
    },

    // Restart the game
    platformsCreate: function() {

    }
};

var Menu = {
    preload: function() {
    },
    create: function() {
    },
    update: function() {
    },
    render: function() {
    },
    start: function() {
    }
};

var Game_Over = {
    preload: function() {
    },
    create: function() {
    },
    update: function() {
    },
    render: function() {
    },
    onDown: function() {
    }
};
// Add and start the 'main' state to start the game
game.state.add('CountDown', CountDown)
game.state.add('main', mainState);
game.state.add('Menu', Menu);
game.state.add('Game_Over', Game_Over);
game.state.start('main');

1 个答案:

答案 0 :(得分:6)

我发布这个anwser来帮助那些遇到同样问题的人。

基本上我查了一下语法,它有三个参数。

在我的代码之前:

game.time.events.add(game.rnd.integerInRange(300, 3000), this.launchGreenEnemy);

最后需要这个参数指向那个对象所以我必须把它放在最后这样:

game.time.events.add(game.rnd.integerInRange(300, 3000), this.launchGreenEnemy,this);

希望这能帮助其他人解决同样的问题并拥有快乐的Christmass!