游戏教程中游戏人物是通过给精灵一个受力的Body让他不断移动。然后通过将Sprite加入到SpriteBatchNode中,进行渲染。同步在移动过程中移动游戏视图,从而达到相机捕捉的作用。
然后问题来了。新版中不是不提倡用SpriteBatchNode,…。然后我直接将Sprite加入到游戏Layer中,运行时发现body和sprite移动距离不同步~。
sprite的移动速度是body的两倍。
而加Sprite加入到SpriteBatchNode中时进行渲染时,整个过程又是正常的,不知道这两者中间到底出了什么问题~
var AnimationLayer = cc.Layer.extend({
spriteSheet: null,
runningAction: null,
sprite: null,
space:null,
body:null,
shape:null,
ctor:function (space) {
this._super();
this.space = space;
this.init();
this._debugNode = new cc.PhysicsDebugNode(this.space);
// this._debugNode.setVisible(false);
// Parallax ratio and offset
this.addChild(this._debugNode, 10);
},
init:function () {
this._super();
// create sprite sheet
cc.spriteFrameCache.addSpriteFrames(res.runner_plist);
this.spriteSheet = new cc.SpriteBatchNode(res.runner_png);
// init runningAction
var animFrames = ];
for (var i = 0; i < 8; i++) {
var str = "runner" + i + ".png";
var frame = cc.spriteFrameCache.getSpriteFrame(str);
animFrames.push(frame);
}
var animation = new cc.Animation(animFrames, 0.1);
this.runningAction = new cc.RepeatForever(new cc.Animate(animation));
//create runner through physic engine
this.sprite = new cc.PhysicsSprite("#runner0.png");
var contentSize = this.sprite.getContentSize();
// init body
this.body = new cp.Body(1, cp.momentForBox(1, contentSize.width, contentSize.height));
this.body.p = cc.p(g_runnerStartX, g_groundHeight + contentSize.height / 2);
this.body.applyImpulse(cp.v(150, 0), cp.v(0, 0));//run speed
this.space.addBody(this.body);
//init shape
this.shape = new cp.BoxShape(this.body, contentSize.width - 14, contentSize.height);
this.space.addShape(this.shape);
this.sprite.setBody(this.body);
this.sprite.runAction(this.runningAction);
//使用SpriteBatchNode,运行正常
//this.addChild(this.spriteSheet);
// this.spriteSheet.addChild(this.sprite);
//直接使用Sprite,出现body和Sprite不同步,Sprite会跑出游戏场景之外
this.addChild(this.sprite);
this.scheduleUpdate();
},
getEyeX:function () {
return this.sprite.getPositionX() - g_runnerStartX;
}
});