未捕获的类型错误:无法读取属性'应用'的未定义相位器
Uncaught TypeError: Cannot read property 'apply' of undefined with phaser
我又遇到了同样的问题,我不知道如何解决。当蓝色敌人到达底部时,错误突然出现,我记下了。请帮忙!
JSBin格式点击右上角的编辑来编辑代码
代码:
var game = new Phaser.Game(500, 550, Phaser.CANVAS, 'gameDiv');
var CountDown = {
preload: function() {
},
update: function() {
},
render: function() {
}
}
var player;
var enemy;
var bullets;
var shields;
var enemies;
var greenEnemies
var explosions;
var score = 0;
var scoreText;
var bulletTimer = 0;
var blueEnemies;
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')
game.load.spritesheet('explosionAnim', 'https://raw.githubusercontent.com/jschomay/phaser-demo-game/master/assets/explode.png', 128, 128)
game.load.bitmapFont('spacefont', 'https://raw.githubusercontent.com/jschomay/phaser-demo-game/master/assets/spacefont/spacefont.png', 'https://rawgit.com/jschomay/phaser-demo-game/master/assets/spacefont/spacefont.xml');
game.load.image('blue', 'https://raw.githubusercontent.com/jschomay/phaser-demo-game/master/assets/enemy-blue.png')
},
create: function() {
this.backgroundImg = this.game.add.tileSprite(0, 0, 500, 550, 'background')
player = game.add.sprite(game.world.centerX, 500, 'player')
player.health = 100;
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);
greenEnemies.forEach(function(enemy){
enemy.body.setSize(enemy.width * 3 / 4, enemy.height * 3 / 4);
enemy.damageAmount = 20;
})
blueEnemies = game.add.group();
blueEnemies.enableBody = true;
blueEnemies.physicsBodyType = Phaser.Physics.ARCADE;
blueEnemies.createMultiple(5, 'blue');
blueEnemies.setAll('anchor.x', 0.5);
blueEnemies.setAll('anchor.y', 0.5);
blueEnemies.setAll('scale.x', 0.5);
blueEnemies.setAll('scale.y', 0.5);
blueEnemies.setAll('angle', 180);
blueEnemies.setAll('outOfBoundsKill', true);
blueEnemies.setAll('checkWorldBounds', true);
blueEnemies.forEach(function(enemy){
enemy.body.setSize(enemy.width * 3 / 4, enemy.height * 3 / 4);
enemy.damageAmount = 40;
})
game.time.events.add(1000, this.launchBlueEnemy);
// Shields stat
shields = game.add.bitmapText(game.world.width - 250, 10, 'spacefont', '' + player.health +'%', 50);
shields.render = function () {
shields.text = 'Shields: ' + Math.max(player.health, 0) +'%';
};
shields.render();
// Score
scoreText = game.add.bitmapText(10, 10, 'spacefont', '', 50);
scoreText.render = function () {
scoreText.text = 'Score: ' + score;
};
scoreText.render();
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);
explosions = game.add.group();
explosions.enableBody = true;
explosions.physicsBodyType = Phaser.Physics.ARCADE;
explosions.createMultiple(30, 'explosionAnim');
explosions.setAll('anchor.x', 0.5);
explosions.setAll('anchor.y', 0.5);
explosions.forEach( function(explosion) {
explosion.animations.add('explosionAnim');
});
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;
}
game.physics.arcade.overlap(player, greenEnemies, this.shipCollide, null, this);
game.physics.arcade.overlap(greenEnemies, bullets, this.bulletCollide, null, this);
game.physics.arcade.overlap(player, blueEnemies, this.shipCollide, null, this);
game.physics.arcade.overlap(bullets, blueEnemies, this.hitEnemy, null, this);
if (player.alive && 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;
}
}
}
if(!(player.alive)){
console.log("Game Over")
}
},
launchGreenEnemy: function(){
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 = 300;
enemy.body.drag.x = 100;
}
game.time.events.add(game.rnd.integerInRange(300, 3000), this.launchGreenEnemy);
},
shipCollide: function(player,enemy){
var explosion = explosions.getFirstExists(false);
explosion.reset(enemy.body.x + enemy.body.halfWidth, enemy.body.y + enemy.body.halfHeight);
explosion.body.velocity.y = enemy.body.velocity.y;
explosion.alpha = 0.7;
explosion.play('explosionAnim', 30, false, true);
enemy.kill();
player.damage(enemy.damageAmount);
shields.render();
},
bulletCollide: function(bullet,enemy){
var explosion = explosions.getFirstExists(false);
explosion.reset(bullet.body.x + bullet.body.halfWidth, bullet.body.y + bullet.body.halfHeight);
explosion.body.velocity.y = enemy.body.velocity.y;
explosion.alpha = 0.7;
explosion.play('explosionAnim', 30, false, true);
enemy.kill();
bullet.kill();
score += enemy.damageAmount * 10;
scoreText.render()
},
launchBlueEnemy:function(){
enemy = blueEnemies.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 = 300;
enemy.body.drag.x = 100;
if (this.y > game.height + 200) {
this.kill();
this.y = -20;
}
}
game.time.events.add(game.rnd.integerInRange(300, 3000), this.launchBlueEnemy);
},
// 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');
没有回调或callbackContext的事件在事件数组中。
args: Array[0]
callback: undefined
callbackContext: undefined
delay: 644
loop: false
pendingDelete: true
repeatCount: -1
tick: 1451125781936
我认为这条线路造成了你的问题:
game.time.events.add(1000, this.launchBlueEnemy);
在寻找如何使用events.add的例子时,我发现了这个:
http://phaser.io/examples/v2/time/basic-timed-event
// Here we'll create a basic timed event. This is a one-off event, it won't repeat or loop:
// The first parameter is how long to wait before the event fires. In this case 4 seconds (you could pass in 4000 as the value as well.)
// The next parameter is the function to call ('fadePicture') and finally the context under which that will happen.
game.time.events.add(Phaser.Timer.SECOND * 4, fadePicture, this);
这表明您需要提供"This"作为第三个参数。
这是事件的源代码。添加:
/**
* Adds a new Event to this Timer. The event will fire after the given amount of 'delay' in milliseconds has passed, once the Timer has started running.
* Call Timer.start() once you have added all of the Events you require for this Timer. The delay is in relation to when the Timer starts, not the time it was added.
* If the Timer is already running the delay will be calculated based on the timers current time.
*
* @method Phaser.Timer#add
* @param {number} delay - The number of milliseconds that should elapse before the Timer will call the given callback.
* @param {function} callback - The callback that will be called when the Timer event occurs.
* @param {object} callbackContext - The context in which the callback will be called.
* @param {...*} arguments - The values to be sent to your callback function when it is called.
* @return {Phaser.TimerEvent} The Phaser.TimerEvent object that was created.
*/
add: function (delay, callback, callbackContext) {
return this.create(delay, false, 0, callback, callbackContext, Array.prototype.splice.call(arguments, 3));
},
明白了:
game.time.events.add(game.rnd.integerInRange(300, 3000), this.launchBlueEnemy);
这个,并没有在这里引用你的应用程序的这个。我认为这是因为你早些时候遗漏了上下文。
我弄错anwser的原因是我错过了@Norbet所说的第三个参数。但我有两个地方错过了。
第一个在最后:
launchBlueEnemy:function(){
enemy = blueEnemies.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 = 300;
enemy.body.drag.x = 100;
if (this.y > game.height + 200) {
this.kill();
this.y = -20;
}
}
game.time.events.add(game.rnd.integerInRange(300, 3000), this.launchBlueEnemy); <<-- Should have had this as the third parameter.
},
第二个是:
launchGreenEnemy: function(){
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 = 300;
enemy.body.drag.x = 100;
}
game.time.events.add(game.rnd.integerInRange(300, 3000), this.launchGreenEnemy); <<-- Should have had this as the third parameter.
},
希望这能帮助到像我一样挣扎的人们:-)。圣诞晚会快乐!
相关文章:
- 有条件地在选项标记中应用布尔属性
- 表单's的action属性-如何在javascript中指定完整的应用程序路径
- CSS根据属性的可用性有条件地应用样式
- 如何在不链接/jquery的情况下使用方法应用css属性数组
- vendors.js:未捕获类型错误:无法读取属性'应用'使用webpack时未定义的
- 使用 SharePoint 查询字符串值作为应用程序部件属性
- 我把“;类“;SVG中存在但未应用的节点的属性!为什么?
- jquery.mobile-1.4.5.js:26未捕获类型错误:无法设置属性'移动'react应用程序
- javascript对象属性应用于所有对象
- 找不到属性'link_to'on对象(生成的应用程序控制器)
- Webpacked Angular2应用程序类型错误:无法读取属性'getOptional'的未定义
- Angular赢得't将对象更改应用于它'的属性
- '无法设置未定义'的属性;应用绑定时出错
- 角度嵌套对象-TypeError:无法读取属性'应用程序'的未定义
- 在knocket js中应用绑定后,向视图模型添加新属性
- 我如何通过 id 找到复选框并使用 JQuery 在字符串上应用属性
- 有条件地应用属性(AngularJS)
- 如何通过调整图像大小和应用属性来减少页面加载时间
- Extjs6通过函数从配置中应用属性
- 应用属性'滚动=yes'到所有iFrames