如何在点击其他对象时更改/更新物品中的精灵纹理?

时间:2016-02-18 17:04:54

标签: javascript textures sprite physics-engine matterjs

在我的项目中,我有一些非静态元素(例如 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
      }...

0 个答案:

没有答案