简体   繁体   English

移相器js中的game.physics.arcade.collide()

[英]game.physics.arcade.collide() in phaser js

I use framework phaser js. 我使用框架移相器js。 I create a two circles using graphics 我使用graphics创建两个圆圈

var graphics;
var ball;

var graphics1;
var ballHome;

function create(){
  game.physics.startSystem(Phaser.Physics.ARCADE);  

    graphics=game.add.graphics(500,500);    //first circle
    game.physics.arcade.enable(graphics);
    graphics.enableBody=true;
    graphics.beginFill(0xFFFFFF,1);
    ball=graphics.drawCircle(10,10,15);
    graphics.endFill();


    graphics1=game.add.graphics(500,500);   //second circle
    game.physics.arcade.enable(graphics1);
    graphics1.enableBody = true;
    graphics1.beginFill(0xFFF55F,1);
    ballHome=graphics1.drawCircle(300,300,500);
    graphics1.endFill();

}

function update() {
     game.physics.arcade.collide(ball,ballHome); 
}

I want them to collide 我要他们碰撞

Why does not game.physics.arcade.collide(ball,ballHome) work? 为什么game.physics.arcade.collide(ball,ballHome)不起作用?

Thanks for the help 谢谢您的帮助

The problem is that the collide() function takes Phaser.Sprite s as its input, but ball and ballHome are not sprites; 问题是collide()函数将Phaser.Sprite s作为其输入,但是ballballHome不是sprites。 they are PIXI.Graphics objects. 它们是PIXI.Graphics对象。 You need to create sprites from your Graphics objects, and then pass those sprites into collide() . 您需要从Graphics对象创建精灵,然后将这些精灵传递到collide()

To make a sprite from your graphics object, first call Graphics.generateTexture() ( doc ) to create a texture, then call game.add.sprite() ( doc ) with the texture you just created. 要从graphics对象制作精灵,请首先调用Graphics.generateTexture()doc )创建纹理,然后使用刚创建的纹理调用game.add.sprite()doc )。

For more information on Phaser, plus loads of really helpful tutorials, see the Phaser website . 有关Phaser的更多信息,以及大量有用的教程,请访问Phaser网站

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

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