Jump to content

Collider between two groups doesn't fire


Allan11
 Share

Recommended Posts

I have a function which is called on update

import enemy from './enemy';

export const shoot = ({ scene }) => {
  scene.input.on('pointerdown', function(pointer) {
    if (pointer.button === 0) {
      gun({
        scene,
        fromX: playerObj.x,
        fromY: playerObj.y,
        toX: pointer.position.x,
        toY: pointer.position.y,
        enemy,
      });
    }
  });
};

In my gun function

const gun = ({ scene, fromX, fromY, toX, toY, enemy }) => {
  gunPhysicsGroup = scene.physics.add.group();

  bullet = scene.add.circle(fromX, fromY, 2, 0xff0000);

  gunPhysicsGroup.add(bullet);

  const delta = Math.sqrt(Math.pow(toX - fromX, 2) + Math.pow(toY - fromY, 2));
  const velocityX = (speed / delta) * (toX - fromX);
  const velocityY = (speed / delta) * (toY - fromY);

  gunPhysicsGroup.setVelocity(velocityX, velocityY);

  //The callback here doesn't fire
  scene.physics.add.collider(bullet, enemy, () => console.log('heeee'));
};

and an enemy is this function

let enemyPhysicsGroup = null;
let enemyObj = null;

const enemy = ({ scene }) => {
  enemyPhysicsGroup = scene.add.group({
    bounceX: 1,
    bounceY: 0.2,
  });

  for (var i = 1; i < 10; i++) {
    enemyObj = scene.add.circle(300 + i * 20, 100, 10, 0xabcdef);
    enemyObj = scene.add.circle(300 + i * 20, 120, 10, 0xabefac);
    enemyObj = scene.add.circle(300 + i * 20, 140, 10, 0xeba2bc);
  }
};

export default enemy;

Everything works fine - the bullet gets fired on mouse click, the enemy is drawn but the collision between them is not registered.

Any help will be appreciated.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...