Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Co-Ordination Between Layers while Bakground Moves with Different Speed

I am developing an iPhone Game using COCOS2d Framework and Objective-C (in Landscape mode/view).

For the game, I am using a Background which has Four Layers ('Sky' on Top, 'Mountain' below sky, 'Hill' below Mountain and 'Foreground' extreme below). Here I need to move each Layer of the Background with a different speed like 'Sky' should move slower than Mountain, 'Mountain' should move slower than Hill and 'Hill' should move slower than Foreground.

While I am moving each Layer with a different speed, the game experiences a BREAK-UP BETWEEN CO-ORDINATION OF the LAYERS.

I tried resolving this with logic like: increasing image/Layer(width) size according to it's speed so that each layer should end and start running again with co-ordination. Couldn't get it to work.

This is my code for Moving the Background:

 -(void) backgroundmoving
{

 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  //return kDevice_iPad;

  // Code for Each Layer's Moving Speed FOR iPad
  bk_f -=1.0;
  bk_f1 -=1.4;
  bk_f2 -=1.8;
  bk_f3 -=2.2;
 } else {

  // Code for Each Layer's Moving Speed FOR iPhone
  bk_f -=0.2;
  bk_f1 -=0.4;
  bk_f2 -=0.6;
  bk_f3 -=0.8;

 }

 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

  //return kDevice_iPad;
  if (bk_f <= -920*1.1) {bk_f=0;}
  if (bk_f1 <= -920*1.1) {bk_f1=0;}
  if (bk_f2 <= -920*1.1) {bk_f2=0;}
  if (bk_f3 <= -920*1.1) {bk_f3=0;}
  if (bk_f4 <= +920*1.1) {bk_f4=0;}
  } else {
  if (bk_f <= -480*2) {bk_f=0;}
  if (bk_f1 <= -480*2) {bk_f1=0;}
  if (bk_f2 <= -480*2) {bk_f2=0;}
  if (bk_f3 <= -480*2) {bk_f3=0;}
  if (bk_f4 <= +480*2) {bk_f4=0;}


 }

 _level_bkgrnd.position = ccp(bk_f, 0);
 _level_bkgrnd1.position = ccp(bk_f1, 0);
 _level_bkgrnd2.position = ccp(bk_f2, 0);
 _level_bkgrnd3.position = ccp(bk_f3, 0);

}

Above code gives me the background issue. Am providing screenshot links for reference:

1) http://screencast.com/t/seUjXClz

2) http://screencast.com/t/8tHq2KYnnMa

Any help pointing me in the right direction would be greatly appreciated.

Thanks In Advance :)

like image 203
NSExpression Avatar asked Mar 13 '12 09:03

NSExpression


1 Answers

cocos2d has built in support for this, checkout some example code with Parallax scrolling. http://www.cocos2d-iphone.org/archives/22

like image 143
amleszk Avatar answered Nov 15 '22 22:11

amleszk