在Phaser中使用JavaScript时重叠不起作用
我正在做一个游戏,在这个游戏中我想当玩家与硬币重叠时,硬币消失了,但这不是出于某种原因,我不知道为什么。当我制作地图时,我已经尝试了很多方法,甚至把硬币放在瓷砖上,但即使这样,它仍然不起作用 有人能帮我吗在Phaser中使用JavaScript时重叠不起作用,javascript,phaser-framework,Javascript,Phaser Framework,我正在做一个游戏,在这个游戏中我想当玩家与硬币重叠时,硬币消失了,但这不是出于某种原因,我不知道为什么。当我制作地图时,我已经尝试了很多方法,甚至把硬币放在瓷砖上,但即使这样,它仍然不起作用 有人能帮我吗 var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); f
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', {
preload: preload, create: create, update: update, render: render });
function preload() {
game.load.tilemap('level12', 'res/level12.json', null,
Phaser.Tilemap.TILED_JSON);
game.load.image('sprite12', 'res/sprite12.png');
game.load.spritesheet('dude', 'res/player.png', 64, 64);
game.load.spritesheet('droid', 'res/droid.png', 32, 32);
game.load.image('starSmall', 'res/star.png');
game.load.spritesheet('coin', 'res/coin.png',32,32 );
game.load.image('background', 'res/sprite3.png');
}
var map;
var tileset;
var layer;
var player;
var facing = 'left';
var jumpTimer = 0;
var cursors;
var jumpButton;
var bg;
var coin;
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
game.stage.backgroundColor = '#000000';
bg = game.add.tileSprite(0, 0, 800, 600, 'background');
bg.fixedToCamera = true;
map = game.add.tilemap('level12');
map.addTilesetImage('sprite12');
map.addTilesetImage('coin');
map.setCollisionBetween(1, 12);
map.setCollisionByExclusion([ 13, 14, 15, 16, 46, 47, 48, 49, 50, 51 ]);
layer = map.createLayer('Tile Layer 1');
layer.resizeWorld();
coin = game.add.group();
coin.enableBody = true;
coin.physicsBodyType = Phaser.Physics.ARCADE;
createcoin();
game.physics.arcade.gravity.y = 200;
player = game.add.sprite(150, 900, 'dude');
game.physics.enable(player, Phaser.Physics.ARCADE);
player.body.bounce.y = 0.2;
player.body.collideWorldBounds = true;
player.animations.add('left', [3,2,1,0], 10, true);
player.animations.add('right', [4,5,6,7], 10, true);
game.camera.follow(player);
cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
game.physics.arcade.overlap(coin, player, killcoin(), null, this);
}
function killcoin(coin,player) {
coin.kill();
}
function createcoin() {
coin = game.add.sprite(50,700, 'coin');
}
function update() {
game.physics.arcade.collide(player, layer);
player.body.velocity.x = 0;
if (cursors.left.isDown) {
player.body.velocity.x = -150;
if (facing != 'left') {
player.animations.play('left');
facing = 'left';
}
}
else if (cursors.right.isDown) {
player.body.velocity.x = 150;
if (facing != 'right') {
player.animations.play('right');
facing = 'right';
}
}
else {
if (facing != 'idle') {
player.animations.stop();
if (facing == 'left') {
player.frame = 0;
}
else {
player.frame = 5;
}
facing = 'idle';
}
}
if (jumpButton.isDown && player.body.onFloor() && game.time.now > jumpTimer) {
player.body.velocity.y = -250;
jumpTimer = game.time.now + 750;
}
}
function render () {
}
代码中有三个问题。首先,变量的命名令人困惑。您正在创建一个组并将其分配给
coin
,但您也在添加新精灵并将其分配给coin
。那么,调用重叠函数时,coin
的值是多少?也许这在技术层面上是正确的,但为了避免混淆,我会为不同的事情使用不同的变量名。如果没有其他东西,它会使代码更具可读性
其次,在createcoin()
中添加到游戏中的硬币精灵不会添加到组中。因为它没有添加到组中,所以精灵的物理体永远不会启用
最后,arcade.overlap
函数在create()中只调用一次。我认为应该在update()函数中调用它。js中的update()函数是一种主要的游戏循环。每次帧更新都会调用它
因此,将这三个修复放在一起,我会尝试以下方法:
grpcoins = game.add.group();
grpcoins.enableBody = true;
grpcoins.physicsBodyType = Phaser.Physics.ARCADE;
//..
function createcoin() {
var c = game.add.sprite(50,700, 'coin');
grpcoins.add(c);
}
function update() {
//..
game.physics.arcade.overlap(player, grpcoins, killcoin, null, this);
//..
}
function killcoin(pl, cn) {
cn.kill();
}
在您创建的创建函数中,在您更新的更新函数中,杀死一枚硬币不是初始状态,而是在特定条件下即将发生的事情,这两行
game.physics.arcade.overlap(coins, player, killcoin, null, this); //killcoin is callback so no(), coins with "s" as you are calling the whole group
function killcoin(coin,player) {
coin.kill();
}
属于更新功能。
您可能仍然对范围有问题
你们也应该为硬币创建一个组,类似这样的,你们创建它们,这样他们就可以创建功能了
coins= game.add.group();// group of many coins
coins.enableBody = true;
for (var i = 0; i<12; i++){
var coin = coins.create(i*70,0, 'coin');// var individual coin
coin.body.gravity.y = 6;
coin.body.bounce.y = 0.7 +Math.random()*0.2;
}
coins=game.add.group();//一组硬币
coins.enableBody=true;
对于(var i=0;我认为这行肯定是错误的game.physics.arcade.overlap(硬币,玩家,killcoin(),null,this);
@Musa is it player.physics.arcade.overlap(硬币,玩家,killcoin(),null,this);回调部分是我正在查看的。idk我仍然不知道该做什么。您可以提供JSON文件,以便我们尝试调试它吗?或者您可以提供最低版本吗?