最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Cannot read property 'apply' of undefined phaser - Stack Overflow

programmeradmin4浏览0评论

Uncaught TypeError: Cannot read property 'apply' of undefined?? What is that suppose to mean?

I mean i tried debugging it and all but cant figure out the problem. Help would be really appreciated. Anything you guys need to help figure out this problem, please feel free to ask me. Thank you!

JSBIN

Here is my code:

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', '.png')
        game.load.image('player', '.gif')
        game.load.image('bullet', '.png');
        game.load.image('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');

Uncaught TypeError: Cannot read property 'apply' of undefined?? What is that suppose to mean?

I mean i tried debugging it and all but cant figure out the problem. Help would be really appreciated. Anything you guys need to help figure out this problem, please feel free to ask me. Thank you!

JSBIN

Here is my code:

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/nqynk9tkv/starfield.png')
        game.load.image('player', 'http://s28.postimg/9qdf4xrfx/145103252914234.gif')
        game.load.image('bullet', 'http://s9.postimg/z2bptetxn/bullet.png');
        game.load.image('green', 'http://s28.postimg/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');
Share Improve this question edited Feb 29, 2016 at 19:13 asked Dec 25, 2015 at 19:39 user5613751user5613751 9
  • It means that somewhere in your code there's something like whatever.apply, and whatever is undefined. That is, instead of whatever being a reference to an object, it's not. – Pointy Commented Dec 25, 2015 at 19:45
  • 1 I never used the apply method. Thats why im so confused... I think its where in the game.events.add method but not sure. – user5613751 Commented Dec 25, 2015 at 19:46
  • I don't see apply mentioned anywhere in your code; it could be in a library you're using, meaning that you're invoking some library API in a way the library doesn't expect. You should be able to use a non-minified version of the library along with the browser's debug tools to figure out where in your code that's happening. – Pointy Commented Dec 25, 2015 at 19:47
  • I included a JSBin format, take a look at that. Maybe it will help – user5613751 Commented Dec 25, 2015 at 19:47
  • It looks like something in that Phaser code expects there to be a "callback" property on elements in some data structure. I'm totally unfamiliar with that code however so I don't really know what much of it means. – Pointy Commented Dec 25, 2015 at 19:56
 |  Show 4 more ments

1 Answer 1

Reset to default 6

I'm posting this anwser to help others who are having the same issue.

Basically I looked up the syntax and it had three arguments.

Before, my code was:

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

It needed the this parameter at the end to point at that object so I had to put this at the end like this:

game.time.events.add(game.rnd.integerInRange(300, 3000), this.launchGreenEnemy, this);
                                                                              ^^^^^^
发布评论

评论列表(0)

  1. 暂无评论