Jump to content

Writing Paralaxer background in cocos2d-html5.


ysrikanth2011
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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