【发布时间】:2016-12-23 09:26:18
【问题描述】:
我想从我的对象发射子弹。一切看起来都是正确的,但经过大量的努力却没有得到适当的理由。看看我的代码。
Game.js
var Game = {
preload : function() {
// spaceship image screen
this.load.image('spaceship', './assets/images/spaceship.png');
//Load the bullet
this.load.image('bullet', 'assets/images/bullet.png');
},
这是创建函数
create : function() {
// Our bullet group
bullets = game.add.group();
bullets.enableBody = true;
bullets.physicsBodyType = Phaser.Physics.ARCADE;
bullets.createMultiple(30, 'bullet', 0, false);
bullets.setAll('anchor.x', 0.5);
bullets.setAll('anchor.y', 0.5);
bullets.setAll('outOfBoundsKill', true);
bullets.setAll('checkWorldBounds', true);
sprite = game.add.sprite(400, 300, 'spaceship');
sprite.anchor.set(0.5);
},
这是更新函数
update: function() {
if (game.input.activePointer.isDown)
{
this.fire();
}
},
火功能
fire: function () {
if (game.time.now > nextFire && bullets.countDead() > 0)
{
nextFire = game.time.now + fireRate;
var bullet = bullets.getFirstDead();
bullet.reset(sprite.x - 80, sprite.y - 80);
game.physics.arcade.moveToPointer(bullet, 300);
}
}
请检查我的代码并指出我哪里错了?
【问题讨论】:
-
实际发生了什么?子弹出现但不动?它会移动,但方向不正确吗? fire 函数会被调用吗?
-
现在它正在工作。我在更新中更改背景,我的项目符号组在创建中,我将我的项目符号组放在更新中并且它工作正常。但是如果我改变 bullets.setAll('anchor.x', 0.5); ,我想改变子弹的起点bullets.setAll('anchor.y', 0.5);从 0.5 增加到更多,所以我得到了错误。所以请帮助我,我想在宇宙飞船的顶部开始子弹。
-
您已经接受了一个答案,但您似乎在问一个问题。您的问题解决了吗,还是仍有问题?
标签: javascript phaser-framework