重叠在 Phaser 中使用 JavaScript 不起作用

Posted

技术标签:

【中文标题】重叠在 Phaser 中使用 JavaScript 不起作用【英文标题】:overlap not working using JavaScript in Phaser 【发布时间】:2017-07-02 14:35:37 【问题描述】:

我正在制作一个游戏,在这个游戏中,我希望当玩家与硬币重叠时,硬币消失,但不是出于某种原因,我不知道为什么。我在制作地图时尝试了很多方法,甚至将硬币放入瓷砖中,但即便如此它仍然不起作用。

谁能帮帮我?

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




【问题讨论】:

这行肯定是错的game.physics.arcade.overlap(coin, player, killcoin(), null, this); @Musa 是 player.physics.arcade.overlap(coin, player, killcoin(), null, this); 回调部分就是我看的。 idk 我还是不知道该怎么办 您是否可以提供您的 JSON 文件以便我们尝试调试它?或者您能否提供一个最小版本? 【参考方案1】:

您的代码存在三个问题。首先,变量的命名令人困惑。您正在创建一个组并将其分配给coin,但您还添加了新的精灵并将它们分配给coin。那么调用overlap函数时coin的值是多少呢?也许这在技术层面上可以正常工作,但为了避免任何混淆,我会为不同的事物使用不同的变量名。如果不出意外,它会使您的代码更具可读性。

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

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

【讨论】:

【参考方案2】:

在你创建的 create 函数中,在你更新的 update 函数中,杀死硬币不是初始状态,而是在某些条件下即将发生的事情,这两行

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;
        

camelCasing 也是你风格的绝佳补充,因为它提高了可读性(killCoin、createCoin...)

【讨论】:

以上是关于重叠在 Phaser 中使用 JavaScript 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Phaser 3 阵列碰撞

phaser学习总结之phaser入门教程

Phaser 3 街机物理世界环绕

在 Phaser 中销毁精灵

如何将 Spine 动画添加到 Phaser 游戏?

如何在 Phaser 3 中启用单击数组中的单个项目并将每个项目显示在屏幕的不同部分?