3

i've some problems with craftyjs, collision and polygons.

Here is my code: http://jsfiddle.net/haenx/85mhj/

As you can see, the player will hit the enemy each time both boxes are overlapping. But the given polygon has another form and it semms it will be ignored...

I can't figure out, why. The docs from craftys are also not the best.

Crafty.init(300,300, document.querySelector('#Game'));
Crafty.e('2D, Canvas, Color, Mouse')
    .attr({
        x: 0,
        y: 0,
        w: 300,
        h: 300
    })
    .color('#333')
    .bind('MouseMove', function () {
        player.x = Crafty.mousePos.x - (32 / 2);
        player.y = Crafty.mousePos.y - (32 / 2);
    });

Crafty.c('Enemy', {
    init: function () {
        this
            .addComponent('2D, Canvas, Color')
            .attr({
                x: 134,
                y: 134,
                w: 32,
                h: 32
            })
            .color('#ff0');
    }
})

var enemy = Crafty.e('Enemy');

var player = Crafty.e('2D, Canvas, Color, Collision, WiredHitBox')
    .attr({
        x: 134,
        y: 134,
        w: 32,
        h: 32
    })
    .color('#f00');
player
    .collision(new Crafty.polygon([0,0], [0,6], [8, 13], [24,13], [32,6], [32,0]))
    .onHit('Enemy',
        function() {
            enemy.color('#fff');
        },
        function() {
            enemy.color('#ff0');
        }
    );
danbruegge
  • 2,104
  • 3
  • 20
  • 27
  • 1
    I solved the problem, by defining the "Collision" component also to the enemy entity. Thx to starwed from github: https://github.com/craftyjs/Crafty/issues/709#issuecomment-33719093 – danbruegge Jan 30 '14 at 18:55
  • 1
    The new fiddle: http://jsfiddle.net/85mhj/12/ – danbruegge Jan 30 '14 at 18:56

0 Answers0