Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5974173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:54:20+00:00 2026-05-22T20:54:20+00:00

I have three images (320×480) that I’m trying to scroll vertically in my Cocos2D

  • 0

I have three images (320×480) that I’m trying to scroll vertically in my Cocos2D application.

In my initialization method, I have the following:

//adding background sprites
background = [CCSprite spriteWithFile:@"BG1.png"];
background2 = [CCSprite spriteWithFile:@"BG2.png"];

//position background sprites
background.position = ccp(size.width, size.height/2);
background2.position = ccp(size.width, size.height*2);

//schedule to move background sprites
[self schedule:@selector(scroll:)];

//adding them to the main layer
[self addChild:background z:0];
[self addChild:background2 z:0];

And here’s my scroll method:

-(void) scroll:(ccTime)dt 
{
//move 30*dt px vertically
background.position = ccp(background.position.x, background.position.y - 30*dt);
background2.position = ccp(background2.position.x, background.position.y - 30*dt);

//reset offscreen position
if (background.position.y < 290)
{
    background.position = ccp(480/2, 480);
}else if (background2.position.y < 290)
{
    background2.position = ccp(480/2,480);
}
}

Currently what’s happening is my first background image is offset by about a quarter of the screen (horizontally), and it starts a quarter of the way up from the bottom of the screen, but it scrolls down. My second background image doesn’t actually spawn, the first image just loops over and over while being offset. Is there any way to make the two images smoothly loop continuously in the background, and how would I incorporate a third image?

Also, just a quick side question, is it bad to name objects (I think they’re objects) with numbers in their name (ie background2/background3)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:54:21+00:00Added an answer on May 22, 2026 at 8:54 pm

    Tested for horizontal scrolling in landscape mode (all you have to do is change the scrolling from horizontal to vertical, you should be able to figure this out) dont forget that ccposition is from the middle of the sprite, not from 0,0 perspective…:

        CGSize size = [CCDirector sharedDirector].winSize;
    
        //adding background sprites
        background = [CCSprite spriteWithFile:@"tracktest.png"];
        background2 = [CCSprite spriteWithFile:@"tracktest.png"];
        [background.texture setAliasTexParameters];
        [background2.texture setAliasTexParameters];
    
        //position background sprites
        background.position = ccp(background.contentSize.height/2,background.contentSize.width/2);
        background2.position = ccp(size.width,0);
    
        //schedule to move background sprites
        [self schedule:@selector(scroll:)];
    
        //adding them to the main layer
        [self addChild:background z:0];
        [self addChild:background2 z:0];
    

    -scroll method:

    -(void) scroll:(ccTime)dt 
    {
            //move 30*dt px vertically
      if (background.position.x<background2.position.x){
          background.position = ccp(background.position.x - 30*dt,background.contentSize.height/2);
          background2.position = ccp(background.position.x+background.contentSize.width,background2.contentSize.height/2);
      }else{
          background2.position = ccp(background2.position.x- 30*dt,background2.contentSize.height/2);
          background.position = ccp(background2.position.x+background2.contentSize.width ,background.contentSize.height/2);
    
      }
    
      //reset offscreen position
      if (background.position.x <-background.contentSize.width/2)
      {
          background.position = ccp(background2.position.x+background2.contentSize.width,background.contentSize.width/2);
      }else if (background2.position.x < -background2.contentSize.width/2)
      {
          background2.position = ccp(background.position.x+background.contentSize.width, background2.contentSize.width/2);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three images that I want to cycle through when clicking on a
I have made a Xcode project for a tabbed application that displays images of
I have three web application projects that share a common library of custom server
I have three images, two of these images animate as follow and third image
I have three images: the first is a light switch, the second is an
I have three divs page-left page-right flipper I have images of equal size in
I basically have three tables, posts, images and postimages (this simply contains the ids
So, I have three server, and the idea was to keep all media (images,
I have a folder with a few background images (one.jpg, two.jpg, three.jpg) , and
I have three videos: a lecture that was filmed with a video camera a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.