2015-10-31 30 views
0

我正在嘗試開發一個簡單的跨平臺遊戲,並嘗試使用Cocos2D-JS將BodySprite和Body移動到我觸摸/單擊的位置。Cocos2D-JS Chipmunk PhysicsSprite移動操作在Android手機中不起作用

這裏是我的代碼:

var TAG_SPRITE = 1; 

var AnimationLayer = cc.Layer.extend({ 
    space:null, 

    ctor:function (space) { 
     this._super(); 
     this.space = space; 
     this.init(); 
    }, 
    init:function() { 
     this._super(); 

     var winsize = cc.director.getWinSize(); 

     //init physics sprite 
     var spriteHead = new cc.PhysicsSprite(res.Head_png); 
     var headContentSize = spriteHead.getContentSize(); 

     //init body 
     var headBody = new cp.Body(1, cp.momentForBox(1, headContentSize.width, headContentSize.height)); 
     headBody.p = cc.p(winsize.width/2, winsize.height/3); 
     this.space.addBody(headBody); 

     //init shape 
     var headShape = new cp.CircleShape(headBody, headContentSize.width/2, cp.v(0, 0)); 
     headShape.setFriction(0.3); 
     headShape.setElasticity(0.8); 
     this.space.addShape(headShape); 

     spriteHead.setBody(headBody); 

     this.addChild(spriteHead, 0, TAG_SPRITE); 

     //for mobile 
     if('touches' in cc.sys.capabilities){ 
      cc.eventManager.addListener({ 
       event: cc.EventListener.TOUCH_ONE_BY_ONE, 
       swallowTouches: true, 
       onTouchBegan:function (touch, event) { 
        cc.log('touch began'); 
        event.getCurrentTarget().moveSprite(touch.getLocation()); 
        return true; 
       }, 
       onTouchMoved: function (touch, event) { 
       }, 
       onTouchEnded: function (touch, event) { 
       }, 
       onTouchCancelled: function (touch, event) { 
       } 
      }, this); 
     } 
     //for desktop 
     else if ('mouse' in cc.sys.capabilities) { 
      cc.eventManager.addListener({ 
       event: cc.EventListener.MOUSE, 
       onMouseUp: function (event) { 
        event.getCurrentTarget().moveSprite(event.getLocation()); 
       } 
      }, this); 
     } 
    }, 
    moveSprite:function(position) { 
     cc.log('move to: ' + position.x + ',' + position.y); 
     var sprite = this.getChildByTag(TAG_SPRITE); 
     var moveAction = new cc.moveTo(1, position); 
     sprite.runAction(moveAction); 
    } 
}); 

當我看到日誌從logcat的,它可以處理觸摸事件,但不能移動精靈。當我將PhysicsSprite轉換爲Sprite對象並刪除所有其他Body和Shape的東西時,它可以移動到我觸摸的位置。 問題是我可以在瀏覽器中移動PhysicsSprite,但我無法在Android手機中完成該操作。

注:我用花栗鼠物理引擎

回答

0

我不知道這是真正的解決辦法還是應該被視爲一種解決方法,但下面的代碼工作正常Web和Android系統。但仍然不知道爲什麼問題中的代碼不適用於Android,而適用於網絡。 (如果它對兩者都不起作用會更有意義...)

我試圖移動精靈的主體而不是它本身。新moveSprite方法是這樣的:

moveSprite: function(sprite){ 
    cc.log('move to: ' + position.x + ',' + position.y); 
    var sprite = this.getChildByTag(TAG_SPRITE); 
    var body = sprite.getBody(); 
    var velocity = 300; 
    this.moveWithVelocity(body, position, velocitiy); 
} 

moveWithVelocity是我同層中創建自定義函數,身體移動到目標點與特定的速度:

moveWithVelocity: function(body, destination, velocity){ 
    var deltaX = destination.x - body.p.x; 
    var deltaY = destination.y - body.p.y; 
    var distance = Math.sqrt(Math.pow(deltaX,2) + Math.pow(deltaY,2)); 
    var time = distance/velocity; 
    var velocityX = deltaX/time; 
    var velocityY = deltaY/time; 

    //start the action with the calculated velocity in the calculated direction 
    body.applyImpulse(cc.v(velocityX, velocityY), cc.v(0,0)); 

    //stop the sprite (or body here) when duration of movement is time out (or when the sprite/body arrives its destination) 
    setTimeout(function(){ 
     body.setVel(cc.v(0,0)); 
    }, time*1000); 

} 

希望這有助於任何人都遇到同樣的問題。