ysrikanth2011 Posted April 18, 2013 Share Posted April 18, 2013 Hi , I have programming a game which has paralaxer background. I have acheived the result some how but the code is not efficient . I am using cocos2d-html5 for programming. in the constructor function I have initialized the sprites each of width 1024 and my window width is 800 px like this ctor : function(){var clouds1 = cc.Sprite.create(clouds_img);clouds1.setAnchorPoint(cc.p(0.0,0.0));clouds1.setPosition(cc.p(0,size.height-128));this.addChild(clouds1,-50);var clouds2 = cc.Sprite.create(clouds_img);clouds2.setAnchorPoint(cc.p(0.0,0.0));clouds2.setPosition(cc.p(1024,size.height-128));this.addChild(clouds2,-50);}In the update functionupdate : function(){clouds1.setPositionX(clouds1.getPositionX() - 0.5);if(clouds1.getPositionX() + clouds1.getContentSize().width <= 0){clouds1.setPositionX(1024);}clouds2.setPositionX(clouds2.getPositionX() - 0.5);if(clouds2.getPositionX()+clouds2.getContentSize().width <= 0){clouds2.setPositionX(1024);}} This code is not efficient .I have tried using cocos2d actions CCMoveTo but it is not working : var Parallax = cc.Layer.extend({ctor : function() {//first imagevar firstImage = cc.Sprite.create(first_image);this.addChild(firstimage);var moveAction = cc.MoveTo.create(8,cc.p(-1024,0);var callBack = cc.CallFunc.create(this.reset,this);var seq = cc.Sequence.create(moveAction,callBack);var repeat = cc.RepeatForever.create(seq);firstimage.runAction(repeat);//second imagevar secondImage = cc.Sprite.create(first_image);this.addChild(secondimage);var moveAction = cc.MoveTo.create(16,cc.p(-2048,0)var callBack = cc.CallFunc.create(this.reset,this);var seq = cc.Sequence.create(moveAction,callBack);var repeat = cc.RepeatForever.create(seq);secondImage.runAction(repeat);},this.reset : function() {firstImage.setPositionX(1024);}}); However this code is not working right.when the firstimage is placed first it is actually at cc.p(0,0) and moves to -1024 at a duration 8 ,but while after it moved it's gets placed at 1024 and hence it goes speeder since the duration is same and distance is increased. What ever it might be using cocos2d actions or general javascript logic , I need a more efficient way to solve this problem. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
tyson Posted April 18, 2013 Share Posted April 18, 2013 Hi, could you use ccMoveBy instead? Then move by a distance. -Tys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.