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

  • Home
  • SEARCH
  • 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 8198239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:54:34+00:00 2026-06-07T05:54:34+00:00

I have been trying to create a Class for my OpenGL ES 2.0 +

  • 0

I have been trying to create a Class for my OpenGL ES 2.0 + GLKit game on iOS. When I increase the speed of the scrolling, the gap between the sprites gets larger. I have done a lot of searching on Google but have not found an answer that has worked for me.

My Scrolling sprite class inherits from a ‘Node’ with position, scale, children etc. (a bit like Cocos2D). The class has an array of sprites (nodes), that move with the set velocity on the x axis and when reaching the end of the screen move to the right position of the last sprite.

Here is the main part of my code:

    -(id)initWithTexture:(GLKTextureInfo *)texture effect:(GLKBaseEffect *)effect xScrollRange:(float)range yPosition:(float)y xScrollVelocity:(float)velocity{
if (self = [super init]) {
        //set globals
    self.velocity = velocity;
    xRange = range;

        //create a first sprite, set position, add to children
    JGSprite *firstSprite = [[JGSprite alloc] initWithTexture:texture effect:effect];
    firstSprite.position = GLKVector2Make(range, y);
    [self.children addObject:firstSprite];

        //calc how many sprites are needed to cover range+1
    float spritesNum = range/firstSprite.contentSize.width;
    int spritesNumRound = roundf(spritesNum)+1;

        //add enough sprites to cover the screen
    for (int i = 1; i < spritesNumRound; i++) {
            //create, set position, add to children
        JGSprite *sprite = [[JGSprite alloc] initWithTexture:texture effect:effect];
        sprite.position = GLKVector2Make(range - (i*sprite.contentSize.width), y);
        [self.children addObject:sprite];

    }

        //if moving left, set last sprite as right most
    if (velocity < 0)
        lastReorderedSprite = 0;
    else    //set left most
        lastReorderedSprite = self.children.count-1;



   }

    return self;
}

-(void)update:(float)dt
{
        //loop through sprites
    for (JGNode *node in self.children)
    {
            //update sprites position
        node.position = GLKVector2Make(node.position.x + self.velocity*dt, node.position.y);

        //if moving left
    if (self.velocity < 0)
    {
            //if reached gone off screen
        if (node.position.x <= -node.contentSize.width/2) 
        {
                //get last node
            JGNode *lastSprite = [self.children objectAtIndex:lastReorderedSprite];

                //set the position to the right of the last node
            node.position = GLKVector2Make(lastSprite.position.x+lastSprite.contentSize.width, node.position.y);

                //set re-positioned node as lastreordered
            lastReorderedSprite = [self.children indexOfObject:node];
        }            
    }
    else //moving right
    {
            //gone off screen   
        if (node.position.x >= xRange+node.contentSize.width/2) 
        {
                //get last node
            JGNode *lastSprite = [self.children objectAtIndex:lastReorderedSprite];

                //set the position to the left of the last node
            node.position = GLKVector2Make(lastSprite.position.x-node.contentSize.width, node.position.y);

                //set re-positioned node as lastreordered
            lastReorderedSprite = [self.children indexOfObject:node];

        }

    }
    }
}

If anyone could help by telling me how I would go about stopping a gap forming, it would be much appreciated 🙂 Thanks in advance!

  • 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-06-07T05:54:36+00:00Added an answer on June 7, 2026 at 5:54 am

    The problem is most likely this line:

    node.position = GLKVector2Make(node.position.x + self.velocity*dt, node.position.y);
    

    When you increase velocity, the amount added to x increases. My guess is that one of your objects is having its speed updated later than the others. To fix it, just have each object check that it’s the distance that you want it to be from the others and if it isn’t, move it there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to create an array of a class containing two values,
I have been trying for some time to create a class/series of classes that,
I have been trying to create a templated class( Test2 ) that takes 2
I'm new to both android and game development and have been trying to create
I have been trying to create a simple class, which defines default property values
I have been trying to create a ListView which I can sort using drag
I have been trying to create a menu panel with jQuery that can be
I have been trying to create a new google custom search engine, but when
I have been trying to create a simple program with Python which uses OpenCV
I have been trying to create an observable tweeter feed using tweetsharp with the

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.