2017-07-02 51 views
0

我正在制作游戏,并且在此游戏中,我希望当玩家与硬币重叠时,硬币消失,但它不是由于某种原因,我也不是为什么。当我制作地图时,我甚至尝试了很多方法将硬币放入瓷砖中,但即使如此,它仍然无法使用。重叠无法在Phaser中使用JavaScript

任何人都可以帮助我吗?

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() { 



} 
+0

那么这行肯定是错误的'game.physics.arcade.overlap(coin,player,killcoin(),null,this);' – Musa

+0

@Musa is it player.physics.arcade.overlap (硬币,玩家,杀戮币(),null,this); – Muthar

+0

回调部分就是我正在看的东西。 – Musa

回答

0

你的代码有三个问题。首先变量的命名很混乱。您正在创建一个组并将其分配给coin,但您还要添加新的精灵并将它们分配到coin。那么调用重叠函数时coin的值是多少?也许这在技术层面上是正确的,但为了避免混淆,我会针对不同的事情使用不同的变量名称。如果没有其他东西,它会让你的代码更具可读性

其次,在createcoin()中添加到游戏中的硬币精灵不会添加到组中。并且因为它没有添加到组中,所以精灵的物理主体从未启用。

最后,在create()中只调用一次arcade.overlap函数。我认为它应该在update()函数中调用。 Phaser.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(); 
} 
0

,在你升级更新功能,杀死一个硬币不是初始状态,这是什么东西即将发生在某些条件下,这两条线

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; 
     } 

而且驼峰规则是一个极好的补充你的风格,因为它提高可读性(killCoin, createCoin ...)