Jump to content

how to Drag event on P2 physics ?


guddy
 Share

Recommended Posts

hi ,

I am can't add drag event after i applied p2 physics to the sprite . I have write my code .So plz help me out .

thanks

 

 

//code

Game.GameScreen = function () {
 
 
};
Game.GameScreen.prototype = {
    create:function () {
        this.backgroundLayer = configObj.game.add.group();
        this.backgroundLayer.z = 0;
 
        this.backGround = configObj.game.add.tileSprite(0, 0, 640, 832, 'bg');     //background image
        this.backgroundLayer.add(this.backGround);
        this.Level1 = [1,  2,   0,   0,   0,   1,  0,     0];
        this.Xpos =   [50, 100,  50,  50,  100, 50, 100,   50];
        this.Ypos =   [100, 200,300, 350, 180, 280, 380,  480];
        this.RowNumber = 4;
        this.ColNumber = 2;
        this.SetVariable();
 
    },
    SetVariable:function () {
        this.blockArray = [];
        this.enablePhysic();
        this.createBlock();
 
    },
    enablePhysic : function(){
        configObj.game.physics.startSystem(Phaser.Physics.P2JS);
        configObj.game.physics.p2.setImpactEvents(true);
        configObj.game.physics.p2.restitution = 0.8;
        this.playerCollisionGroup =  configObj.game.physics.p2.createCollisionGroup();
        this.pandaCollisionGroup =  configObj.game.physics.p2.createCollisionGroup();
        configObj.game.physics.p2.updateBoundsCollisionGroup();
 
    },
    createBlock : function(){
        var mWidth,mHeight;
        var pandas = configObj.game.add.group();
        pandas.enableBody = true;
        pandas.physicsBodyType = Phaser.Physics.P2JS;
        for(var i = 0 ; i < this.ColNumber ;i++ ){
            this.blockArray = [];
            for(var j = 0; j < this.RowNumber ; j++){
                var pos = j*this.ColNumber+i;
                if(this.Level1[pos] != 0){
                    this.image = 'block'+this.Level1[pos];
                    var panda =  configObj.game.add.sprite( this.Xpos[pos], this.Ypos[pos], this.image);
                    configObj.game.physics.p2.enable(panda,true);
                    panda.body.moves = false
 
                    panda.body.setRectangle(40, 40);
                    panda.body.setCollisionGroup(this.pandaCollisionGroup);
                    panda.body.collides([this.pandaCollisionGroup, this.playerCollisionGroup]);
                    panda.inputEnabled = true;
                    panda.input.enableDrag( true);
 
                }
 
            }
        }
 
        panda.body.collides(this.pandaCollisionGroup, this.hitPanda, this);
    },
    hitPanda : function(){
        console.log("in cooool");
    },
    update : function(){
 
    }
 
};
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...