Cocos2d Scrolling Background


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //Create our scrolling background
  2. - (void)setupBackgroundImage
  3. {
  4. //create both sprite to handle background
  5. background1 = [CCSprite spriteWithFile:@"cloud_bg1.png"];
  6. background2 = [CCSprite spriteWithFile:@"cloud_bg2.png"];
  7.  
  8. background1.position = ccp(0, size.height/2);
  9. background2.position = ccp(size.width, size.height/2);
  10.  
  11. //add them to main layer
  12. [self addChild:background1 z:0];
  13. [self addChild:background2 z:0];
  14.  
  15. //add schedule to move backgrounds
  16. [self schedule:@selector(scroll:)];
  17. }
  18.  
  19. - (void)scroll:(ccTime)dt {
  20.  
  21. background1.position = ccp( background1.position.x - 2, background1.position.y );
  22. background2.position = ccp( background2.position.x - 2, background2.position.y );
  23.  
  24. //reset position when they are off from view.
  25. if (background1.position.x == - (size.width/2)) {
  26. background1.position = ccp(size.width+(size.width/2), size.height/2);
  27. }
  28. else if (background2.position.x == - (size.width/2)) {
  29. background2.position = ccp(size.width+(size.width/2), size.height/2);
  30. }
  31.  
  32. }
  33.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.