简体   繁体   English

Phaser-如何使用Phaser.js正确实现Arcade Physics Collisions

[英]Phaser - How to properly implement Arcade Physics Collisions with Phaser.js

I've scanned the web for some tutorials and looked at the documentation but it just seems to get me more confused. 我已经在网上扫描了一些教程,并查看了文档,但这似乎使我更加困惑。

The problem: 问题:

All sprites have their physics initialised in this code: 此代码中初始化了所有精灵的物理特性:

// Store all Sprites in an Array for easy access
var x64_guys = [Player, Dave, EvilDave];

// Sprite physics properties. Give the little guys some life.
hal.physics.arcade.enable(x64_guys, Phaser.Physics.ARCADE);

for(var j=0; j<x64_guys.length;j++){
    x64_guys[j].body.collideWorldBounds = true;
    x64_guys[j].body.bounce.x = true;
    x64_guys[j].body.bounce.y = true;
}

Where hal is equal to new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, "stage", { preload: preload, create: create, update: update }); 其中hal等于new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, "stage", { preload: preload, create: create, update: update });

Now I am assuming this works fine to initialise physics like this? 现在我假设这样可以很好地初始化物理?

If so then I am rather confused on how to detect Player and EvilDave collisions, and also launch a function on collide (eg Player death or EvilDave getting hurt). 如果是这样,那么我对如何检测PlayerEvilDave碰撞以及在碰撞时启动功能(例如, Player死亡或EvilDave受伤) EvilDave (Chars are 64x64 pixels) (字符为64x64像素)

If anyone could help me I'd be grateful. 如果有人可以帮助我,我将不胜感激。

Are Player , EvilDave , etc instances of Sprites? PlayerEvilDave等Sprites 实例吗? You don't show the code so it's hard to be sure. 您没有显示代码,因此很难确定。

The call to enable physics should be like this: 启用物理的调用应如下所示:

hal.physics.arcade.enable(x64_guys);

Although I would get out of the practise of using 'hal' at all and instead use 'this'. 尽管我会完全放弃使用“ hal”的做法,而使用“ this”。 You don't need the constant as the 2nd argument because you're enabling direct on the Arcade Physics manager anyway. 您不需要将常量作为第二个参数,因为无论如何您都将在Arcade Physics Manager上直接启用。

Is all of the above code happening in the create function? 以上所有代码是否都在create函数中发生? If not, it should be. 如果不是,应该是。

This is also wrong: body.bounce.x = true; 这也是错误的: body.bounce.x = true; Bounce is a Phaser.Point object, not a boolean, so it expects a value to be assigned to it. Bounce是一个Phaser.Point对象,而不是布尔值,因此它希望为其分配一个值。 If you want to enable 100% fully reflective bouncing then set it to 1: body.bounce.x = 1; 如果要启用100%全反射反射,则将其设置为1: body.bounce.x = 1;

To check collision between Player and EvilDave you need to add this to your update function: 要检查PlayerEvilDave之间的冲突,您需要将其添加到update功能中:

function update() {

    // object1, object2, collideCallback, processCallback, callbackContext
    game.physics.arcade.collide(Player, EvilDave, collisionHandler, null, this);

}

function collisionHandler (obj1, obj2) {

    //  The two sprites are colliding
    game.stage.backgroundColor = '#992d2d';

}

You can see the full code for this in the Sprite vs. Sprite example on the site. 您可以在网站上的Sprite vs. Sprite示例中看到完整的代码。 Would be well worth looking over that carefully. 值得仔细研究一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM