Skip to content

18 | Cocos2D-JS | How remove objects with Chipmunk Physics #19

@Gurigraphics

Description

@Gurigraphics

18 | How remove objects with Chipmunk Physics

Game.layers.js

Add Listener

Game.layers[1].extend = cc.Layer.extend({
    init:function () {
  (... code ...)
        cc.eventManager.addListener( Game.touch, game );
    }
  (... code ...)
});

Touch event

Game.touch = cc.EventListener.create({
    event: cc.EventListener.TOUCH_ONE_BY_ONE,
    onTouchBegan: function ( touch, event ) {

        console.log( "touch ");

        Game.removeItemById( "tag_1" );
        
        //Game.removeItemsByType( "box" );

        Game.removeItemByTouch( touch, event, "box" );  
    }       
});

Remove item by id

Game.removeItemById = function( id ){

    for( var i = coliders.length - 1; i >= 0; i-- ){
        if( coliders[i].id == id ){
            layer.removeChild( coliders[i].image );
            world.removeBody( coliders[i].getBody() )
            world.removeShape( coliders[i] )
            coliders.splice( i, 1 );
            break
        }
    }
}

Remove items by type

Game.removeItemsByType = function( type ){

    for( var i = coliders.length - 1; i >= 0; i-- ){
        if( coliders[i].name == type ){
            layer.removeChild( coliders[i].image );
            world.removeBody( coliders[i].getBody() )
            world.removeShape( coliders[i] )
            coliders.splice( i, 1 );       
        }
    }
}

Remove item by touch

Game.removeItemByTouch = function( touch, event, type ){

    for( var i = coliders.length - 1; i >= 0; i-- ){
        if( coliders[i].pointQuery( cp.v( touch.getLocation().x, touch.getLocation().y ) ) != undefined ){
            if( coliders[i].name == type ){
                layer.removeChild( coliders[i].image );
                world.removeBody( coliders[i].getBody() );
                world.removeShape( coliders[i] );
                coliders.splice( i, 1 ); 
                break
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions