Javascript Cocos2D JS花栗鼠PhysicsSprite移动动作在Android手机中不起作用
我正在尝试开发一个简单的跨平台游戏,并尝试使用cocos2djs将一个有身体的PhysicsSprite移动到我触摸/点击的位置 以下是我的代码:Javascript Cocos2D JS花栗鼠PhysicsSprite移动动作在Android手机中不起作用,javascript,android,chipmunk,cocos2d-js,Javascript,Android,Chipmunk,Cocos2d Js,我正在尝试开发一个简单的跨平台游戏,并尝试使用cocos2djs将一个有身体的PhysicsSprite移动到我触摸/点击的位置 以下是我的代码: var TAG_SPRITE = 1; var AnimationLayer = cc.Layer.extend({ space:null, ctor:function (space) { this._super(); this.space = space; this.init();
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对象并删除所有其他身体和形状内容时,它可以移动到我触摸的位置。
问题是,我可以在浏览器中移动PhysicsSprite,而在Android手机中则无法移动
注意:我使用的是花栗鼠物理引擎,我不知道这是真正的解决方案,或者应该被视为一种变通方法,但下面的代码对web和Android都适用。但仍然不知道为什么问题中的代码在Android上不起作用,而在web上却起作用。(如果两者都不起作用,那就更有意义了……) 我试图移动精灵的身体而不是它自己。新的
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);
}
希望这能帮助遇到同样问题的人