1

在我的项目中,我有一些非静态元素(如示例中的object1),它们具有与之关联的一个精灵纹理。然后我有另一个静态元素(change_sprites_object),我想在其中单击并更改精灵纹理。

我怎样才能在 matter.js 中实现这一点?

我的对象示例:

  var x_object1 = 1370;
  var y_object1 = 725;


  var obj_properties = {
    density: 0.0005,
    frictionAir: 0.01,
    restitution: 0.8,
    friction: 0.01
  };

  var object1_properties = obj_properties;
  jogo_properties.render = {
    sprite: {
      texture: 'images/object1.png'
    }
  };
  object1 = Bodies.circle(x_object1, y_object1, 113, object1_properties);

  change_sprites_object = Bodies.rectangle(1680, 1005, 77, 50, {
    isStatic: true,
    render: {
      sprite: {
        texture: 'images/change_sprites_object.png'
      }
    }
  });

  var flag = 0;
  var element = document.getElementsByTagName('canvas')[0];

  //verify if it's a click or drag
  element.addEventListener("mousedown", function () {
    flag = 0;
  }, false);
  element.addEventListener("mousemove", function () {
    flag = 1;
  }, false);
  element.addEventListener("mouseup", function () {
    if (flag === 0) {
      var mouse = mouseConstraint.mouse;
      if (Bounds.contains(change_sprites_object.bounds, mouse.position)  {
        //change object1 sprite
      }...
4

0 回答 0