0

I am building this simple HTML5 drag and drop game using Craftyjs.

  1. I created an entity let's call it E1 with some components, one of the components is "Draggable".
  2. I created a new entity E2 and made it as a clone of E1 (now E2 should have a copy of all E1's properties and components)
  3. on run, E2 is cloned with the same properties and attribute but it is not draggable!

    var E1 = Crafty.e("2D, Canvas, apple, Draggable, Gravity, Collision");

    var E2 = E1.clone();

    E2.attr({x:100, y:100});

    E2.addComponent("Draggable");

    E2.enableDrag(); E2.bind("Dragging", function(){ console.log("E2 is being dragged"); });

I tried to:

  1. force add the Draggable component with E2.addComponent("Draggable") but it won't work!
  2. bind the Draggable events like "Dragging" and "StartDrag" but it won't work either!
  3. force enable drag by using "enableDrag"method but that did not have any effect either :(
  4. consloe.log whether or not E2 has Draggable component by using E2.has("Draggable") and it returned yes!

FYI: all other functions like MouseUp and MouseOver are not working for E2 as well even if I add them later any ideas of what is making the Draggable component not working in the cloned entity?

Alsmayer
  • 236
  • 1
  • 4
  • 13

1 Answers1

0

The following minimal example works for me using the latest stable release (v0.6.3).
Do you clone the entity on initialization of your game? Cloning the entity while it is being dragged might lead to inconsistent state.

<html>
  <head></head>
  <body>
    <div id="game"></div>
    <script src="https://rawgithub.com/craftyjs/Crafty/release/dist/crafty-min.js"></script>
    <script>
        Crafty.init(600, 300, document.getElementById('game'));
        Crafty.background('rgb(127,127,127)');

        var ent1 = Crafty.e('2D, Canvas, Color, Draggable')
              .attr({x: 200, y: 50, w: 50, h: 50})
              .color('blue');

        var ent2 = ent1.clone()
              .attr({x:100, y:100})
              .color('green');
    </script>
  </body>
</html>
mucaho
  • 2,119
  • 20
  • 35