2015-12-25 14 views
5

Unchaught TypeError: Impossibile leggere la proprietà 'apply' di undefined ?? Che cosa dovrebbe significare?Unchaught TypeError: Impossibile leggere la proprietà 'apply' del phaser non definito

Voglio dire, ho provato il debug e tutto ma non riesco a capire il problema. L'aiuto sarebbe molto apprezzato. Qualunque cosa tu abbia bisogno di aiutare a capire questo problema, per favore sentiti libero di chiedermelo. Grazie!

JSBIN

Ecco il mio codice:

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'); 
+0

Significa che da qualche parte nel codice c'è qualcosa come "whatever.apply', e" whatever' è 'undefined'. Cioè, invece di "qualunque cosa" è un riferimento a un oggetto, non lo è. – Pointy

+1

Non ho mai usato il metodo apply. Ecco perché sono così confuso ... Penso che sia in che punto del metodo game.events.add ma non sono sicuro. –

+0

Non vedo 'apply' menzionato da nessuna parte nel codice; potrebbe essere in una libreria che stai utilizzando, il che significa che stai invocando alcune API della libreria in un modo che la biblioteca non si aspetta. Dovresti essere in grado di utilizzare una versione non minificata della libreria insieme agli strumenti di debug del browser per capire dove sta accadendo nel tuo codice. – Pointy

risposta

5

ho posto questa anwser per aiutare gli altri che stanno avendo lo stesso problema.

Fondamentalmente ho cercato la sintassi e aveva tre argomenti.

Prima che il mio codice era:

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

E 'necessario l'questo parametro alla fine per puntare a quell'oggetto così ho dovuto mettere questo alla fine in questo modo:

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

Spero che questo aiutato gli altri con lo stesso problema e Buon Natale!

+0

Nel mio caso non era il parametro di contesto ma il gestore di eventi che avevo un errore di battitura in cui causato questo Grazie. – icepicker

Problemi correlati