javascriptcraftyjs

Collision detection on a tweened object


I cann't get it to work, seems like tweened objects don't fire the 'Moved' event?

I'm tweening a object across the screen, and need to check if it collided with the player (that flies up and down by the keyboard events). It works if I append the check on 'Moved' for the player, but what if the player stands still :-)

So I need to check when the tweened objects moves if it hit the player

Does not fire any of the events:

        // astroid
        Crafty.e('astroid, 2D, DOM, Color, Tween, Collision')
            .attr({ x: 600, y: 550, w: 50, h: 200 })
            .color('#8e44ad')
            .tween({ x: -50 }, 4000)
            .bind('Moved', function () {

                console.log('moved');

                if (this.hit('player1')) {
                    console.log('player hit 1');
                }
            })
            .onHit('player', function () {
                console.log('player hit 2');
            });

Solution

  • Updated :

    I am using little plugin Here ,collisions($div1, $div2):

    So,

     function interval() {
    
       timeout = setTimeout(function () {
    
            if (collisions($('.player1'), $('.astroid'))) {
               alert('true');
    
            }
            else{
                interval();
            }
        }, 25);
    
     }
    
     interval()
    

    WORKING DEMO

    May this was helpful ...