Reputation:
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!
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.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');
Upvotes: 5
Views: 7155
Reputation:
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);
^^^^^^
Upvotes: 6